20 + .2) / 4.1
(#> [1] 4.926829
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.
- 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
- 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.Answer (b)
Change the
x
to an asterisk*
because asterisk*
is the multiplication operator in R. Also, remove the comma,
from1,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?
score@T1 <- 3.2
score_at_T1 <- 3.2
score at T1 <- 3.2
1_score <- 3.2
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
R4DS Chapter 8: Read more about projects
R4DS Chapter 4: Read about basic programming
R4DS Chapter 27: Read more about RMarkdown
Fun Stuff
Literal Genie
What it feels like to talk to R sometimes…