0

I have some data with count value (which contain integer), date column and a identifiant column (containing 10 distinct values). I would like to know when the identifiant reaches a value in count value (like 100). For this reason, I would like to cummulate my count value for each identifiant (I don't know how to do this first part in R, I used Data.table) and after I will do a condition (when my commulate column is > 100, I will put 1 else 0) and a selection.

For the cummulate part, I don't know how to do according to column value.

#◘ Exemple of data
data <-data.frame(identifiant = c("A","A","A","A","A","B","B","B"),
                  date = as.Date(c("01/01/2018","02/01/2018","03/01/2018","04/01/2018","08/01/2018","03/01/2018","04/01/2018","08/01/2018"),format = '%d/%m/%Y'),
                  count = c(25,39,50,41,10,3,95,2))



# I would like a cummulate column like this

identifiant date    count   Cummulate
       A    01/01/2018  25  25
       A    02/01/2018  39  64
       A    03/01/2018  50  114
       A    04/01/2018  41  155
       A    08/01/2018  10  165
       B    03/01/2018  3   3
       B    04/01/2018  95  98
       B    08/01/2018  2   100

Thank you for advance

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
JulietteC
  • 129
  • 10

1 Answers1

3

We can group by 'identifiant' and get the cumulative sum of 'count'

library(dplyr)
data %>% 
   group_by(identifiant) %>% 
   mutate(Cummulate = cumsum(count))
# A tibble: 8 x 4
# Groups:   identifiant [2]
#  identifiant date       count Cummulate
#  <fct>       <date>     <dbl>     <dbl>
#1 A           2018-01-01    25        25
#2 A           2018-01-02    39        64
#3 A           2018-01-03    50       114
#4 A           2018-01-04    41       155
#5 A           2018-01-08    10       165
#6 B           2018-01-03     3         3
#7 B           2018-01-04    95        98
#8 B           2018-01-08     2       100
akrun
  • 874,273
  • 37
  • 540
  • 662