Skip to content

Instantly share code, notes, and snippets.

@btskinner
Created April 7, 2017 13:55
Show Gist options
  • Save btskinner/a1f5bc5c1c32b48d4f45b05d2531e423 to your computer and use it in GitHub Desktop.
Save btskinner/a1f5bc5c1c32b48d4f45b05d2531e423 to your computer and use it in GitHub Desktop.
Reshaping data in R
library(dplyr)
library(tidyr)
## make wide toy data
df <- data.frame(schid = c('A','B','C','D'),
year = 2013,
var_x = 1:4,
var_y = 5:8,
var_z = 9:12,
stringsAsFactors = FALSE) %>%
tbl_df()
## gather (wide --> long)
df_long <- df %>%
gather(var, value, -c(schid, year)) %>%
mutate(var = gsub('var_', '', var)) %>%
arrange(schid, var)
## spread (long --> wide)
df_wide <- df_long %>%
mutate(var = gsub('(.*)', 'var_\\1', var)) %>%
spread(var, value) %>%
arrange(schid)
## print df
df
df_long
df_wide
## confirm that df_wide == df
identical(df, df_wide)
## -- NOTES --
## mutate() commands not strictly necessary; just for -/+ stub names
## tbl_df() not necessary; just makes printing nicer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment