Multiple Choices: Data Frames
Questions
- What function is used to create a data frame in R?
- If
df <- data.frame(A = 1:3, B = c("X", "Y", "Z"))
, what doesdf$B
return?
- What is the output of
nrow(data.frame(x = 1:4, y = 5:8))
?
- Given
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
, how would you select the first row of the data frame?
- If
df <- data.frame(A = 1:5, B = c(NA, 2, NA, 4, 5))
, what willna.omit(df)
do?
- If
df <- data.frame(x = 1:5, y = c(10, 20, 30, 40, 50))
, what doesdf[ , "y"]
return?
- What does
str(df)
display for a data framedf
?
- Given
df <- data.frame(A = c(5, 3, 2, 8), B = c("A", "B", "C", "D"))
, what willdf[order(df$A), ]
return?
- Which of the following will add a new column
C
with valuesc(100, 200, 300, 400)
todf <- data.frame(A = 1:4, B = 5:8)
?
- If
df <- data.frame(Name = c("Alice", "Bob", "Carol"), Age = c(25, NA, 30))
, what doesis.na(df$Age)
return?
- Which function would you use to view the first few rows of a data frame
df
? ::: {.cell}
- Given
df <- data.frame(A = c(2, 4, 6), B = c("X", "Y", "Z"))
, what willdf[1:2, ]
return? ::: {.cell}
:::
- If
df <- data.frame(A = 1:3, B = 4:6)
, which command will rename the columns toX
andY
? ::: {.cell}
:::
- Which function gives a summary of each column in a data frame
df
, including min, max, mean, and quartiles? ::: {.cell}
:::
- What does
ncol(df)
return for a data framedf
? ::: {.cell}
:::
- Given
df1 <- data.frame(A = 1:3, B = 4:6)
anddf2 <- data.frame(A = 7:9, B = 10:12)
, what doesrbind(df1, df2)
do? ::: {.cell}
:::
- How would you select only the columns
B
andC
from a data framedf
? ::: {.cell}
:::
- If
df <- data.frame(A = 1:5, B = 6:10)
, which of the following commands will add a new rowc(11, 12)
todf
? ::: {.cell}
:::
- Which function checks if an object
df
is a data frame? ::: {.cell}
:::
- If
df <- data.frame(A = c("apple", "banana", "apple"), B = c(5, 3, 8))
, which command will count the number of occurrences of each unique value in columnA
? ::: {.cell}
:::
- Given
df <- data.frame(A = c(3, 1, 2), B = c(9, 6, 4))
, what doesdf[order(df$A), ]
return? ::: {.cell}
:::
- If
df <- data.frame(X = c(1, NA, 3, 4), Y = c("A", "B", NA, "D"))
, which function would return a logical vector indicating missing values inX
? ::: {.cell}
:::
- Which function can you use to combine two data frames
df1
anddf2
with the same columns, addingdf2
as rows todf1
? ::: {.cell}
:::
- If
df <- data.frame(A = 1:5, B = letters[1:5])
, what willdf[ , 1]
return? ::: {.cell}
:::
- Given
df <- data.frame(A = c(1, 2, 3, 4), B = c(10, 20, 30, 40))
, which command will select rows where columnB
is greater than 20? ::: {.cell}
:::
- Which function will display the names of columns in a data frame
df
? ::: {.cell}
:::
- If
df <- data.frame(A = 1:5, B = 6:10)
, what doesdf[-1, ]
return? ::: {.cell}
:::
- To find the unique values in a column
A
of a data framedf
, which command would you use? ::: {.cell}
:::
- If
df <- data.frame(A = 1:3, B = 4:6)
, what doesdim(df)
return? ::: {.cell}
:::
- Given
df <- data.frame(A = c(1, 2, 3), B = c("X", "Y", "Z"))
, how would you convert columnA
to a character type? ::: {.cell}
:::
:::