Last observation carried forward by id in R

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… Read More Last observation carried forward by id in R

Last observation carried forward by id in R

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… Read More Last observation carried forward by id in R