Search code examples
rloopsincrement

How to increment a value in column when a chose value id in another column


I have this kind if table :

Value:

U

R

T

'*

D

E

'*

M

R

Z

.*

...

I want to create a second column with value incremented when a "*" is the first column :

Value,newcolumn:

U 1

R 1

T 1

'* 1

D 2

E 2

'* 2

M 3

R 3

Z 3

'* 3

How would you do it by not using for loop? Or what IS thé most idiomatic way for doing this ?

I will be very grateful toward any help.

Thanks.

I tried with data.frame rleid, but the count is incremented when "*" is the value columns, while I want it to be incremented just after.

My data : structure(list(value = c("U", "R", "T", "*", "D", "E", "*", "M","R", "Z", "*")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,-11L))

Solved (thanks to GuedesBF):

df %>% mutate(value2 = cumsum(lag(value, default = '') == '*') +1)


Solution

  • We can use rleid or cumsumon the lagged values:

    library(dplyr)
    
    df %>% mutate(value2 = paste0(value,
                                  ' ',
                                  cumsum(lag(value, default = '') == '*') +1))
    
    
    # A tibble: 11 × 2
       value value2
       <chr> <chr> 
     1 U     U 1   
     2 R     R 1   
     3 T     T 1   
     4 *     * 1   
     5 D     D 2   
     6 E     E 2   
     7 *     * 2   
     8 M     M 3   
     9 R     R 3   
    10 Z     Z 3   
    11 *     * 3