Home > Enterprise >  R Filling all observations after first occurrence with that observation in longitudinal data
R Filling all observations after first occurrence with that observation in longitudinal data

Time:09-28

I have data that is set up as on the left in this table, but I want to generate data on the right:

ID A --> ID A B
1 0 1 0 0
1 0 1 0 0
1 1 1 1 1
1 0 1 0 1
2 0 2 0 0
2 1 2 1 1
2 0 2 0 1
2 0 2 0 1
2 0 2 0 1

I want to generate the column B, which is 0 until the first occurrence of a 1 in column A, and then fill the proceeding observations with 1 from that first observation in A, by ID. I'm not even sure where to start. If anyone can help I'd appreciate it

CodePudding user response:

library(tidyverse)
df <-
  structure(list(
    D = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
    A = c(0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L)
  ),
  class = "data.frame",
  row.names = c(NA,-9L))

df %>% 
  group_by(D) %>% 
  mutate(B = cummax(A)) %>% 
  ungroup()
#> # A tibble: 9 x 3
#>       D     A     B
#>   <int> <int> <int>
#> 1     1     0     0
#> 2     1     0     0
#> 3     1     1     1
#> 4     1     0     1
#> 5     2     0     0
#> 6     2     1     1
#> 7     2     0     1
#> 8     2     0     1
#> 9     2     0     1

Created on 2021-09-28 by the reprex package (v2.0.1)

CodePudding user response:

Maybe we can try

transform(
  df,
  B =  ave(A==1,ID,FUN = cumsum)
)
  • Related