I have a vector (I actually just retrieved individual columns from a dataframe) which has missing data in it. I want to replace the missing data with the next available data (or previous data if the next one is unavailable) in the vector. So for instance,
data <- c(NA, NA, NA, NA, 5, NA, NA, 7, NA, NA)
should become
data <- c(5, 5, 5, 5, 5, 7, 7, 7, 7, 7)
I know this is a very specific way to fill in missing data, but I was wondering if there is an elegant solution to this. I tried using which(is.na(data))
to get the missing indices and which(!is.na(data))
to get the indices with data, but manipulating the vector even with those 2 lists of indices requires a lot of messy logic. I was wondering if I was misusing which
in any way or if there were other in-built functions or packages that would allow me to perform this logic gracefully.
Thank you for your help!
For reference, the code below works as long as the vector does not end with a bunch of NAs but I would have to add more logic to make it work with the original data set.
data <- c(NA, NA, NA, NA, 5, NA, NA, 7)
missingIndeces <- which(is.na(data))
filledIndeces <- which(!is.na(data))
if(length(missingIndeces) > 1) {
for(j in 1:length(data)) {
temp <- data[j:length(data)]
filledData <- which(!is.na(temp))
if(filledData[1] > 1)
data[j] <- temp[filledData[1]]
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…