0
0
R Programmingprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pivot Longer 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_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)
A
  id  subject score
1  1     math    90
2  1  science    85
3  2     math    80
4  2  science    95
B
  id  subject score
1  1  science    85
2  1     math    90
3  2  science    95
4  2     math    80
C
  id  subject score
1  1     math    90
2  2     math    80
3  1  science    85
4  2  science    95
D
  id  subject score
1  1     math    90
2  1  science    85
3  2  science    95
4  2     math    80
Attempts:
2 left
💡 Hint
Remember that pivot_longer stacks the selected columns in order, keeping the original row order.
🧠 Conceptual
intermediate
1:30remaining
Which statement best describes the purpose of pivot_longer?
What does the pivot_longer function do in data manipulation?
AIt converts data from a wide format to a long format by gathering multiple columns into key-value pairs.
BIt converts data from a long format to a wide format by spreading key-value pairs into multiple columns.
CIt filters rows based on a condition in one or more columns.
DIt sorts the data frame by one or more columns.
Attempts:
2 left
💡 Hint
Think about how columns are transformed into rows.
🔧 Debug
advanced
2: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")
AError: unused argument (names_to = "subject")
BError: unexpected symbol in "pivot_longer(df, cols = math science"
CError: object 'math science' not found
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the syntax for specifying multiple columns in the cols argument.
Predict Output
advanced
2: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)
A
  id       subject score
1  1   score_math    90
2  1 score_science    85
3  2 score_science    95
4  2   score_math    80
B
  id       subject score
1  1   score_math    90
2  2   score_math    80
3  1 score_science    85
4  2 score_science    95
C
  id       subject score
1  1   score_math    90
2  1 score_science    85
3  2   score_math    80
4  2 score_science    95
D
  id       subject score
1  1 score_science    85
2  1   score_math    90
3  2 score_science    95
4  2   score_math    80
Attempts:
2 left
💡 Hint
The order of rows follows the original row order and the order of selected columns.
🚀 Application
expert
1: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?
A12
B4
C3
D9
Attempts:
2 left
💡 Hint
Each original row will produce one row per gathered column.