Suppose I have the data frame:
dfTest <- data.frame(year = c(1,2,3,1,3),
meanVal = c(1,2,3,1,3),
var1 = c(1,2,3,1,3),
var2 = c(NA,2,NA,1,3),
var3 = c(1,NA,NA,1,3))
> dfTest
year meanVal var1 var2 var3
1 1 1 1 NA 1
2 2 2 2 2 NA
3 3 3 3 NA NA
4 1 1 1 1 1
5 3 3 3 3 3
What I need:
> dfTest
year meanVal var1 var2 var3
1 1 1 1 1 1
2 2 2 2 2 NA
3 3 3 3 3 3
For this to happen, rows need to collapse by year. If there is an NA for a column, it should replace with the value in that column. i.e. there is only 1 value for var1 for year 1 (1). However some rows in the var1 column may have an NA. Further, if there is only an NA for a column like we see with year 2 var 3, the NA must remain.