Skip to content

Instantly share code, notes, and snippets.

@jlehtoma
Last active August 29, 2015 14:18
Show Gist options
  • Save jlehtoma/216759807f2d11d80e64 to your computer and use it in GitHub Desktop.
Save jlehtoma/216759807f2d11d80e64 to your computer and use it in GitHub Desktop.
Massaging data
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