Search code examples
rdataframecalculated-columnsdplyr

R - Mutate column based on another column


Using R: For the dataframe:

A<-c(3,3,3,3,1,1,2,2,2,2,2)
df<-data.frame(A)

How do you add a column such that the output is the same as:

A<-c(3,3,3,3,1,1,2,2,2,2,2)
df<-data.frame(A)

B<-c(1,1,1,0,1,0,1,1,0,0,0)
mutate(df,B)

In other words, is there a formula for column 'B' - such that it looks at column 'A'....and lists '1', 3 times the puts a '0' .....etc etc.

So - the desired output (given column 'A') is:

enter image description here

Thankyou.


Solution

  • Here I assign a new group each time A changes, then within each group put a 1 in B in the first #A rows.

    (If the values of A are distinct for each group, you could replace the first two lines with group_by(A), but unclear if that's a fair assumption.)

    library(dplyr)
    df %>%
      mutate(group = cumsum(A != lag(A, default = 0))) %>%
      group_by(group) %>%
      mutate(B = 1 * (row_number() <= A)) %>%
      ungroup()
    

    result

    # A tibble: 11 x 3
           A group     B
       <dbl> <int> <dbl>
     1     3     1     1
     2     3     1     1
     3     3     1     1
     4     3     1     0
     5     1     2     1
     6     1     2     0
     7     2     3     1
     8     2     3     1
     9     2     3     0
    10     2     3     0
    11     2     3     0