name | sex | total |
---|---|---|
Courtney | F | 257289 |
Courtney | M | 22619 |
Riley | F | 100881 |
Riley | M | 92789 |
pivot_wider
and pivot_longer
in the tidyr
packageDownload a template .Rmd of this activity. Put the file in a Day_08
folder within your COMP_STAT_112
folder.
Everyone: Look at this small data set.
Partner A: Describe the structure of the data set in words.
name | sex | total |
---|---|---|
Courtney | F | 257289 |
Courtney | M | 22619 |
Riley | F | 100881 |
Riley | M | 92789 |
Partner A: Close your eyes.
Partner B: Describe the how the structure of the data set changed. Think of describing “steps” taken.
Partner A: Sketch what you think the new data set looks like.
name | F | M |
---|---|---|
Courtney | 257289 | 22619 |
Riley | 100881 | 92789 |
Partner B: Close your eyes.
Partner A: Describe the how the structure of the data set changed. Think of describing “steps” taken.
Partner B: Sketch what you think the new data set looks like.
name | F | M | ratio |
---|---|---|---|
Courtney | 257289 | 22619 | 0.0879128 |
Riley | 100881 | 92789 | 0.9197867 |
name | ratio | sex | total |
---|---|---|---|
Courtney | 0.0879128 | F | 257289 |
Courtney | 0.0879128 | M | 22619 |
Riley | 0.9197867 | F | 100881 |
Riley | 0.9197867 | M | 92789 |
If we want to maintain all of the values in the data set (not collapsing rows with summarize
) but have a different unit of observation (or case), we can:
pivot_wider()
pivot_longer()
var_name2
, var_name2
), names_to = “string”, values_to = “string”Go through the example code in the Rmd file to make sure you understand how we reshape data to be wider and longer.
Part 1 in Chap 7 (Six Main Verbs)
Part 2 in Chap 8 (Reshaping Data)