Data Manipulation Quiz - Languages R
Free practice quiz: Data Manipulation (Languages R). 40 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 40 questions freeSample questions
Which R function is primarily used for reshaping data from a wide format to a long format?
- pivot_wider()
- pivot_longer()
- gather()
- spread()
Show answer
Answer: B. pivot_longer()
The `pivot_longer()` function from the `tidyr` package is specifically designed to take columns that represent values and stack them into rows, effectively converting data from a wide format to a long format. `pivot_wider()` does the opposite. `gather()` and `spread()` are older functions with similar purposes but are generally superseded by `pivot_longer()` and `pivot_wider()` respectively.
When dealing with data that has multiple columns representing different time points for the same variable (e.g., 'value_2020', 'value_2021'), which `tidyr` function is typically used to transform this into a format with columns like 'year' and 'value'?
- pivot_wider()
- pivot_longer()
- unite()
- separate()
Show answer
Answer: B. pivot_longer()
This scenario describes data in a 'wide' format where years are column names. To transform this into a 'long' format with a 'year' column and a 'value' column, `pivot_longer()` is the appropriate function. It takes a set of columns (e.g., 'value_2020', 'value_2021') and 'pivots' them into rows, creating new columns to store the original column names (years) and their corresponding values.
Which `dplyr` function is most suitable for counting the occurrences of each unique combination of values in two or more columns?
- count()
- group_by() %>% summarize(n = n())
- summarize_at()
- table()
Show answer
Answer: A. count()
The `count()` function in `dplyr` is a convenient shorthand specifically designed for counting unique combinations of specified variables. It's equivalent to `group_by()` followed by `summarize(n = n())`. While `table()` can do this, `count()` is the idiomatic `dplyr` way. `summarize_at()` is for applying summaries to multiple columns at once but doesn't directly count combinations.
What is the primary advantage of using `tidyr`'s `pivot_longer()` over base R's `reshape()` for transforming wide data to long data?
- `pivot_longer()` is generally slower.
- `pivot_longer()` is more flexible and often easier to read, especially with many 'wide' columns.
- `pivot_longer()` can only handle numeric data.
- `pivot_longer()` requires all columns to be of the same data type.
Show answer
Answer: B. `pivot_longer()` is more flexible and often easier to read, especially with many 'wide' columns.
The `tidyr` package, particularly `pivot_longer()`, was designed to simplify and make more intuitive the process of reshaping data compared to base R's `reshape()`. `pivot_longer()`'s syntax for specifying which columns to pivot (e.g., using ranges or `starts_with()`) and its clear `names_to` and `values_to` arguments often make it easier to use and understand, especially for complex datasets. It's also generally more performant for common tasks.
Consider the `dplyr` pipe: `df %>% select(col1, col2) %>% filter(col1 > 10)`. What is the first operation performed?
- Filtering the data frame `df` where `col1` is greater than 10.
- Selecting columns `col1` and `col2` from `df`.
- Creating a new data frame with only `col1` and `col2`.
- An error occurs because `filter` is before `select`.
Show answer
Answer: B. Selecting columns `col1` and `col2` from `df`.
The pipe operator (`%>%`) passes the output of the left-hand side to the first argument of the right-hand side function. In `df %>% select(col1, col2)`, `df` is passed to `select`, so `select(df, col1, col2)` is executed first. The result of this selection is then passed to `filter(col1 > 10)`.