[R] approxfun values

Rui Barradas ruipbarradas at sapo.pt
Thu Feb 14 12:43:37 CET 2013


Hello,

In what follows I've changed your df name to 'dat', to save some keystrokes.
approxfun returns a function, so if you want just the interpolated 
values, apply that function to the x values where y is NA.



dat <- read.table(text = "
  V1 V2
1   10  2
2   20 NA
3   30  5
4   40  7
5   50 NA
6   60 NA
7   70  2
8   80  6
9   90  9
10 100 NA
", header = TRUE)

f <- approxfun(dat)
x <- dat$V1[is.na(dat$V2)]
y <- f(x)
y


Hope this helps,

Rui Barradas

Em 14-02-2013 08:43, e-letter escreveu:
> Readers,
>
> According to the help '?approxfun', the function can be used to obtain
> the interpolated values. The following test was tried:
>
>> testinterpolation<-read.csv('test.csv',header=FALSE)
>> testinterpolation
>      V1 V2
> 1   10  2
> 2   20 NA
> 3   30  5
> 4   40  7
> 5   50 NA
> 6   60 NA
> 7   70  2
> 8   80  6
> 9   90  9
> 10 100 NA
>> testinterpolationvalues<-approxfun(testinterpolation,y=NULL)
>> testinterpolationvalues
> function (v)
> .C(C_R_approxfun, as.double(x), as.double(y), as.integer(n),
>      xout = as.double(v), as.integer(length(v)), as.integer(method),
>      as.double(yleft), as.double(yright), as.double(f), NAOK = TRUE,
>      PACKAGE = "stats")$xout
> <bytecode: 0x33ffab84>
> <environment: 0x34072eac>
>> testinterpolationvalues<-approx(testinterpolation,y=NULL)
>> testinterpolationvalues
> $x
>   [1] 10.00000 11.63265 13.26531 14.89796 16.53061 18.16327 19.79592 21.42857
>   [9] 23.06122 24.69388 26.32653 27.95918 29.59184 31.22449 32.85714 34.48980
> [17] 36.12245 37.75510 39.38776 41.02041 42.65306 44.28571 45.91837 47.55102
> [25] 49.18367 50.81633 52.44898 54.08163 55.71429 57.34694 58.97959 60.61224
> [33] 62.24490 63.87755 65.51020 67.14286 68.77551 70.40816 72.04082 73.67347
> [41] 75.30612 76.93878 78.57143 80.20408 81.83673 83.46939 85.10204 86.73469
> [49] 88.36735 90.00000
>
> $y
>   [1] 2.000000 2.244898 2.489796 2.734694 2.979592 3.224490 3.469388 3.714286
>   [9] 3.959184 4.204082 4.448980 4.693878 4.938776 5.244898 5.571429 5.897959
> [17] 6.224490 6.551020 6.877551 6.829932 6.557823 6.285714 6.013605 5.741497
> [25] 5.469388 5.197279 4.925170 4.653061 4.380952 4.108844 3.836735 3.564626
> [33] 3.292517 3.020408 2.748299 2.476190 2.204082 2.163265 2.816327 3.469388
> [41] 4.122449 4.775510 5.428571 6.061224 6.551020 7.040816 7.530612 8.020408
> [49] 8.510204 9.000000
>
> How to obtain a vector consisting _only_ of the interpolated values?
>
> It was expected that 'approx' would return both original and
> interpolated values (which the above shows) and that 'approxfun' would
> not show the original values.
>
> --
> r2151
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list