0
0
R Programmingprogramming~10 mins

pivot_wider (long to wide) in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the data from long to wide format using pivot_wider.

R Programming
library(tidyr)
data <- data.frame(name = c('A', 'A', 'B', 'B'),
                   key = c('x', 'y', 'x', 'y'),
                   value = c(1, 2, 3, 4))
wide_data <- pivot_wider(data, names_from = [1], values_from = value)
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Cname
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong column for names_from, like 'value' or 'name'.
Confusing names_from with values_from.
2fill in blank
medium

Complete the code to specify the values column in pivot_wider.

R Programming
wide_data <- pivot_wider(data, names_from = key, values_from = [1])
Drag options to blanks, or click blank then click option'
Avalue
Bkey
Cname
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using the key column instead of the value column for values_from.
Mixing up names_from and values_from.
3fill in blank
hard

Fix the error in the code to correctly pivot the data wider.

R Programming
wide_data <- pivot_wider(data, names_from = key, values_from = [1])
Drag options to blanks, or click blank then click option'
Avalues
Bvalues_from
Cval
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural or incorrect column names.
Trying to use argument names as column names.
4fill in blank
hard

Fill both blanks to create a wide data frame with names as rows and keys as columns.

R Programming
wide_data <- pivot_wider(data, names_from = [1], values_from = [2])
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Cname
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the two arguments.
Using the wrong column names.
5fill in blank
hard

Fill all three blanks to pivot the data wider and rename the new columns with uppercase keys.

R Programming
wide_data <- pivot_wider(data, names_from = [1], values_from = [2], names_prefix = [3])
Drag options to blanks, or click blank then click option'
Akey
Bvalue
C"COL_"
D"VAL_"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names.
Forgetting to put quotes around the prefix string.