Last active
August 29, 2015 14:18
-
-
Save jlehtoma/216759807f2d11d80e64 to your computer and use it in GitHub Desktop.
Massaging data
This file contains hidden or 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(dplyr) | |
library(tidyr) | |
dat <- data.frame(laji=c("accgen", "accnis", "accgen", "accnis" , "accgen", | |
"accnis", "accgen", "accnis"), | |
lkm=c(2, 2, 1, 4, 3, 2, 2, 6), | |
linja_P_E = c("P", "P", "E", "E", "P", "P", "E", "E")) | |
dat_agg <- dat %>% | |
# Group by both laji and linja_P_E | |
group_by(laji, linja_P_E) %>% | |
# Then calculate the total count per laji/linja_P_E | |
summarise( | |
lkm=sum(lkm) | |
) %>% | |
# Grouping set by group_by() is still on, let's ungroup the data | |
ungroup() %>% | |
# Spread (pivot) the data in terms of linja_P_E -> creates cols "P" and "E". | |
# spread is from tidyr | |
spread(linja_P_E, lkm) %>% | |
# Calculate the total count into a new column "lkm" | |
mutate(lkm=P+E) %>% | |
# Reorder and rename the cols | |
select(laji, lkm, lkm_P=P, lkm_E=E) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment