What is the best way to create overlapping groups using dplyr?
For example, imagine you have the following data set
test <- data.frame(year = rep(as.character(2014:2016), 2), value = 1:6)
And you want to summarize using a group for each year, and then full period. Two ways to do this could be as follows:
Using bind_rows and mutate (and probably filter in a more complex example)
year.totals <- bind_rows(test %>% mutate(year = "2014:2016"),
test) %>%
group_by(year) %>%
summarize(value = sum(value))
Using gather
year.totals.2 <- test %>%
mutate(year.2 = "2014:2016") %>%
gather(drop, year, year, year.2) %>%
group_by(year) %>%
summarize(value = sum(value))
Is there a better way to do this?
I've also seen this question and answer, which I don't think is too bad but I would prefer to avoid the lapply.
base R
i.e.addmargins(rowsum(test$value, test$year), 1)
– akrundplyr
doesn't have this functionality explicitly. I still use thebind_rows()
approach in the absence of anything better – wjchulme