I'm trying to filling a replicated time-series data with some missing values, and I have tried serveral methods, but none works.
The data should be like this:
Year Var
2001 1
2002 2
2003 3
2001 4
2002 5
2001 6
2003 7
What I want to get is:
Year Var
2001 1
2002 2
2003 3
2001 4
2002 5
2003 NA
2001 6
2002 NA
2003 7
I have tried merge() by first building a dataframe which includes the whole sequence I need.
yearlabel <- data.frame(Year = rep(2001:2003, 3)
df <- merge(df, yearlabel, all = T)
But the resutls had a number of length(df)*length(yearlabel) rows.
Also, I tried cbind.fill from the rowr package, it just add the NAs at the end of df. If I use
Map(merge, df, yearlabel, by = 'Year', all = T),
it would return:
Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column
Can anyone help me with this problem? Thank you very much!