Below are some interactive exercises to test your R knowledge. Just type your answers into the appropriate box, and hit “Run”. In some cases, there’ll be a hint or solution for you to use, but usually there’ll be more than one way to do it!
Write the R code required to add 2 plus 2:
2 + 2
Write the R code required to divide 10 by 5:
10 / 5
Create a variable called variable_1 and assign it the value 10. Then output it to the console.
variable_1 <- 10
variable_1
Create two numeric variables (called whatever you like), and multiply them.
variable_1 <- 10
variable_2 <- 5
variable_1 * variable_2
Create a named vector with 3 values. (Hint: That doesn’t mean create a variable, it means that the values in the vector should be named)
c("value_1" = 10, "value_2" = 20, "value_3" = 30)
Create a list with two values; one that’s a vector and another that’s a dataframe.
list(
"vector" = c(1,2,3,4),
"dataframe" = data.frame(col_1 = "hello",
col_2 = "world")
)
Change the following code to only return the first 10 values from the vector.
vector_1 <- c(56,3,324,6,7,86,5,4,17,54,8468,45,345,234,7,56,8,4,32,234,5,764,658,6,3463)
vector_1
vector_1 <- c(56,3,324,6,7,86,5,4,17,54,8468,45,345,234,7,56,8,4,32,234,5,764,658,6,3463)
vector_1[1:10]