Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolate multiple NA values with R

I want to interpolate multiple NA values in a matrix called, tester.

This is a part of tester with only 1 column of NA values, in the whole 744x6 matrix other columns have multiple as well:

ZONEID   TIMESTAMP         U10            V10            U100          V100
1        20121022 12:00    -1.324032e+00  -2.017107e+00 -3.278166e+00  -5.880225574
1        20121022 13:00    -1.295168e+00            NA  -3.130429e+00  -6.414975148
1        20121022 14:00    -1.285004e+00            NA  -3.068829e+00  -7.101699541
1        20121022 15:00    -9.605904e-01            NA  -2.332645e+00  -7.478168285
1        20121022 16:00    -6.268261e-01 -3.057278e+00  -1.440209e+00  -8.026791079

I have installed the zoo package and used the code library(zoo). I have tried to use the na.approx function, but it returns on a linear basis:

na.approx(tester)
# Error ----> need at least two non-NA values to interpolate

na.approx(tester, rule = 2)
# Error ----> need at least two non-NA values to interpolate

na.approx(tester, x = index(tester), na.rm = TRUE, maxgap = Inf)

Afterward I tried:

Lines <- "tester"
library(zoo) 
z <- read.zoo(textConnection(Lines), index = 2)[,2] 
na.approx(z)

Again I got the same multiple NA values error. I also tried:

z <- zoo(tester)
index(Cz) <- Cz[,1]
Cz_approx <- na.approx(Cz)

Same error.

I must be doing something really stupid, but I would really appreciate your help.

like image 441
Tjeerd Luykx Avatar asked Sep 02 '14 14:09

Tjeerd Luykx


1 Answers

You may apply na.approx only on columns with at least two non-NA values. Here I use colSums on a boolean matrix to find relevant columns.

# create a small matrix
m <- matrix(data = c(NA, 1, 1, 1, 1,
                     NA, NA, 2, NA, NA,
                     NA, NA, NA, NA, 2,
                     NA, NA, NA, 2, 3),
            ncol = 5, byrow = TRUE)

m
#      [,1] [,2] [,3] [,4] [,5]
# [1,]   NA    1    1    1    1
# [2,]   NA   NA    2   NA   NA
# [3,]   NA   NA   NA   NA    2
# [4,]   NA   NA   NA    2    3

library(zoo)

# na.approx on the entire matrix does not work
na.approx(m)
# Error in approx(x[!na], y[!na], xout, ...) : 
#   need at least two non-NA values to interpolate

# find columns with at least two non-NA values
idx <- colSums(!is.na(m)) > 1
idx
# [1] FALSE FALSE  TRUE  TRUE  TRUE

# interpolate 'TRUE columns' only
m[ , idx] <- na.approx(m[ , idx])
m
#      [,1] [,2] [,3]     [,4] [,5]
# [1,]   NA    1    1 1.000000  1.0
# [2,]   NA   NA    2 1.333333  1.5
# [3,]   NA   NA   NA 1.666667  2.0
# [4,]   NA   NA   NA 2.000000  3.0
like image 58
Henrik Avatar answered Sep 20 '22 21:09

Henrik