Search code examples
rdataframena

Duplicate Values From Previous Row For Same Subject in R


Assume df which looks like this:

subjectid event led1 led2
ABCD_1234 base 22.5 50.3
ABCD_1234 followup NA NA
ABCD_3456 base 11.2 -23.87
ABCD_3456 followup NA NA
ZXRT_5555 base NA -0.9
ZXRT_5555 followup NA NA
EFGH_8976 base NA NA
EFGH_8976 followup NA NA

How can one duplicate the values in rows where event is base to the followup rows for led1 and led2?

Output:

subjectid event led1 led2
ABCD_1234 base 22.5 50.3
ABCD_1234 followup 22.5 50.3
ABCD_3456 base 11.2 -23.87
ABCD_3456 followup 11.2 -23.87
ZXRT_5555 base NA -0.9
ZXRT_5555 followup NA -0.9
EFGH_8976 base NA NA
EFGH_8976 followup NA NA

Solution

  • You can use across to perform operation on all columns that starts with "led". When the event column equals "followup" and at the same time the led column is NA, fill the led column with values at event equals "base".

    library(dplyr)
    
    df %>% 
      mutate(across(starts_with("led"), ~ifelse(event == "followup" & is.na(.x), .x[event == "base"], .x)), 
             .by = subjectid)
    
    #>   subjectid    event led1   led2
    #> 1 ABCD_1234     base 22.5  50.30
    #> 2 ABCD_1234 followup 22.5  50.30
    #> 3 ABCD_3456     base 11.2 -23.87
    #> 4 ABCD_3456 followup 11.2 -23.87
    #> 5 ZXRT_5555     base   NA  -0.90
    #> 6 ZXRT_5555 followup   NA  -0.90
    #> 7 EFGH_8976     base   NA     NA
    #> 8 EFGH_8976 followup   NA     NA