Search code examples
rdplyrtidyversedata-cleaningdata-wrangling

How to tally occurrences of a value in a column in R?


I have the following data:

df <- data.frame(id = c("1", "1", "1", "1", "2", "2", "2"), 
                 x = c(0, 1, 0, 1, 0, 1, 0))

id x
 1 0
 1 1
 1 0
 1 1
 2 0
 2 1
 2 0

I want to tally the occurences of 1 in x. The desired output:

id x count
 1 0     0
 1 1     1
 1 0     0
 1 1     2
 2 0     0
 2 1     1
 2 0     0

How do I do this? A dplyr solution is preferred.


Solution

  • You may take cumulative sum of x values and keep the result to be 0 where value of x is 0 for each id.

    library(dplyr)
    
    df %>%
      mutate(count = replace(cumsum(x), x == 0, 0), .by = id)
    
    #  id x count
    #1  1 0     0
    #2  1 1     1
    #3  1 0     0
    #4  1 1     2
    #5  2 0     0
    #6  2 1     1
    #7  2 0     0