Reshaping data in R to be long or wide with pivot_longer and pivot_wider (CC185)
Channel: Riffomonas Project
Date: 2022-02-07

The tidyverse's dplyr R package has powerful functions for reshaping data frames to be long or wide using the pivot_longer and pivot_wider functions, respectively. In this episode, Pat Schloss will show how to use these functions to convert a distance matrix into a format that is easier to use with other tools like inner_join and filter. The goal of this episode is to filter a square distance matrix to only contain distances from a subset of samples. We'll do all this in RStudio using the stringr package.

You can find my blog post for this episode at https://www.riffomonas.org/code_club/2022-02-07-pivot-longer-wider. The data were generated in our Kozich et al. 2013 paper (http://doi.org/10.1128/AEM.01043-13) using samples from the Schloss et al. 2012 paper (http://doi.org/10.4161/gmic.21008).

#pivot_longer #pivot_wider #inner_join #R #Rstudio #Rstats

Want more practice on the concepts covered in Code Club? You can sign up for my weekly newsletter at https://shop.riffomonas.org/youtube to get practice problems, tips, and insights.

If you're interested in taking an upcoming 3 day R workshop be sure to check out our schedule at https://riffomonas.org/workshops/

You can also find complete tutorials for learning R with the tidyverse using...
Microbial ecology data: https://www.riffomonas.org/minimalR/
General data: https://www.riffomonas.org/generalR/

0:00 Reshaping a data frame with pivot_longer and pivot_wider
2:31 Gathering columns with pivot_longer
3:51 Spreading columns with pivot_wider
6:20 Joining the same data frame to different columns
8:56 Creating distance matrix with desired samples