very similar to this questions I try to populate a new variable by finding the last non missing value by group for an existing variable in a dataframe, ideally using dplyr/zoo. I want to only keep the last value though, and not merely overwrite missings, consider the following minimal example:
df1 <- data.frame(ID = c(1, 1, 1, 2, 2,2),
date = c(1,2,3,1,2,3),
var1 = c('a', '', 'b', '','c', ''))
df2 = ## R-commands to get:
df2 <- data.frame(ID = c(1, 1, 1, 2, 2,2),
date = c(1,2,3,1,2,3),
var1 = c('b', 'b', 'b', 'c','c', 'c'))