I am computing summary statistcs for many variables in a large data frame (it has 130 variables). I wish to compute summary statistics per decade and for all my data. I would like to avoid writing summarise twice: once for the grouping and once for the full data. I also use summarise_at, so my question also applies for summarise_at.
Here is a minimal example, where summarise_at() seems a bit over the top, but not so for my real data:
my.data <- data.frame(Date = as.Date(c('1981-04-09', '1983-02-01', '1992-10-19', '1996-11-22', '1987-05-15')),
decade = c('Eighties', 'Eighties', 'Nineties', 'Nineties', 'Eighties'),
price = c(10, 11, 17, 34, 12),
d.sector.Oil = c(0,0,1,1,1),
d.sector.Mines = c(1,1,0,0,0)) # An example dataframe
# Calculate summary statistics for each decade and each variable:
sumst.decades<- my.data %>% group_by(decade) %>% summarise(mean.price = mean(price))
sumd.decades<- my.data %>% group_by(decade) %>% summarise_at(vars(starts_with('d.sector.')), sum)
I need the same summaries for my full data. For which I would repeat above command lines, but without the "group_by":
sumst<- summarise(my.data, mean.price = mean(price))
sumd<- summarise_at(my.data, vars(starts_with('d.sector.')), sum)
I wish to not have to write the last two lines but somehow have dplyr give me a global summary in addition to the groupwise summaries. For example, if there were a function group_by* that creates groups as required and also creates a global group. Does something like this exist? I hope my question is clear. Thanks in advance.