Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace NA with previous and next rows mean in R

Tags:

replace

r

na

How could I Replace a NA with mean of its previous and next rows in a fast manner?

  name grade
1    A    56
2    B    NA
3    C    70
4    D    96

such that B's grade would be 63.

like image 751
sia Avatar asked Apr 07 '14 15:04

sia


2 Answers

Or you may try na.approx from package zoo: "Missing values (NAs) are replaced by linear interpolation"

library(zoo)
x <- c(56, NA, 70, 96)
na.approx(x)
# [1] 56 63 70 96

This also works if you have more than one consecutive NA:

vals <- c(1, NA, NA, 7, NA, 10)
na.approx(vals) 
# [1]  1.0  3.0  5.0  7.0  8.5 10.0

na.approx is based on the base function approx, which may be used instead:

vals <- c(1, NA, NA, 7, NA, 10)
xout <- seq_along(vals)
x <- xout[!is.na(vals)]
y <- vals[!is.na(vals)]

approx(x = x, y = y, xout = xout)$y
# [1]  1.0  3.0  5.0  7.0  8.5 10.0
like image 66
Henrik Avatar answered Nov 12 '22 04:11

Henrik


Assume you have a data.frame df like this:

> df
  name grade
1    A    56
2    B    NA
3    C    70
4    D    96
5    E    NA
6    F    95

Then you can use the following:

> ind <- which(is.na(df$grade))
> df$grade[ind] <- sapply(ind, function(i) with(df, mean(c(grade[i-1], grade[i+1]))))
> df
  name grade
1    A    56
2    B    63
3    C    70
4    D    96
5    E  95.5
6    F    95
like image 21
Jilber Urbina Avatar answered Nov 12 '22 04:11

Jilber Urbina