Challenge - 5 Problems
Pivot Longer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this pivot_longer operation?
Given the data frame
df below, what will be the output after applying the pivot_longer function as shown?R Programming
library(tidyr) df <- data.frame( id = 1:2, math = c(90, 80), science = c(85, 95) ) result <- pivot_longer(df, cols = c(math, science), names_to = "subject", values_to = "score") print(result)
Attempts:
2 left
💡 Hint
Remember that pivot_longer stacks the selected columns in order, keeping the original row order.
✗ Incorrect
pivot_longer takes the columns math and science and stacks them under a new column 'subject'. The values go into 'score'. The order preserves the original row order and the order of columns selected.
🧠 Conceptual
intermediate1:30remaining
Which statement best describes the purpose of pivot_longer?
What does the
pivot_longer function do in data manipulation?Attempts:
2 left
💡 Hint
Think about how columns are transformed into rows.
✗ Incorrect
pivot_longer gathers multiple columns into two columns: one for the column names (keys) and one for the values, changing wide data into long data.🔧 Debug
advanced2:00remaining
What error does this pivot_longer code produce?
Consider the code below. What error will it raise when run?
R Programming
library(tidyr) df <- data.frame( id = 1:2, math = c(90, 80), science = c(85, 95) ) result <- pivot_longer(df, cols = math science, names_to = "subject", values_to = "score")
Attempts:
2 left
💡 Hint
Check the syntax for specifying multiple columns in the cols argument.
✗ Incorrect
The cols argument requires columns to be separated by commas or use c(). Writing 'math science' without a comma or c() causes a syntax error.
❓ Predict Output
advanced2:00remaining
What is the output of pivot_longer with regex selection?
Given the data frame
df below, what will be the output of the pivot_longer call that selects columns starting with 'score_'?R Programming
library(tidyr) df <- data.frame( id = 1:2, score_math = c(90, 80), score_science = c(85, 95), age = c(15, 16) ) result <- pivot_longer(df, cols = starts_with("score_"), names_to = "subject", values_to = "score") print(result)
Attempts:
2 left
💡 Hint
The order of rows follows the original row order and the order of selected columns.
✗ Incorrect
pivot_longer stacks columns selected by starts_with("score_") in the order they appear, preserving row order.
🚀 Application
expert1:30remaining
How many rows are in the result after pivot_longer?
You have a data frame with 3 rows and 4 columns: one ID column and three measurement columns. After applying
pivot_longer to gather the three measurement columns, how many rows will the resulting data frame have?Attempts:
2 left
💡 Hint
Each original row will produce one row per gathered column.
✗ Incorrect
pivot_longer stacks the selected columns into rows. With 3 original rows and 3 columns gathered, the result has 3 * 3 = 9 rows.