rfor-loopdplyrrolling-sum

How to get tally (rolling sum) by group in R?


I would like to create a column called "tally" in my dataset that takes sums the count of each type and rank.

type <- c("A","A","A","B","B","C")
rank <- c("low", "med", "high","med", "high", "low")
count <- c(9,20,31,2,4,14)

df <- data.frame(type, rank, count)

My desired output would be:

type rank count tally
1    A  low     9     9
2    A  med    20    29
3    A high    31    60
4    B  med     2     2
5    B high     4     6
6    C  low    14    14 

I guess another way to describe it would be a rolling sum (where it takes into account the low to high order)? I have looked around but I can't find any good functions to do this. Ideally, I could have a for loop that would allow me to get this "rolling sum" by type.


Solution

  • We can use cumsum after grouping by 'type'

    library(dplyr)
    df <- df %>%
       group_by(type) %>%
        mutate(tally = cumsum(count)) %>%
        ungroup
    

    -output

    # A tibble: 6 x 4
      type  rank  count tally
      <chr> <chr> <dbl> <dbl>
    1 A     low       9     9
    2 A     med      20    29
    3 A     high     31    60
    4 B     med       2     2
    5 B     high      4     6
    6 C     low      14    14