[R] create vector from indices interpolated values

e-letter inpost at gmail.com
Thu Mar 7 08:27:41 CET 2013


Readers,

Is it possible to create a plot command based upon the indices of
missing values in a data set?

dataset1<-read.table(text='
10 2
20 NA
30 5
40 7
50 NA
60 NA
70 2
80 6
90 NA
100 9
')

dataset2<-read.table(text='
0.2
0.4
0.1
0.9
0.2
0.3
1.1
0.7
0.9
0.6
0.4
')

The 'approx' function is used to obtain the interpolated values for
'NA' in dataset1.

dataset1interpolatedvalues<-approx(dataset1,y=NULL,xout=dataset1$V1[is.na(dataset1$V2)])

dataset1interpolatedvalues
$x
[1] 20 50 60 90

$y
[1] 3.500000 5.333333 3.666667 7.500000

x<-dataset1interpolatedvalues$y

How to create a vector 'y' by selecting the values in 'dataset2' using
the indices equivalent to interpolated values in 'dataset1' (i.e.
indices 2, 5, 6, 9 in dataset1)? The result of creating 'y' should be

y
0.4
0.2
0.3
0.9

Then the desired plot would be

plot(y~x)

Thanks.

--
r2151



More information about the R-help mailing list