Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

r - Replacing missing data in vector with existing data

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe use na.locf...?

library(zoo)
na.locf(na.locf(zoo(data),fromLast = TRUE,na.rm = FALSE),na.rm = FALSE)
 1  2  3  4  5  6  7  8  9 10 
 5  5  5  5  5  7  7  7  7  7 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...