Day 1A Practice

Question 1.

Read what the user wanted to do and look at the command they sent to R. Fix the errors in the commands and calculate the answers.

  1. I want to add 20 and 0.2 and divide that sum by 4.1. But I get an error when I try:
    [20 + .2] \ 4.1
  2. I want to multiply 6 by the sum of 2 and 1,300. But I get an error when I try:
    6 x (2 + 1,300)

Click here for the answer key

Answer (a)

Change the brackets [] to parentheses () because parentheses () are the order of operations operator in R. Also, change the backslash \ to a forward slash / because forward slash / is the division operator in R.

(20 + .2) / 4.1
#> [1] 4.926829

Answer (b)

Change the x to an asterisk * because asterisk * is the multiplication operator in R. Also, remove the comma , from 1,300 because R doesn’t use commas to delimit digits in large numbers.

6 * (2 + 1300)
#> [1] 7812

Question 2.

Which of the following commands do you think will create an error in R? Why?

  1. score@T1 <- 3.2
  2. score_at_T1 <- 3.2
  3. score at T1 <- 3.2
  4. 1_score <- 3.2
  5. ScoreAtTime1 <- 3.2

Make your best guesses just by looking at the commands and then check your guesses by running the commands in R.

Click here for the answer key

The following commands will create an error in R: a (@ is not allowed in an object name), c (spaces are not allowed in an object name), and d (the first character in an object name cannot be a number).

Resources

Fun Stuff

Literal Genie

What it feels like to talk to R sometimes…