Consider this R code that uses a tidy data frame. What will be the output of the summary call?
library(dplyr) data <- tibble( id = c(1, 1, 2, 2), variable = c("height", "weight", "height", "weight"), value = c(170, 65, 180, 75) ) data_wide <- data %>% pivot_wider(names_from = variable, values_from = value) summary(data_wide)
Think about how pivot_wider reshapes data from long to wide format, making columns for each variable.
The pivot_wider function converts the tidy long data into a wide format where each variable becomes a column. The summary then shows statistics for id, height, and weight. Option C correctly reflects this output.
Which of the following best explains why tidy data makes analysis easier?
Think about how data organized in rows and columns helps tools work better.
Tidy data means each variable is a column and each observation is a row. This structure fits well with most analysis tools and makes it easy to apply functions and create plots. The other options describe incorrect or unrelated ideas.
Given this untidy data frame, what will be the output of the pivot_longer call?
library(tidyr) data <- data.frame( id = 1:2, height = c(170, 180), weight = c(65, 75) ) data_long <- pivot_longer(data, cols = c(height, weight), names_to = "variable", values_to = "value") print(data_long)
Remember that pivot_longer turns columns into rows.
The pivot_longer function converts the wide format data into a long format where 'height' and 'weight' become values in a 'variable' column, with their corresponding values in 'value'. Option B shows this correctly.
What error will this code produce and why?
library(tidyr) data <- data.frame( id = 1:2, height = c(170, 180), weight = c(65, 75) ) data_long <- pivot_longer(data, cols = c(height, weight), names_to = "variable")
Check the required arguments for pivot_longer.
The pivot_longer function requires the values_to argument to specify the name of the column for values. Omitting it causes an error about a missing argument. Option D correctly describes this error.
Given this untidy data frame, how many rows will the tidy data have after applying pivot_longer to columns height and weight?
library(tidyr) data <- data.frame( id = 1:3, height = c(170, 180, 175), weight = c(65, 75, 70) ) data_long <- pivot_longer(data, cols = c(height, weight), names_to = "variable", values_to = "value")
Each original row will produce multiple rows after pivoting columns into rows.
There are 3 rows and 2 columns being pivoted. Each row produces 2 rows in the long format, so total rows = 3 * 2 = 6. Option A is correct.