Front
Back
What does c() do in R?
Combines values into a vector (atomic).
Code:
x <- c(1, 2, 3)
y <- c(“a”, “b”)
x; y
Notes:
Coerces to a common type if mixed (e.g., numeric + character -> character).
How do you create sequences in R with seq()?
Generates sequences by range or length.
Code:
seq(1, 10, by = 2) # 1 3 5 7 9
seq(from = 0, to = 1, length.out = 5)
Notes:
Prefer explicit named arguments to avoid confusion.
What does rep() do?
Repeats elements of a vector.
Code:
rep(1:3, times = 2) # 1 2 3 1 2 3
rep(1:3, each = 2) # 1 1 2 2 3 3
Notes:times repeats the whole vector; each repeats each element.
How do you inspect an object quickly? (str, class, typeof)
Use str() for structure, class() for S3 class, typeof() for internal type.
Code:
str(iris); class(iris); typeof(iris)
How do you subset vectors in base R?
Use [] with positions, names, or logicals.
Code:
x <- c(a = 10, b = 20, c = 30)
x[1] # by position
x[c(“a”,”c”)] # by name
x[x > 15] # by logical
Notes:
Negative indices drop elements: x[-1]. 0 returns empty.
How do you subset lists ([], [[]], $) and why does it matter?
[] returns a sublist; [[ ]] extracts the element; $ extracts by name.
Code:
lst <- list(name = “Ada”, scores = 1:3)
lst[1] # list of length 1
lst[[1]] # ‘Ada’
lst$scores # 1:3
Notes:
Use [[ ]] when you need the element itself (e.g., a vector) not a list.
What does data.frame() and tibble::tibble() do?
Create tabular data structures. tibble() is stricter, prints nicely.
Code:
df <- data.frame(x = 1:3, y = letters[1:3])
tibble::tibble(x = 1:3, y = letters[1:3])
Notes:
Tibbles do not convert strings to factors by default.
How do you merge data frames in base R?
Use merge(x, y, by = ‘key’) for equi-joins.
Code:
merge(x = data.frame(id=1:3, a=11:13),
y = data.frame(id=2:4, b=21:23),
by = ‘id’, all.x = TRUE) # left join
Notes:all.x, all.y control left/right; all=TRUE for full join.
What are apply/lapply/sapply/vapply in base R?
Vectorised iteration helpers over arrays/lists.
Code:
m <- matrix(1:9, nrow=3)
apply(m, 2, mean) # by columns
lapply(1:3, function(i) i^2) # list output
sapply(1:3, sqrt) # simplified
vapply(1:3, sqrt, numeric(1))# type safe
Notes:
Prefer vapply() in packaged code for type safety.
What does tapply() do?
Applies a function by groups to a vector, returning an array.
Code:
x <- c(10,20,30,40,50)
g <- factor(c(‘A’,’A’,’B’,’B’,’B’))
tapply(x, g, mean)
Notes:
Similar to grouped summarise; INDEX can be a list of factors.
How do you handle missing values in base R?
Check with is.na()/anyNA(); remove with na.omit(); compute with na.rm=TRUE.
Code:
x <- c(1, NA, 3)
mean(x, na.rm = TRUE)
x_no_na <- na.omit(x)
anyNA(x)
How do you bin numeric data into categories? (cut)
Use cut() to discretise numeric variables into intervals.
Code:
x <- c(5, 12, 18, 30)
cut(x, breaks=c(0,10,20,Inf), labels=c(‘low’,’mid’,’high’), right=FALSE)
How do you compute common summaries in base R?
Use mean, median, sd, var, quantile, IQR with na.rm when appropriate.
Code:
summary(mtcars$mpg)
IQR(mtcars$mpg)
quantile(mtcars$mpg, probs=c(0.25,0.5,0.75), na.rm=TRUE)
What is the difference between matrix and data frame?
Matrix is homogeneous (one type), data frame/tibble is column-wise heterogeneous.
Code:
m <- matrix(1:6, nrow=2)
df <- data.frame(a=1:3, b=letters[1:3])
class(m); class(df)
How do you write and use your own function in R?
Define with function(…){…} and return a value with return() (optional).
Code:
square <- function(x) x^2
square(4)
Notes:
Use stop() for errors, warning() for warnings, message() for messages.