Created
January 10, 2019 00:03
-
-
Save aaronschiff/850e6bcff968327785e902cce3a71ad3 to your computer and use it in GitHub Desktop.
Example of gather-unite-spread data reshaping in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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