I have some longitudinal data like this:
id <- c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9)
var1 <- c(0,1,1,1,NA,2,2,NA,NA,NA,1,1,0,1,1,2,1,2,NA,1,1,2,2,NA,NA,2,NA)
var2 <- c(1,NA,NA,2,1,NA,1,1,NA,0,0,1,NA,0,1,1,0,0,0,NA,1,1,1,2,0,NA,2)
visit <- rep(1:3, 9)
dt <- as.data.frame(cbind(id, var1, var2, visit))
Each distinct id
represents a patient. visit
is the measuring cycle (i.e., each patient should be measured three times).
I would like to conduct the last observation carried forward on var1
and var2
by patient id
. I found some codes in other post. It used function from package zoo
and package gsubfn
. It worked well for one variable (see below)
dt1 <- transform(dt, var1=fn$ave(var1, id, FUN= ~ na.locf(x, na.rm = FALSE)))
I'm wondering how to do it with multiple columns? Thank you.
Using dplyr you can do
library(dplyr)
dt %>%
group_by(id) %>%
mutate(across(var1:var2, ~zoo::na.locf(.x, na.rm=FALSE)))