1

I have daily data (df$date is the daily field):

enter image description here

Which I want to group by week (df$wbm = "week beginning monday") in a new data frame (df2). When I run the below statement, the data frame that is returned is the same as the original:

df2<- df%>%
  group_by(wbm)

The function runs without throwing an error, but it just produces the same data frame. enter image description here

How can I drop date and ensure that my variables are grouped by wbm?

dre
  • 474
  • 5
  • 19

1 Answers1

4

The group_by steps adds a grouping attribute, but we didn't give any command as to how to summarise it. If we need to get the sum of the columns that have column names as 'var' grouped by 'wbm', then use summarise_at

library(dplyr)
df%>%
   group_by(wbm) %>%
   summarise_at(vars(matches('^var\\d+$')), sum)

If it is only a single column to be summarised, it can be summarise

df %>%
   group_by(wbm) %>%
   summarise(var1 = sum(var1))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    "If we need" , "If it is". If the question is unclear, please ask OP for clarification instead of guessing. Cheers. – Henrik Nov 21 '18 at 19:34