01_Base_R Flashcards

(16 cards)

1
Q

Front

A

Back

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does c() do in R?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create sequences in R with seq()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does rep() do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you inspect an object quickly? (str, class, typeof)

A

Use str() for structure, class() for S3 class, typeof() for internal type.

Code:
str(iris); class(iris); typeof(iris)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you subset vectors in base R?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you subset lists ([], [[]], $) and why does it matter?

A

[] 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does data.frame() and tibble::tibble() do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you merge data frames in base R?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are apply/lapply/sapply/vapply in base R?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does tapply() do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you handle missing values in base R?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you bin numeric data into categories? (cut)

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you compute common summaries in base R?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between matrix and data frame?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you write and use your own function in R?

A

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.