Basically, I have this dataset with missing obs. So suppose we want to fill in missing data using the previous observation's Xs, and the average change for the Xs that exist for both the current and previous observation.
I'd expect this to be a common routine. Just don't know its name.
Added below is a 'manual' example:
data <- data.frame(A=1:4,
B=c(21, 18, NA, NA),
C=c(5:7,NA))
data
A B C
1 1 21 5
2 2 18 6
3 3 NA 7
4 4 NA NA
for(i in 2:nrow(data)){
for(j in 1:ncol(data)){
if(is.na(data[i,j])){
data[i,j] <- data[i-1,j]*
(sum(data[i,],na.rm=T))/sum(as.integer(!is.na(data[i,]))*data[i-1,])
}
}
}
data
A B C
1 1 21.0 5.000000
2 2 18.0 6.000000
3 3 22.5 7.000000
4 4 30.0 9.333333
I can do it manually like provided. Just assuming it already exists.
I've updated a previous version, so that now it would to only affect NAs. Should be OK now.