rimputationlocf

Last observation carried forward conditional on value and colums


I have a longhitudinal dataframe with a lot of missing values that looks like this.

ID = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3)
date = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
cond = c(0,0,0,1,0,0,0,0,1,0,0,0,0,0,0)
var = c(1, NA , 2, 0,NA, NA, 3, NA,0, NA, 2, NA, 1,NA,NA)
df = data.frame(ID, date, cond,var)

I would like to carry forward the last observation based on two conditions:

1) when cond=0 it should carry on the observation the higher value of the variable of interest.

2) when cond=1 it should carry forward the lower value of the variable of interest.

Does anyone have an idea on how I could do this in an elegant way?

The final dataset should look like this

ID = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3)
date = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
cond = c(0,0,0,1,0,0,0,0,1,0,0,0,0,0,0)
var = c(1, 1 , 2, 0, 0, NA, 3, 3, 0, 0,2,2,2,2,2)
final = data.frame(ID, date, cond,var)

So far I was able to carry forward the last observation, but I was unable to impose the conditions

library(zoo)
df <- df %>%
  group_by(ID) %>% 
  mutate(var = 
           na.locf(var, na.rm = F))

any suggestion is welcomed


Solution

  • This is the use of accumulate2 ie

    df%>%
       group_by(ID)%>%
       mutate(d = unlist(accumulate2(var,cond[-1],function(z,x,y) if(y) min(z,x,na.rm=TRUE) else max(z,x,na.rm=TRUE))))
    # A tibble: 15 x 5
    # Groups:   ID [3]
          ID  date  cond   var     d
       <dbl> <dbl> <dbl> <dbl> <dbl>
     1     1     1     0     1     1
     2     1     2     0    NA     1
     3     1     3     0     2     2
     4     1     4     1     0     0
     5     1     5     0    NA     0
     6     2     1     0    NA    NA
     7     2     2     0     3     3
     8     2     3     0    NA     3
     9     2     4     1     0     0
    10     2     5     0    NA     0
    11     3     1     0     2     2
    12     3     2     0    NA     2
    13     3     3     0     1     2
    14     3     4     0    NA     2
    15     3     5     0    NA     2