R Programming Language Certification Data Manipulation & Tidyverse in R 2 — Questions and Answers
Question 1: Which tidyr function converts data from wide format to long format?
- pivot_longer() (Correct answer)
- gather()
- melt()
- reshape()
Correct answer: pivot_longer()
pivot_longer() (tidyr) transforms multiple columns into key-value pairs, converting wide data to long/tidy format.
Question 2: Which dplyr join function keeps all rows from the left data frame and matching rows from the right?
- left_join() (Correct answer)
- inner_join()
- full_join()
- semi_join()
Correct answer: left_join()
left_join() retains all rows from the left table and fills NA for non-matching rows from the right table.
Question 3: Which R function reads a CSV file into a data frame using the readr package?
- read_csv() (Correct answer)
- read.csv()
- fread()
- import_csv()
Correct answer: read_csv()
readr's read_csv() reads delimited files faster than base R's read.csv() and returns a tibble with better default behavior.
Question 4: What is a tibble in R?
- A modern version of a data frame with better printing and stricter behavior (Correct answer)
- A list of vectors
- A matrix with labels
- A tidyverse plot object
Correct answer: A modern version of a data frame with better printing and stricter behavior
A tibble (tbl_df) is a tidyverse-enhanced data frame that prints cleanly, never converts strings to factors by default, and gives clearer error messages.
Question 5: Which dplyr function selects specific columns from a data frame by name or condition?
- select() (Correct answer)
- filter()
- pull()
- keep()
Correct answer: select()
select() chooses columns by name, position, or helper functions like starts_with(), ends_with(), and contains().
Question 6: Which tidyr function splits one column into multiple columns based on a delimiter?
- separate() (Correct answer)
- split_col()
- str_split_col()
- divide()
Correct answer: separate()
separate() splits a single character column into multiple columns using a specified separator character.
Which tidyr function converts data from wide format to long format?