[R] Replacing N.A values in a data frame

Gabor Grothendieck ggrothendieck at gmail.com
Thu Oct 14 14:51:44 CEST 2010


On Thu, Oct 14, 2010 at 4:17 AM, Santosh Srinivas
<santosh.srinivas at gmail.com> wrote:
> Hello,  I have a data frame as below ... in cases where I have N.A. I want
> to use an average of the past date and next date .. any help?
>
> 13/10/2010      A       23
> 13/10/2010      B       12
> 13/10/2010      C       124
> 14/10/2010      A       43
> 14/10/2010      B       54
> 14/10/2010      C       65
> 15/10/2010      A       43
> 15/10/2010      B       N.A.
> 15/10/2010      C       65

Assuming A, B and C refer to separate time series you can use
na.approx in zoo.

Lines <- "13/10/2010      A       23
13/10/2010      B       12
13/10/2010      C       124
14/10/2010      A       43
14/10/2010      B       54
14/10/2010      C       65
15/10/2010      A       43
15/10/2010      B       N.A.
15/10/2010      C       65"

library(zoo)

# z <- read.zoo("myfile.dat", format = "%d/%m/%Y", split = 2,
na.strings = "N.A.")
z <- read.zoo(textConnection(Lines), format = "%d/%m/%Y", split = 2,
na.strings = "N.A.")

na.approx(z)  # or na.approx(z, rule = 2)

which gives this multivariate time series in zoo:

> na.approx(z)
            A  B   C
2010-10-13 23 12 124
2010-10-14 43 54  65
2010-10-15 43 NA  65

> # or
> na.approx(z, rule = 2)
            A  B   C
2010-10-13 23 12 124
2010-10-14 43 54  65
2010-10-15 43 54  65





-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list