Search code examples
rdplyrradix

Change on column based on any previous values in another column


I have a dataframe like so:

foo <- c(rep(FALSE, 5), TRUE, rep(FALSE, 4))
rank_order <- seq(11,20)

df <- data.frame(rank_order = as.numeric(rank_order),
                 foo = foo)

What I'd like to do is add one to every value of rank_order following a row where df$foo == TRUE. That means rank_order should look like this:

rank_order_target <- c(11, 12, 13, 14, 15, 17, 18, 19, 20, 21)

It's easy enough to change one value of rank_order, with lag looking at one previous value of foo (like below), but how do I look at all previous values of foo?

df %>% 
  mutate(rank_order_new = case_when(lag(foo, default = FALSE) == TRUE ~ rank_order + 1,
                          TRUE ~ rank_order))
   rank_order   foo rank_order_new
1          11 FALSE             11
2          12 FALSE             12
3          13 FALSE             13
4          14 FALSE             14
5          15 FALSE             15
6          16  TRUE             16
7          17 FALSE             18
8          18 FALSE             18
9          19 FALSE             19
10         20 FALSE             20

Either a base solution, or something tidyverse would be helpful.


Solution

  • We can use cumsum

    library(dplyr)
    df %>%
         mutate(new = rank_order + cumsum(foo))