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.
This Languages R practice quiz has 40 multiple-choice questions on Data Manipulation, and 40 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 40 questions, freeSample questions
Which R function is primarily used for reshaping data from a wide format to a long format?
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'?
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?
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?
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?
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)`.
More questions in this quiz
Answers and explanations are in the full quiz.
- When performing a left join between two data frames `df1` and `df2` on a common column `key`, what is the expected outcome for rows in `df1` that do not have a matching `key` in `df2`?
- Consider a data frame `df` with columns `id`, `category`, and `value`. Which dplyr `filter()` expression would select all rows where `category` is either 'A' or 'B' and `value` is greater than 10?
- What is the purpose of the `arrange()` function in the `dplyr` package?
- In `tidyr`, what does the `pivot_wider()` function do?
- Which of the following is NOT a common data wrangling task in R?
- When using `group_by()` and `summarize()` in `dplyr`, what is the primary role of `group_by()`?
- What is the output type of `unique(c(1, 2, 2, 3, 1, 4))` in R?
- What does the `na.rm = TRUE` argument typically do when used with aggregation functions in R (e.g., `mean()`, `sum()`)?
- Consider two data frames `sales` and `products`. If you want to combine them so that each row in `sales` has the corresponding product information from `products` based on `product_id`, which type of join would you typically use?
- What is the primary use of the `bind_rows()` function in `dplyr`?
- When data is in a 'long' format with columns like `variable` and `value`, how would you use `tidyr` to transform it into a 'wide' format where `variable` values become new column names and `value` are the corresponding cell entries?
- Which `dplyr` verb is used for selecting specific columns from a data frame?
- What does the `%>%` operator (pipe) in R, often used with `dplyr`, facilitate?
- When using `group_by()` followed by `summarize()` in `dplyr`, what is the role of `n()` within the `summarize()` function?
- Which `dplyr` verb is used to transform the data, typically by creating new variables or modifying existing ones, often involving calculations based on other columns?
- Consider a scenario where you have a data frame `df` with columns 'ID', 'Date', and 'Amount'. You want to calculate the total 'Amount' for each 'ID'. Which `dplyr` code would achieve this efficiently?
- What is the role of the `summarize()` function in `dplyr`?
- Which `tidyr` function is used to split a single column into multiple columns based on a delimiter?
- When merging two data frames `df1` and `df2` using `dplyr::left_join(df1, df2, by = 'ID')`, what happens to rows in `df2` that do not have a matching 'ID' in `df1`?
- In `dplyr`, what does the `rename()` function do?
- Which function is commonly used to calculate the percentage of missing values (NA) for each column in a data frame?
- Which `dplyr` function is specifically designed to combine multiple data frames that have the same columns by stacking them vertically?
- If you have a data frame `sales` with columns `(product_id, quantity, price)` and you want to create a new column `total_cost` which is `quantity * price`, which `dplyr` verb would you use?
- What is the output of `x <- c('apple', 'banana', 'apple'); table(x)`?
- Which `tidyr` function would you use to convert a data frame from a wide format (e.g., columns `var_2020`, `var_2021`) to a long format (columns `year`, `var`)?
- What is the primary function of `filter()` in the `dplyr` package?
- If you want to combine `df1` and `df2` such that all rows from both are kept, and matching rows are merged, while non-matching rows have `NA`s for the columns of the other data frame, which join would you use?
- Which `dplyr` function is used to arrange rows in ascending order of a specified column?
- Which `stringr` function is used to split a string into multiple strings based on a pattern?
- What is the purpose of the `join` family of functions in `dplyr`?
- When using `pivot_longer()`, what do the `names_to` and `values_to` arguments represent?
- Which of the following is NOT a valid `dplyr` join function?
- Which `tidyr` function would you use to transform a data frame where each row represents an observation and columns are variables, into a format where some columns are converted into rows?
- What does `df %>% filter(!is.na(column_name))` do?
- When using `dplyr::left_join(df1, df2, by = 'ID')`, what is the guarantee about the rows that will be present in the final output data frame?