Groupby and sum different columns

I have a dataset, df, where I wish to sum and groupby the type along with the date:

     date     size       type

1/1/2020     1          a
1/1/2020     1          a
1/1/2020     1          a
1/1/2020     2          b
1/1/2020     5          b
1/1/2020     6          b
2/1/2020     20         a
2/1/2020     21         a
2/1/2020     10         a
2/1/2020     1          b
2/1/2020     4          b     
2/1/2020     5          b

Desired output

(grouping by type and date to find sum)

date        size    type

1/1/2020    3       a
2/1/2020    51      a
1/1/2020    13      b
2/1/2020    10      b 

This is what I am doing:

 library(plyr)
 ddply(df, .(date, type), (sum))

dput

 structure(list(Date = c("1/1/2020", "1/1/2020", "1/1/2020", "1/1/2020", 
"1/1/2020", "1/1/2020", "2/1/2020", "2/1/2020", "2/1/2020", "2/1/2020", 
"2/1/2020", "2/1/2020"), size = c(1L, 1L, 1L, 2L, 5L, 6L, 20L, 
 21L, 10L, 1L, 4L, 5L), type = c("a", "a", "a", "b", "b", "b", 
"a", "a", "a", "b", "b", "b")), class = "data.frame", row.names = c(NA, 
-12L))
library(dplyr)

df <-  structure(list(Date = c("1/1/2020", "1/1/2020", "1/1/2020", "1/1/2020", 
                               "1/1/2020", "1/1/2020", "2/1/2020", "2/1/2020", "2/1/2020", "2/1/2020", 
                               "2/1/2020", "2/1/2020"), size = c(1L, 1L, 1L, 2L, 5L, 6L, 20L, 
                                                                 21L, 10L, 1L, 4L, 5L), type = c("a", "a", "a", "b", "b", "b", 
                                                                                                 "a", "a", "a", "b", "b", "b")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                     -12L))
df %>% 
    group_by(Date, type) %>% 
    summarise(size = sum(size)) %>% 
    arrange(type, Date)
#> `summarise()` regrouping output by 'Date' (override with `.groups` argument)
#> # A tibble: 4 x 3
#> # Groups:   Date [2]
#>   Date     type   size
#>   <chr>    <chr> <int>
#> 1 1/1/2020 a         3
#> 2 2/1/2020 a        51
#> 3 1/1/2020 b        13
#> 4 2/1/2020 b        10

Created on 2020-11-04 by the reprex package (v0.3.0.9001)

1 Like

Thank you,
Can I save this to a new name? df<-df %>%
group_by(Date, type) %>%
summarise(size = sum(size)) %>%
arrange(type, Date)

I tried saving it to a new name and received an error:

summarise() regrouping output by 'Date' (override with .groups argument)

Oh I see this is just a warning. Disregard. It works fine. Thank you

That is not an error, just an informative message about the grouping, you can drop the grouping if you like.

df <- df %>% 
    group_by(Date, type) %>% 
    summarise(size = sum(size), .groups = "drop") %>% 
    arrange(type, Date)

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.