Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the pivot_wider function do in R's tidyverse?
easy
A. It filters rows based on a condition.
B. It transforms data from long format to wide format by spreading key-value pairs into columns.
C. It sorts data in ascending order.
D. It merges two data frames by a common column.

Solution

  1. Step 1: Understand the purpose of pivot_wider

    The function is designed to reshape data by turning unique values from one column into multiple columns.
  2. Step 2: Compare with other data operations

    Filtering, sorting, and merging are different operations; pivot_wider specifically changes data shape from long to wide.
  3. Final Answer:

    It transforms data from long format to wide format by spreading key-value pairs into columns. -> Option B
  4. Quick Check:

    pivot_wider = long to wide reshape [OK]
Hint: pivot_wider spreads rows into columns for easier comparison [OK]
Common Mistakes:
  • Confusing pivot_wider with filtering or sorting functions
  • Thinking pivot_wider merges data frames
  • Assuming pivot_wider removes rows instead of reshaping
2. Which of the following is the correct syntax to use pivot_wider to spread the column key into multiple columns with values from value?
easy
A. pivot_wider(data, values_from = key, names_from = value)
B. pivot_wider(data, spread = key, gather = value)
C. pivot_wider(data, key = names_from, value = values_from)
D. pivot_wider(data, names_from = key, values_from = value)

Solution

  1. Step 1: Recall pivot_wider argument names

    The correct arguments are names_from for the column to spread and values_from for the values to fill.
  2. Step 2: Match arguments to columns

    pivot_wider(data, names_from = key, values_from = value) correctly uses names_from = key and values_from = value, matching the expected syntax.
  3. Final Answer:

    pivot_wider(data, names_from = key, values_from = value) -> Option D
  4. Quick Check:

    names_from = key, values_from = value [OK]
Hint: Use names_from for columns, values_from for data [OK]
Common Mistakes:
  • Swapping names_from and values_from arguments
  • Using incorrect argument names like spread or gather
  • Passing arguments in wrong order or with wrong names
3. Given this data frame in R:
library(tidyr)
data <- data.frame(
  id = c(1,1,2,2),
  variable = c('A', 'B', 'A', 'B'),
  value = c(10, 20, 30, 40)
)
result <- pivot_wider(data, names_from = variable, values_from = value)
print(result)
What will be the output?
medium
A. id A B 1 1 10 20 2 2 30 40
B. id variable value 1 1 A 10 2 1 B 20 3 2 A 30 4 2 B 40
C. id A 1 1 10 2 2 30
D. Error: object 'pivot_wider' not found

Solution

  1. Step 1: Understand pivot_wider effect on data

    It converts the 'variable' column values ('A', 'B') into separate columns, filling them with corresponding 'value' entries.
  2. Step 2: Check the resulting data frame structure

    Each 'id' has columns 'A' and 'B' with values 10, 20 for id=1 and 30, 40 for id=2.
  3. Final Answer:

    id A B 1 1 10 20 2 2 30 40 -> Option A
  4. Quick Check:

    pivot_wider spreads variable into columns with values [OK]
Hint: pivot_wider creates columns from variable, fills with value [OK]
Common Mistakes:
  • Expecting original long format output
  • Confusing column names after pivot
  • Not loading tidyr library causing error
4. You run this code but get an error:
library(tidyr)
data <- data.frame(
  id = c(1,1,2,2),
  variable = c('A', 'B', 'A', 'B'),
  value = c(10, 20, 30, 40)
)
result <- pivot_wider(data, names_from = variable)
print(result)
What is the likely cause of the error?
medium
A. The library tidyr is not loaded.
B. The data frame has duplicate rows causing conflict.
C. Missing the values_from argument to specify which column fills new columns.
D. The variable column contains invalid characters.

Solution

  1. Step 1: Check pivot_wider required arguments

    pivot_wider needs both names_from and values_from to know which columns to spread and fill.
  2. Step 2: Identify missing argument

    The code only provides names_from = variable but misses values_from, causing an error.
  3. Final Answer:

    Missing the values_from argument to specify which column fills new columns. -> Option C
  4. Quick Check:

    values_from argument is required [OK]
Hint: Always include values_from with names_from in pivot_wider [OK]
Common Mistakes:
  • Forgetting values_from argument
  • Assuming names_from alone is enough
  • Not reading error messages carefully
5. You have this long data frame with sales data:
sales <- data.frame(
  store = c('X', 'X', 'Y', 'Y'),
  product = c('apple', 'banana', 'apple', 'banana'),
  sales_amount = c(100, 150, 200, 250)
)
You want to create a wide table where each product is a column showing sales per store. Which code correctly does this and handles missing values by filling with 0?
hard
A. pivot_wider(sales, names_from = product, values_from = sales_amount, values_fill = 0)
B. pivot_wider(sales, names_from = sales_amount, values_from = product, values_fill = 0)
C. pivot_wider(sales, names_from = product, values_from = sales_amount)
D. pivot_wider(sales, names_from = store, values_from = sales_amount, values_fill = NA)

Solution

  1. Step 1: Identify columns to spread and fill

    The products ('apple', 'banana') become new columns, so names_from = product. The sales amounts fill these columns, so values_from = sales_amount.
  2. Step 2: Handle missing values

    Use values_fill = 0 to replace any missing sales with zero for completeness.
  3. Final Answer:

    pivot_wider(sales, names_from = product, values_from = sales_amount, values_fill = 0) -> Option A
  4. Quick Check:

    Use values_fill to replace missing with 0 [OK]
Hint: Add values_fill = 0 to fill missing cells with zero [OK]
Common Mistakes:
  • Swapping names_from and values_from
  • Not using values_fill causing NA in output
  • Using wrong columns for names_from or values_from
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard