[R] Help with interpolation

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 13 18:04:07 CEST 2010


On Wed, May 12, 2010 at 5:14 PM, Jorge Nieves <jorge.nieves at moorecap.com> wrote:
> I have a two Colum matrix ( I named it holder) as shown bellow. It seems
> to be three columns, but the first column contains the row names.

You can't have a matrix whose columns are different classes.

>
> I am trying to fill all  NAs by linear interpolation the known values in
> the matrix. I tried using the interpNA function, but I get an error
> message:
>
>> interpNA(holder, method = "linear")
> Error in approx(idx, x[idx], 1:n, method = method, f = f) :
>  need at least two non-NA values to interpolate
> In addition: Warning message:
> In xy.coords(x, y) : NAs introduced by coercion
>>
> I was wondering if some one could recommend another function to get the
> interpolated values?

Try this:

Lines <- "20100319 2010-03-19  1626
20100320 2010-03-20    NA
20100321 2010-03-21    NA
20100322 2010-03-22    NA
20100323 2010-03-23    NA
20100324 2010-03-24    NA
20100325 2010-03-25    NA
20100326 2010-03-26  1638
20100327 2010-03-27    NA
20100328 2010-03-28    NA
20100329 2010-03-29    NA
20100330 2010-03-30    NA
20100331 2010-03-31    NA
20100401 2010-04-01    NA
20100402 2010-04-02  1669
20100403 2010-04-03    NA
20100404 2010-04-04    NA
20100405 2010-04-05    NA
20100406 2010-04-06    NA
20100407 2010-04-07    NA
20100408 2010-04-08    NA
20100409 2010-04-09  1756
20100410 2010-04-10    NA
20100411 2010-04-11    NA
20100412 2010-04-12    NA
20100413 2010-04-13    NA
20100414 2010-04-14    NA
20100415 2010-04-15    NA
20100416 2010-04-16  1829
20100417 2010-04-17    NA
20100418 2010-04-18    NA
20100419 2010-04-19    NA
20100420 2010-04-20    NA
20100421 2010-04-21    NA
20100422 2010-04-22    NA
20100423 2010-04-23  1912
20100424 2010-04-24    NA
20100425 2010-04-25    NA
20100426 2010-04-26    NA
20100427 2010-04-27    NA
20100428 2010-04-28    NA
20100429 2010-04-29    NA
20100430 2010-04-30  1995"

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

Result is:

> na.approx(z)
2010-03-19 2010-03-20 2010-03-21 2010-03-22 2010-03-23 2010-03-24 2010-03-25
  1626.000   1627.714   1629.429   1631.143   1632.857   1634.571   1636.286
2010-03-26 2010-03-27 2010-03-28 2010-03-29 2010-03-30 2010-03-31 2010-04-01
  1638.000   1642.429   1646.857   1651.286   1655.714   1660.143   1664.571
2010-04-02 2010-04-03 2010-04-04 2010-04-05 2010-04-06 2010-04-07 2010-04-08
  1669.000   1681.429   1693.857   1706.286   1718.714   1731.143   1743.571
2010-04-09 2010-04-10 2010-04-11 2010-04-12 2010-04-13 2010-04-14 2010-04-15
  1756.000   1766.429   1776.857   1787.286   1797.714   1808.143   1818.571
2010-04-16 2010-04-17 2010-04-18 2010-04-19 2010-04-20 2010-04-21 2010-04-22
  1829.000   1840.857   1852.714   1864.571   1876.429   1888.286   1900.143
2010-04-23 2010-04-24 2010-04-25 2010-04-26 2010-04-27 2010-04-28 2010-04-29
  1912.000   1923.857   1935.714   1947.571   1959.429   1971.286   1983.143
2010-04-30
  1995.000



More information about the R-help mailing list