Recall & Review
beginner
What does the
pivot_longer() function do in R?It transforms data from a wide format (many columns) into a long format (fewer columns, more rows), making it easier to analyze and visualize.
Click to reveal answer
beginner
In
pivot_longer(), what do the cols and names_to arguments specify?cols selects which columns to turn from wide to long. names_to names the new column that will hold the old column names.Click to reveal answer
intermediate
Why would you use
pivot_longer() instead of keeping data wide?Long format data is easier to work with for many R functions and plotting tools because it standardizes the structure, making grouping and summarizing simpler.
Click to reveal answer
beginner
What happens if you omit the
values_to argument in pivot_longer()?The new column holding the values from the original columns will be named
value by default.Click to reveal answer
intermediate
How can you use
pivot_longer() to reshape this data?<br>id Jan Feb Mar 1 A 10 20 30 2 B 15 25 35
Use
pivot_longer(cols = Jan:Mar, names_to = "month", values_to = "value") to get columns id, month, and value with one row per month per id.Click to reveal answer
What is the main purpose of
pivot_longer() in R?✗ Incorrect
pivot_longer() changes wide data (many columns) into long data (fewer columns, more rows).
Which argument in
pivot_longer() specifies the new column name for the old column names?✗ Incorrect
names_to sets the name of the new column that holds the original column names.
If you want to keep columns
id and reshape columns Q1, Q2, Q3, which cols argument would you use?✗ Incorrect
cols = Q1:Q3 selects columns Q1 to Q3 to pivot longer.
What default name does
pivot_longer() give to the values column if values_to is not set?✗ Incorrect
The default name for the values column is value.
Which package provides the
pivot_longer() function?✗ Incorrect
pivot_longer() is part of the tidyr package.
Explain in your own words what
pivot_longer() does and why it is useful.Think about changing many columns into fewer columns with more rows.
You got /3 concepts.
Describe how you would use
pivot_longer() to reshape a dataset with monthly sales columns into a long format with one column for month and one for sales.Focus on naming the new columns for months and sales values.
You got /3 concepts.