Variable assignment (3)

Every tasty fruit basket needs oranges, so you decide to add six oranges. As a data analyst, your reflex is to immediately create the variable my_oranges and assign the value 6 to it. Next, you want to calculate how many pieces of fruit you have in total. Since you have given meaningful names to these values, you can now code this in a clear way:
my_apples + my_oranges

Instruction

# Assign a value to the variables my_apples and my_oranges my_apples <- 5 # Add these two variables together # Create the variable my_fruit # Assign a value to the variables my_apples and my_oranges my_apples <- 5 my_oranges <- 6 # Add these two variables together my_apples + my_oranges # Create the variable my_fruit my_fruit <- my_apples + my_oranges test_object("my_apples", incorrect_msg = "Keep the line that assigns 5 to `my_apples`.") test_object("my_oranges", incorrect_msg = "Keep the line that assigns 6 to `my_oranges`.") test_output_contains("my_apples + my_oranges", incorrect_msg = "Make sure to print out the result of adding `my_apples` and `my_oranges`. The code example in the description already gives away the answer to this instruction!") msg <- "Have you used `my_fruit <- my_apples + my_oranges` to create the `my_fruit` variable?" test_object("my_fruit", undefined_msg = msg, incorrect_msg = msg) success_msg("Nice one! The great advantage of doing calculations with variables is reusability. If you just change `my_apples` to equal 12 instead of 5 and rerun the script, `my_fruit` will automatically update as well. Continue to the next exercise.")

my_fruit is just the sum of my_apples and my_oranges. You can use the + operator to sum the two and <- to assign that value to the variable my_fruit.

Previous: 1-4 | Variable assignment (2)

Next: 1-6 | Apples and oranges

Back to Main