[R] Missing Values

Duncan Murdoch murdoch at stats.uwo.ca
Mon Dec 6 16:36:25 CET 2004


On Mon, 6 Dec 2004 15:26:40 -0000 (GMT), nhy303 at abdn.ac.uk wrote :

>I have just started using R for my PhD.  I am importing my data from Excel
>via notepad into Word.  Unfortunately, my data has many missing values.  I
>have put '.' and this allowed me to import the data into R.  However, I
>now want to interpolate these missing values.  Please can someone give me
>some pointers as to the method/code I could use?

The approx() function does linear interpolation.

For example:

> x <- 1:10
> y <- c(1, NA, 3, NA, NA, 2, NA, NA, NA, NA)
> 
> approx(x, y, xout = x)
$x
 [1]  1  2  3  4  5  6  7  8  9 10

$y
 [1] 1.000000 2.000000 3.000000 2.666667 2.333333 2.000000       NA
NA
 [9]       NA       NA

To get it to extrapolate those values at the end, you could try "rule
= 2", but this might not do what you want...

Duncan Murdoch




More information about the R-help mailing list