R Programming Language Certification Data Manipulation & Transformation 2 — Questions and Answers
Question 1: Which tidyr function separates a single column into multiple columns based on a delimiter?
- unite()
- separate() (Correct answer)
- extract()
- split_col()
Correct answer: separate()
separate() splits a single character column into multiple columns using a specified separator character.
Question 2: Which dplyr function removes duplicate rows from a data frame?
- distinct() (Correct answer)
- unique_rows()
- dedupe()
- filter_unique()
Correct answer: distinct()
distinct() returns only unique rows from a data frame, optionally based on specified columns.
Question 3: What is the result of left_join(df1, df2, by='id') in dplyr?
- Only rows in df2
- Only matching rows from both
- All rows from df1, matched rows from df2 (Correct answer)
- All rows from both data frames
Correct answer: All rows from df1, matched rows from df2
left_join() retains all rows from the left data frame (df1) and fills in matched values from df2, leaving NAs for non-matches.
Question 4: Which function in base R applies a function to each element of a list and returns a list?
- sapply()
- lapply() (Correct answer)
- vapply()
- tapply()
Correct answer: lapply()
lapply() applies a function to each element of a list and always returns a list of the same length.
Question 5: In dplyr, which function is used to reorder rows based on column values?
- sort()
- order()
- arrange() (Correct answer)
- rank()
Correct answer: arrange()
arrange() reorders the rows of a data frame based on the values of one or more columns, defaulting to ascending order.
Question 6: Which function replaces NA values with a specified value in R?
- replace_na() (Correct answer)
- fill_na()
- impute()
- coalesce_na()
Correct answer: replace_na()
replace_na() from tidyr substitutes NA values in a vector or data frame columns with a user-specified replacement value.
Which tidyr function separates a single column into multiple columns based on a delimiter?