Sum after Rbind and group_by

Hi Community I try to sum after create a unique df with rbind.

df <- data.frame(a=c('A', 'B', 'C', 'D'),
                 b=c(1, 2, 3, 5))
df1 <- data.frame(a=c('A', 'B', 'G', 'H'),
                 b=c(3, 4, 1, 1))
df3 <- rbind(df, df1)
df3 <- df3 %>% 
  group_by(a) %>% 
  sum("b")

The sum function does not know about the grouping of a data frame. Use the summarize () function can be used to do this.

df3 <- df3 %>% 
  group_by(a) %>% 
  summarize(SUM = sum(b))

Hey great, It works

Might be you know, how to create a column in the same df with the sum by month and year.

format: dmy

df <- data.frame(date=c('02/01/2021', '18/01/2021', '26/02/2021', '16/02/2021'),
                 b=c(1, 2, 3, 5))
df1 <- data.frame(a=c('14/02/2021', '27/04/2021', '06/06/2021', '15/01/2021'),
                 b=c(3, 4, 1, 1))
df3 <- rbind(df, df1)
df3 <- df3 %>% 
  group_by(a) %>% 
  summarise_each(funs(sum(b))

You need to convert date from character to appropriate date format. Then you can group data by month & year.

df <- data.frame(date=c('02/01/2021', '18/01/2021', '26/02/2021', '16/02/2021'),
                 b=c(1, 2, 3, 5))
df1 <- data.frame(date=c('14/02/2021', '27/04/2021', '06/06/2021', '15/01/2021'),
                  b=c(3, 4, 1, 1))
df3 <- rbind(df, df1)

library(dplyr)
library(lubridate)

df3 %>% 
  mutate(date = as_date(date, format="%d/%m/%Y")) %>% 
  group_by(month_year = floor_date(date, "month")) %>% 
  summarize(sum=sum(b))


# if you really want to present date as Month & Year

df3 %>% 
  mutate(date = as_date(date, format="%d/%m/%Y")) %>% 
  group_by(month_year = floor_date(date, "month")) %>% 
  summarize(sum=sum(b)) %>% 
  mutate(month_year = zoo::as.yearmon(month_year))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.