Imagine you have a large dataset similar to the matrix M below:
M <- data.frame(code = c("001", "001", "002", "002", "003", "003"),
decr = c("x", NA, "y", "y", NA, "z"))
# M
# code decr
# 1 001 x
# 2 001 <NA>
# 3 002 y
# 4 002 y
# 5 003 <NA>
# 6 003 z
I would like to fulfill the NAs in the following intuitive form:
# code decr
# 1 001 x
# 2 001 x
# 3 002 y
# 4 002 y
# 5 003 z
# 6 003 z
How would it be possible to do this transformation optimally?