0
0
R Programmingprogramming~10 mins

pivot_longer (wide to long) 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 load the tidyr package.

R Programming
library([1])
Drag options to blanks, or click blank then click option'
Adplyr
Btidyr
Cggplot2
Dstringr
Attempts:
3 left
💡 Hint
Common Mistakes
Loading the wrong package like dplyr or ggplot2 instead of tidyr.
2fill in blank
medium

Complete the code to reshape the data frame df from wide to long format using pivot_longer().

R Programming
df_long <- pivot_longer(df, cols = [1], names_to = "variable", values_to = "value")
Drag options to blanks, or click blank then click option'
Ac(col1, col2)
Bc(1, 2)
Ccol1:col2
Dc("col1", "col2")
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted column names or numeric indices instead of strings.
3fill in blank
hard

Fix the error in the code to correctly specify the columns to pivot.

R Programming
df_long <- pivot_longer(df, cols = [1], names_to = "variable", values_to = "value")
Drag options to blanks, or click blank then click option'
Acol1:col2
Bc(col1, col2)
Cc("col1", "col2")
Dc('col1', 'col2')
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted column names or colon notation which is invalid here.
4fill in blank
hard

Fill both blanks to create a long format data frame with columns named "key" and "val".

R Programming
df_long <- pivot_longer(df, cols = [1], names_to = [2], values_to = "val")
Drag options to blanks, or click blank then click option'
Ac("A", "B")
B"variable"
C"key"
Dc("col1", "col2")
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of columns and names_to arguments.
5fill in blank
hard

Fill all three blanks to pivot columns "X1" and "X2" into a long format with names in "variable" and values in "value".

R Programming
df_long <- pivot_longer(df, cols = [1], names_to = [2], values_to = [3])
Drag options to blanks, or click blank then click option'
Ac("X1", "X2")
B"value"
C"variable"
D"val"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the names_to and values_to argument values.