1/252
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What does %% do in R?
It returns the remainder of division (modulo operator).
What does %/% do in R?
It returns the integer quotient of a division.
What function sets the working directory?
setwd()
What function shows your current working directory?
getwd()
How do you create a vector with the values 1, 2, and 3?
c(1, 2, 3)
c(1, 2, 3)
length()
What function gives you the type of storage for a vector?
mode()
How do you repeat a vector [1, 2] three times?
rep(c(1, 2), times = 3)
What does sample() do?
Randomly samples elements from a vector.
What does o[x] do if x is numeric?
Returns elements at positions specified in x.
What does o[-x] do?
Returns all elements except the ones at positions in x.
How do you coerce a vector to numeric?
as.numeric(x)
What does logical() create?
An empty logical vector.
What function creates a matrix in R?
matrix()
How do you access the second row of a matrix A?
A[2, ]
What does summary() return for a numeric vector?
Min, 1st Qu., Median, Mean, 3rd Qu., Max
What function converts characters to numbers?
as.numeric()
What function gets the sine of a value?
sin()
What does rev() do?
Reverses a vector.
What is NaN?
"Not a Number" – result of invalid math like 0/0.
How do you check if a value is NA?
is.na(x)
How do you test if a number is infinite?
is.infinite(x)
What does ?mean do?
Opens help for the mean() function.
What’s the difference between ? and ???
? searches by function name; ?? does a broader search (keywords, topics).
What does which() do?
Returns indices of TRUE values in a logical vector.
How do you check if all elements are TRUE?
all(x)
How do you check if two objects are nearly equal?
all.equal(x, y)
What function creates a data frame?
data.frame()
How do you extract a column from a data frame called df named "age"?
df$age
What function returns the number of rows?
nrow(df)
What does str(df) show?
The structure of the data frame, including types and samples of data.
What does ggplot() do?
Initializes a ggplot object.
What function sets the mapping of x/y to variables?
aes()
What function saves a plot to file?
ggsave()
What are the three main loop/control structures?
for(), if()/else, while()
What function exits the current iteration in a loop early?
next
What function exits the entire loop?
break
How do you define a function?
function(x) { … }
What do |> and %>% do?
Pipe the result of one expression into the next.
What does ifelse() do?
Vectorized if-else conditional; returns values based on a logical test.
What does unlist() do?
Flattens a list to an atomic vector.
How do you check if a value is in a vector?
x %in% y
Write an R function isleapyear() that takes a single year as input and returns TRUE if it's a leap year, and FALSE otherwise. A year is a leap year if it is divisible by 4, but not divisible by 100 unless also divisible by 400.
is_leap_year <- function(year) {
(year %% 4 == 0) & ((year %% 100 != 0) | (year %% 400 == 0))
}
What is the output of isleapyear(3)?
[1] FALSE
Explanation:
3 is not divisible by 4, so it is not a leap year.
What is the output of isleapyear(8)?
[1] TRUE
Explanation:
8 is divisible by 4, and not divisible by 100, so it is a leap year.
What is the output of isleapyear(200)?
[1] FALSE
Explanation:
200 is divisible by 100 but not by 400, so it is not a leap year.
What is the output of isleapyear(800)?
[1] TRUE
Explanation:
800 is divisible by 4, by 100, and by 400. It is a leap year.
Given the nested list big_list, how is the data structured?
The list big_list has four top-level elements named:
"None" – an empty list
"One" – a list containing a single number: 1
"Star Wars" – a list with:
A sublist called "characters" (with nested lists for Obi Wan, Luke Skywalker, and R2D2)
A sublist called "movies" (a list of 3 movie titles)
"Four" – a character vector: "Even", "more", "entries"
So big_list looks like this at the top level:
list(
"None" = list(),
"One" = list(1),
"Star Wars" = list( # <-- third element
characters = list( # <-- first element inside
"Obi Wan" = list(species = "human", movies = 1:6),
"Luke Skywalker" = list(species = "human", movies = 4:6),
"R2D2" = list(species = "robot", movies = 1:6)
),
movies = list(...) # <-- second element inside
),
"Four" = c("Even", "more", "entries")
)
How do you extract the character "robot" from big_list using only numeric indices?
big_list[[3]][[1]][[3]][[1]]
Correct Output:
[1] "robot"
What does each step in big_list[[3]][[1]][[3]][[1]] do?
Write an R function imperial_to_metric() that takes two non-negative scalar arguments feet and inches and returns the total height in centimeters. Assumes: 12 inches = 1 foot, 1 inch = 2.54 cm. Include default values for both inputs.
imperial_to_metric <- function(feet = 0, inches = 0) {
total_inches <- feet * 12 + inches
c(cm = total_inches * 2.54)
}
Explanation: Converts total height to inches first, then multiplies by 2.54 to get centimeters. Returns a named vector with name \"cm\"
What is the output of the following call?
imperial_to_metric(feet = 1)
cm 30.48
Explanation: 1 foot = 12 inches → 12 * 2.54 = 30.48 cm
What is the output of the following call?
imperial_to_metric(inches = 2)
cm 5.08
Explanation: 2 inches * 2.54 = 5.08 cm
What is the output of the following call?
```r
imperialtometric(feet = 1, inches = 2)
cm 35.56
Explanation: 1 foot = 12 inches → 12 + 2 = 14 inches, then 14 * 2.54 = 35.56 cm
Write an R function metrictoimperial() that:
Takes a single non-negative scalar input cm (centimeters)
Converts the value into feet and inches
Returns a named numeric vector of length 2: c(feet = …, inches = …)
Uses default input value cm = 0
metric_to_imperial <- function(cm = 0) {
total_inches <- cm / 2.54
feet <- total_inches %/% 12
inches <- total_inches %% 12
c(feet = feet, inches = inches)
}
Explanation:
cm / 2.54 converts centimeters to inches
total_inches %/% 12 computes full feet
total_inches %% 12 gives the remaining inches
What is the output of the following?
metrictoimperial(30.48)
feet inches
1 0
Explanation:
30.48 cm is exactly 12 inches (1 foot). This function avoids outputting “0 feet and 12 inches,” which would be incorrect formatting.
What is the output of the following?
metrictoimperial(5.08)
feet inches
0 2
Explanation:
5.08 cm is exactly 2 inches. Correctly returns no feet and 2 inches.
What is the output of the following?
metrictoimperial(35.56)
feet inches
1 2
Explanation:
35.56 cm = 14 inches → 1 foot + 2 inches. This output avoids “26 inches” and formats the result correctly.
What does %% do in R?
It returns the remainder of division (modulo operator).
What does %/% do in R?
It returns the integer quotient of a division.
What function sets the working directory?
setwd()
What function shows your current working directory?
getwd()
How do you create a vector with the values 1, 2, and 3?
c(1, 2, 3)
What function returns the number of elements in a vector?
length()
What function gives you the type of storage for a vector?
mode()
How do you repeat a vector [1, 2] three times?
rep(c(1, 2), times = 3)
What does sample() do?
Randomly samples elements from a vector.
What does o[x] do if x is numeric?
Returns elements at positions specified in x.
What does o[-x] do?
Returns all elements except the ones at positions in x.
How do you coerce a vector to numeric?
as.numeric(x)
What does logical() create?
An empty logical vector.
What function creates a matrix in R?
matrix()
How do you access the second row of a matrix A?
A[2, ]
What does IQR() compute?
The interquartile range.
How do you get the cumulative sum of a vector?
cumsum(x)
What does summary() return for a numeric vector?
Min, 1st Qu., Median, Mean, 3rd Qu., Max
What function converts characters to numbers?
as.numeric()
What function gets the sine of a value?
sin()
What does rev() do?
Reverses a vector.
What is NaN?
"Not a Number" – result of invalid math like 0/0.
How do you check if a value is NA?
is.na(x)
How do you test if a number is infinite?
is.infinite(x)
What does ?mean do?
Opens help for the mean() function.
What’s the difference between ? and ???
? searches by function name; ?? does a broader search (keywords, topics).
What does which() do?
Returns indices of TRUE values in a logical vector.
How do you check if all elements are TRUE?
all(x)
How do you check if two objects are nearly equal?
all.equal(x, y)
What function creates a data frame?
data.frame()
How do you extract a column from a data frame called df named "age"?
df$age
What function returns the number of rows?
nrow(df)
What does str(df) show?
The structure of the data frame, including types and samples of data.
What does ggplot() do?
Initializes a ggplot object.
What function sets the mapping of x/y to variables?
aes()
What function saves a plot to file?
ggsave()
What are the three main loop/control structures?
for(), if()/else, while()
What function exits the current iteration in a loop early?
next
What function exits the entire loop?
break
How do you define a function?
function(x) { … }
What do |> and %>% do?
Pipe the result of one expression into the next.
What does ifelse() do?
Vectorized if-else conditional; returns values based on a logical test.