0
0
R Programmingprogramming~20 mins

pivot_wider (long to wide) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pivot Wider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this pivot_wider code?
Given the following R code using pivot_wider, what will be the resulting data frame?
R Programming
library(tidyr)
library(dplyr)

long_df <- data.frame(
  id = c(1, 1, 2, 2),
  key = c("A", "B", "A", "B"),
  value = c(10, 20, 30, 40)
)

wide_df <- pivot_wider(long_df, names_from = key, values_from = value)
print(wide_df)
AAn error because pivot_wider needs a grouping variable
BA tibble with columns: id, key, value; rows doubled
CA tibble with columns: id, A, B; rows: (1,10,20), (2,30,40)
DA tibble with columns: key, value; rows unchanged
Attempts:
2 left
💡 Hint
Think about how pivot_wider spreads key-value pairs into columns.
Predict Output
intermediate
2:00remaining
What does this pivot_wider code produce?
Consider this R code snippet using pivot_wider. What is the output?
R Programming
library(tidyr)
long_df <- data.frame(
  person = c("Alice", "Alice", "Bob", "Bob"),
  measurement = c("height", "weight", "height", "weight"),
  value = c(165, 60, 180, 75)
)
wide_df <- pivot_wider(long_df, names_from = measurement, values_from = value)
print(wide_df)
AAn error because 'value' column is numeric
BA tibble with columns: measurement, value; rows unchanged
CA tibble with columns: person, measurement, value; rows doubled
DA tibble with columns: person, height, weight; rows: (Alice,165,60), (Bob,180,75)
Attempts:
2 left
💡 Hint
pivot_wider reshapes long data by spreading measurement names into columns.
🔧 Debug
advanced
2:00remaining
What error does this pivot_wider code raise?
What error will this R code produce when run?
R Programming
library(tidyr)
long_df <- data.frame(
  id = c(1, 1, 2),
  key = c("X", "X", "X"),
  value = c(5, 10, 15)
)

pivot_wider(long_df, names_from = key, values_from = value)
AError: Values in 'value' are not uniquely identified; multiple rows per id-key pair
BNo error; returns a wide data frame with columns id, X, Y
CError: 'names_from' column 'key' not found
DError: 'values_from' column 'value' is missing
Attempts:
2 left
💡 Hint
Check if each id-key pair has only one value.
Predict Output
advanced
2:00remaining
What is the output of pivot_wider with multiple value columns?
What will this R code output when run?
R Programming
library(tidyr)
long_df <- data.frame(
  id = c(1, 1, 2, 2),
  key = c("A", "B", "A", "B"),
  val1 = c(10, 20, 30, 40),
  val2 = c(100, 200, 300, 400)
)
wide_df <- pivot_wider(long_df, names_from = key, values_from = c(val1, val2))
print(wide_df)
AA tibble with columns: id, val1_A, val1_B, val2_A, val2_B; rows: (1,10,20,100,200), (2,30,40,300,400)
BA tibble with columns: id, A, B; rows with nested lists of val1 and val2
CError: values_from must be a single column name
DA tibble with columns: id, key, val1, val2; rows unchanged
Attempts:
2 left
💡 Hint
pivot_wider can handle multiple value columns by creating multiple new columns per key.
🧠 Conceptual
expert
2:00remaining
How many rows are in the output after pivot_wider?
Given this long data frame with 6 rows and 3 unique ids, what will be the number of rows after applying pivot_wider with names_from = variable and values_from = value?
R Programming
library(tidyr)
long_df <- data.frame(
  id = c(1,1,2,2,3,3),
  variable = c("X", "Y", "X", "Y", "X", "Y"),
  value = c(5, 10, 15, 20, 25, 30)
)
wide_df <- pivot_wider(long_df, names_from = variable, values_from = value)
A6
B3
C2
D1
Attempts:
2 left
💡 Hint
pivot_wider groups by the columns not used in names_from or values_from.