We aren’t computer scientists and that’s okay!
We make lots of mistakes. Mistakes are funny. You can laugh with us.
Let’s go, Simba, Pumbaa, and Timon!
Bind multiple data frames by column.
All data frames must have the same number of rows.
Bind multiple data frame by rows.
We use this function to increase the number of rows and decrease the number of columns.
# create a data frame
df <- data.frame(site = 1:3,
jan = runif(3, 0, 1),
feb = rnorm(3, 5),
mar = rnorm(3, 10))
long_df <- df %>%
pivot_longer(-site, names_to = "month", values_to = "value" )
df %>%
pivot_longer(jan:mar, names_to = "month", values_to = "value")
cheatsheet of tidyr
https://raw.githubusercontent.com/rstudio/cheatsheets/main/tidyr.pdf
Exercise
Apply the pivot_longer() function to reshape the billboard dataset
This function is the inverse transformation of pivot_longer().
Exercise
Use the pivot_wider() function to reshape the us_rent_income dataset
bind data frames
pivot_longer()
pivot_wider()