Drew
Drew

Reputation: 593

How to remove and fill in NAs

Let's say I have a dataframe with one observation:

a <- data.frame(x = c(1, 3, NA, 4, 21, 43, 19, 21, NA, NA, 53, 54, NA, NA, NA, NA, NA, NA))

How do I replace the NA that are sandwiched between numbers with the value above the NA (i.e. 3 and 21) and remove NAs that are not sandwiched? I need to be able to remove non-sandwiched NAs without specifying the row number because I need to do this for many dataframes with variable rows.

I want to end up with this:

a <- data.frame(x = c(1, 3, *3*, 4, 21, 43, 19, 21, *21*, *21*, 53, 54))

Now how do I do this, but instead of using the value above the NA to replace the NA, I use a value that is an average of the value above and below the NA like this:

a <- data.frame(x = c(1, 3, *3.5*, 4, 21, 43, 19, 21, *37*, *37*, 53, 54))

Upvotes: 1

Views: 61

Answers (1)

akrun
akrun

Reputation: 887148

We can use na.locf0 from zoo to fill up the NA with the previous non-NA element while creating a logical index with the cumulative sum of is.na to remove the NAs at the end

library(zoo)
i1 <- cumsum(!is.na(a$x))
na.locf0(a$x)[i1 != max(i1)| !duplicated(i1)]
#[1]  1  3  3  4 21 43 19 21 21 21 53 54

Upvotes: 1

Related Questions