What is an object in R?
An object is anything created in R, they store data or results for later use.
What does the q() function do?
quits an R session. When used, R asks if you want to save the workspace image.
What is a function in R?
A function performs an action in R, such as calculating, transforming, or analyzing data.
What is the assignment operator in R?
<- assigns values to objects.
Example: x <- 2 stores the value 2 in the object x.
What is a script in RStudio?
A script is a text file containing R commands. You can save it as .R and run (source) it later.
What command shows the current working directory?
getwd()
What command sets a new working directory?
setwd()
How do you combine values into a vector?
Use c()
colors <- c( “red”, “blue”, “green”)
How do you create a sequence of numbers?
Use the : operator
1:5 1, 2, 3, 4, 5
How do you repeat a value or sequence in R?
Use the rep() function
rep(3, 4) 3 3 3 3
rep(1:3, 2) 1 2 3 1 2 3
How do you select an element from a vector?
Use square brackets []
x <- c (5, 10, 15)
x[2] 10
How do you select multiple elements from a vector?
Use c() inside brackets.
x <- c (5, 10, 15)
x [c(1,3)] 5 15
How do you exclude elements from a vector?
Use negative indices
x <- 5:10
x[-2] 5 7 8 9 10
What does vectorized arithmetic mean?
Operations apply element-by-element across a vector
x <- 1:4
x + 2 3 4 5 6
x * 2 2 4 6 8
How do you do element-wise operations on two vectors?
Operations match elements in the same position
a <- c(1,2,3)
b <- c(10,20,30)
a + b 11 22 33
How do you take a square root in R?
Use sqrt()