R Programming Language Certification Data Manipulation & Transformation 1 — Questions and Answers
Question 1: Which dplyr function is used to select specific columns from a data frame?
- select() (Correct answer)
- filter()
- mutate()
- arrange()
Correct answer: select()
The select() function in dplyr is used to choose specific columns from a data frame by name or index.
Question 2: Which function from tidyr converts a wide-format data frame to long format?
- spread()
- pivot_longer() (Correct answer)
- pivot_wider()
- gather_cols()
Correct answer: pivot_longer()
pivot_longer() from tidyr reshapes data from wide to long format by stacking multiple columns into key-value pairs.
Question 3: What does the dplyr function group_by() do?
- Sorts rows by a column
- Removes duplicate rows
- Groups rows for subsequent summarise operations (Correct answer)
- Merges two data frames
Correct answer: Groups rows for subsequent summarise operations
group_by() defines groups within a data frame so that subsequent operations like summarise() are applied to each group separately.
Question 4: Which operator is used to chain dplyr operations in R?
- ->
- +
- %>% (Correct answer)
- ::
Correct answer: %>%
The pipe operator %>% (from magrittr/dplyr) passes the left-hand result as the first argument to the right-hand function.
Question 5: Which function would you use to add a new computed column to a data frame in dplyr?
- filter()
- mutate() (Correct answer)
- summarise()
- rename()
Correct answer: mutate()
mutate() adds new columns to a data frame, often computed from existing columns, without removing any existing columns.
Question 6: What does the na.omit() function do in R?
- Replaces NA values with zero
- Removes all rows containing NA values (Correct answer)
- Counts NA values in each column
- Converts NA to empty string
Correct answer: Removes all rows containing NA values
na.omit() removes all rows from a data frame (or elements from a vector) that contain at least one NA value.
Which dplyr function is used to select specific columns from a data frame?