I have a dataframe with a column about time and this column contains some NA
. I would like to fill these cells with the year before + 1 (if the missing cell is not the beginning of the serie). Here's a reproducible example:
df <- data.frame(x = c("A", "B", "C", "A", "B", "C"),
y = c(2000, NA, NA, 2000, 2001, 2002))
I tried to follow this post
df <- df %>%
complete(y = seq(min(y), max(y), by = "year"))
but I can't find out how to do so. Any idea?
Edit: expected output:
df <- data.frame(x = c("A", "B", "C", "A", "B", "C"),
y = c(2000, 2001, 2002, 2000, 2001, 2002))
Note: I would prefer a dplyr
solution.
Note 2 (October 23rd 2019): The three answers so far are good but quite complicated. I'm really surprised that it is not possible to do that simply (for example, having the possibility to add a lag in the fill
function would be really useful I think).