Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Average previous and next row for missing value

I'm relatively new to R and running into some issues. I'm working with a dataframe that has missing values in certain years. For example:

year var1 var2
1972 1.3  1.4
1973 1.6  2.8
1974 2.0  1.5
1975 NA   NA
1976 1.5  2.1
1977 NA   NA
1978 1.9  1.1

For each NA, I want to take the mean of the previous and next rows. So var1 and var2 in 1975 should be 1.75 and 1.8, respectively. In 1977 they should be 1.7 and 1.6. Any ideas?

like image 537
Alex Avatar asked Oct 29 '25 12:10

Alex


1 Answers

You can use na.approx in the package zoo:

library(zoo)
df$var1 <- na.approx(df$var1)
df$var2 <- na.approx(df$var2)
##
> df
  year var1 var2
1 1972 1.30  1.4
2 1973 1.60  2.8
3 1974 2.00  1.5
4 1975 1.75  1.8
5 1976 1.50  2.1
6 1977 1.70  1.6
7 1978 1.90  1.1
  • As @Jilber pointed out, this can be done more concisely with

    df <- sapply(df, na.approx)
    
  • Per @Richard Scriven's comment, you may want to preserve the data.frame class with

    df[-1] <- lapply(df[-1], na.approx)
    

    or

    df[-1] <- vapply(df[-1], na.approx, numeric(nrow(df))) 
    

Data:

df <- read.table(
  text="year var1 var2
1972 1.3  1.4
1973 1.6  2.8
1974 2.0  1.5
1975 NA   NA
1976 1.5  2.1
1977 NA   NA
1978 1.9  1.1",
  header=TRUE)
like image 96
nrussell Avatar answered Oct 31 '25 02:10

nrussell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!