abhisarihan
abhisarihan

Reputation: 261

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]]
    }
}

Upvotes: 2

Views: 816

Answers (2)

polkas
polkas

Reputation: 4184

I want to add a next solution which using the runner r cran package.

library(runner)
> fill_run(data, run_for_first = T)
 [1] 5 5 5 5 5 5 5 7 7 7
> rev(fill_run(rev(data), run_for_first = T))
 [1] 5 5 5 5 5 7 7 7 7 7

The whole package is optimized and major of it was written in cpp. Thus offer a great efficiency.

Upvotes: 1

joran
joran

Reputation: 173577

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 

Upvotes: 7

Related Questions