Skip to content

Instantly share code, notes, and snippets.

@aaronschiff
Created January 10, 2019 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronschiff/850e6bcff968327785e902cce3a71ad3 to your computer and use it in GitHub Desktop.
Save aaronschiff/850e6bcff968327785e902cce3a71ad3 to your computer and use it in GitHub Desktop.
Example of gather-unite-spread data reshaping in R
library(tidyverse)
example_dat <- tribble(
~year, ~group, ~revenues, ~costs,
2015, "Apple", 1000, 500,
2016, "Apple", 1500, 600,
2017, "Apple", 1200, 550,
2015, "Pear", 2000, 1100,
2016, "Pear", 2500, 1300,
2017, "Pear", 2900, 1500,
)
step1 <- example_dat %>%
gather(key = "measure", value = "value", revenues, costs)
step2 <- step1 %>%
unite(col = "measure", group, measure, sep = " ")
step3 <- step2 %>%
spread(key = "measure", value = "value")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment