DataCamp Exercise

It is time for DATA CAMP 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 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.
DataCamp Light | Standalone example

Apples and oranges

Common knowledge tells you not to add apples and oranges.
The my_apples and my_oranges variables both contained a number in the previous exercise. The + operator works with numeric variables in R. If you really tried to add "apples" and "oranges", and assigned a text value to the variable my_oranges (see the editor), you would be trying to assign the addition of a numeric and a character variable to the variable my_fruit. This is not possible.

Instructions:

  • Submit the answer and read the error message. Make sure to understand why this did not work.
  • Adjust the code so that R knows you have 6 oranges and thus a fruit basket with 11 pieces of fruit.
# no pec # Assign a value to the variable my_apples my_apples <- 5 # Fix the assignment of my_oranges my_oranges <- "six" # Create the variable my_fruit and print it out my_fruit <- my_apples + my_oranges my_fruit # Assign a value to the variable my_apples my_apples <- 5 # Fix the assignment of my_oranges my_oranges <- 6 # Create the variable my_fruit and print it out my_fruit <- my_apples + my_oranges my_fruit test_error(incorrect_msg = "You can do this by setting the `my_oranges` variable to a numeric value, not a string!") test_object("my_apples", incorrect_msg = "Make sure that `my_apples` still contains `5`.") test_object("my_oranges", incorrect_msg = "Make sure that `my_oranges` is equal to `6`.") test_object("my_fruit", incorrect_msg = "The value of `my_fruit` is not correct. It should be 11, the sum of `my_apples` and `my_oranges`.") test_output_contains("my_fruit", incorrect_msg = "Don't remove the line that prints out `my_fruit`.") success_msg("Awesome, keep up the good work! Continue to the next exercise.")

You have to assign the numeric value 6 to the my_oranges variable instead of the character value "six". Note how the quotation marks are used to indicate that "six" is a character.

Basic data types in R

R works with numerous data types. Some of the most basic types to get started are:

  • Decimal values like 4.5 are called numerics.
  • Whole numbers like 4 are called integers. Integers are also numerics.
  • The Boolean values TRUE or FALSE are called logical.
  • Text (or string) values are called characters.
    Note how the quotation marks in the editor indicate that "Apple" is a string just like "five".

Instructions:

Change the value of the following variables:
  • my_numeric to 42.
  • my_character to "universe". Note that the quotation marks indicate that "universe" is a character.
  • my_logical to FALSE.

Note that R is case sensitive!

# no pec # Change my_numeric to be 42 my_numeric <- 42.5 # Change my_character to be "universe" my_character <- "some text" # Change my_logical to be FALSE my_logical <- TRUE # Change my_numeric to be 42 my_numeric <- 42 # Change my_character to be "universe" my_character <- "universe" # Change my_logical to be FALSE my_logical <- FALSE test_object("my_numeric", incorrect_msg = "Have you correctly changed the declaration of `my_numeric` so it contains the value 42?") test_object("my_character", incorrect_msg = "Have you correctly changed `my_character` to `\"universe\"`? Don't forget the quotes!") test_object("my_logical", incorrect_msg = "Have you correctly changed `my_logical` to `FALSE`? All letters of `FALSE` should be capitalized!") success_msg("Great work! Continue to the next exercise.")

Replace the values in the editor with the values that are provided in the exercise. For example: my_numeric <- 42 assigns the value 42 to the variable my_numeric.

What's that data type?

Do you remember that when you added 5 + "six", you got an error due to a mismatch in data types? You can avoid such situations by checking the data type of a variable beforehand. You can do this with the class() function, as the code in the editor shows.

Instructions:

  • Complete the code in the editor and also print out the classes of my_character and my_logical.
# no pec # Declare variables of different types my_numeric <- 42 my_character <- "universe" my_logical <- FALSE # Check class of my_numeric class(my_numeric) # Check class of my_character # Check class of my_logical # Declare variables of different types: my_numeric <- 42 my_character <- "universe" my_logical <- FALSE # Check class of my_numeric class(my_numeric) # Check class of my_character class(my_character) # Check class of my_logical class(my_logical) msg <- "Do not change the declaration of the variables!" lapply(c("my_numeric", "my_character", "my_logical"), test_object, undefined_msg = msg, incorrect_msg = msg) patt <- "Have you included `class(%1$s)` to print out the data type of `%1$s`?" test_output_contains("class(my_numeric)", incorrect_msg = "Do not remove the code that prints out the type of `my_numeric`.") test_output_contains("class(my_character)", incorrect_msg = sprintf(patt, "my_character")) test_output_contains("class(my_logical)", incorrect_msg = sprintf(patt, "my_logical")) success_msg("Congratulations! This was the last exercise for this chapter. Head over to the next chapter to get immersed in the world of vectors!")

The code that prints the data type of my_numeric is already included; do a similar things for my_character and my_logical.

Congratulations! This was the last exercise for this chapter.

Updated: