DataCamp Exercise

It is time for DATA CAMP exercises!

Before you start: No previous R programming is required.
Setup: Use the embedded browser exercises. For local practice, run the corresponding commands in R.
Your check: Predict arithmetic/assignment results, then compare the exercise feedback. Practise in the unit summary.

Take your first steps with R. To test your knowledge up to now you can test yourself in the following exercises.

  • R makes use of the # sign to add comments, so that you and others can understand what the R code is about. Comments are not run as R code, so they will not influence your result. You will find the task written as a comment.
  • Write directly in the script.R window on the left. The comments are telling you what to do.
  • When you are ready press Submit and every line of code is interpreted and executed by R and you get a message whether or not your code was correct.
  • The output of your R code is shown in the Console panel on the right.
  • Press Hint for tips.
  • Press Solution for a new tab showing the solution.
  • Be patient: this may take some time to load.

Simple calculation to start:

# no pec # Calculate 3 + 4 3 + 4 # Calculate 6 + 12 # Calculate 3 + 4 3 + 4 # Calculate 6 + 12 6 + 12 test_output_contains("18", incorrect_msg = "Make sure to add `6 + 12` on a new line. Do not start the line with a `#`, otherwise your R code is not executed!") success_msg("Awesome! See how the console shows the result of the R code you submitted? Now that you're familiar with the interface, let's get down to R business!")

Just add a line of R code that calculates the sum of 6 and 12, just like the example in the sample code!

Arithmetic calculations

In its most basic form, R can be used as a simple calculator with the following arithmetic operators:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: ^
  • Modulo: %%

The last two might need some explaining:

The ^ operator raises the number to its left to the power of the number to its right: for example 3^2 is 9.

The modulo %% returns the remainder of the division of the number to the left by the number on its right, for example 5 %% 3 is 2.

Instructions:

  • Type 5^2 in the editor to calculate 5 to the power 2.
  • Type 28 %% 6 to calculate 28 modulo 6.
  • Submit the answer and have a look at the R output in the console.
# no pec # An addition example 5 + 5 # A subtraction example 5 - 5 # A multiplication example 3 * 5 # A division example 10 / 2 # Exponentiation (5²) # Modulo (28 modulo 6) # An addition 5 + 5 # A subtraction 5 - 5 # A multiplication 3 * 5 # A division 10 / 2 # Exponentiation (5²) 5 ^ 2 # Modulo (28 modulo 6) 28 %% 6 msg = "Do not remove the other arithmetic examples!" test_output_contains("5^2", incorrect_msg = "The exponentiation example is not correct. Write `5 ^ 2` on a new line.") test_output_contains("28 %% 6", incorrect_msg = "There seems to be an issue with the modulo example. Write `28 %% 6` on a new line.") success_msg("Great! Head over to the next exercise.")

Another example of the modulo operator: 9 %% 2 equals 1.

Assign variables part 1

A basic concept in (statistical) programming is called a variable.

A variable allows you to store a value (e.g. 4) or an object (e.g. a function description) in R. You can then later use this variable's name to easily access the value or the object that is stored within this variable. You can assign a value 4 to a variable with the command my_var <- 4.

Instructions:

  • Complete the code in the editor such that it assigns the value 42 to the variable x.
  • Print x.
  • Submit the answer.
# no pec # Assign the value 42 to x # Print out the value of the variable x # Assign the value 42 to x x <- 42 # Print out the value of the variable x x test_object("x", undefined_msg = "Make sure to define a variable `x`.", incorrect_msg = "Make sure that you assign the correct value to `x`.") success_msg("Good job! Have you noticed that R does not print the value of a variable to the console when you did the assignment? `x <- 42` did not generate any output, because R assumes that you will be needing this variable in the future. Otherwise you wouldn't have stored the value in a variable in the first place, right? Proceed to the next exercise!")

Look at how the value 4 was assigned to my_variable in the exercise's assignment. Do the exact same thing in the editor, but now assign 42 to the variable x.

Assign variables part 2

Suppose you have a fruit basket with five apples and you want to store the number of apples in a variable with the name my_apples.

Instructions:

  • Type the following code in the editor: my_apples <- 5. This will assign the value 5 to my_apples.
  • Print my_apples.
  • Submit the answer.
# no pec # Assign the value 5 to the variable my_apples # Print out the value of the variable my_apples # Assign the value 5 to the variable my_apples my_apples <- 5 # Print out the value of the variable my_apples my_apples test_object("my_apples", undefined_msg = "Please make sure to define a variable `my_apples`.", incorrect_msg = "Make sure that you assign the correct value to `my_apples`.") test_output_contains("my_apples", incorrect_msg = "Have you explicitly told R to print out the `my_apples` variable to the console?") success_msg("Great! Continue to the next exercise!")

Remember that if you want to assign a number or an object to a variable in R, you can make use of the assignment operator <-. Alternatively, you can use =, but <- is widely preferred in the R community.

Assign variables part 3

Every tasty fruit basket needs oranges, so you decide to add six oranges. 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.

Instructions:

  • Assign to my_oranges the value 6.
  • Add the variables my_apples and my_oranges and have R simply print the result.
  • Assign the result of adding my_apples and my_oranges to a new variable my_fruit.
# no pec # 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.

Updated: