Search code examples
rlagdplyr

How to fill column based on previous value + value other column in R


I want to fill a new column that is based on the previous value of the column itself (of which the 1st value I set at 1) and the value of event.

test_model

Obs event enum
1056 1      1
1059 1      NA
1070 0      NA
1054 1      NA
1034 0      NA

This should become :

Obs event enum
1056 1      1
1059 1      2
1070 0      2
1054 1      3
1034 0      3

So far I have tried with lag()

test_model$enum <- mutate(test_model, enum = (lag(enum) + event))

However, this results in the column filled with the observations of a different column in test_model (Obs).

Obs event enum
1056 1      1056
1059 1      1059
1070 0      1070
1054 1      1054
1034 0      1034

Any idea how this happens & What way I should approach my objective?


Solution

  • I think you are lookig for the cumsum() function.

    test_model <- data.frame(Obs = c(1056,1059,1070,1054,1034),
                         event = c(1,1,0,1,0))
    test_model$enum <- cumsum(test_model$event)
    
    #Or using the tidyverse
    test_model <- test_model %>% mutate(enum = cumsum(event))