[R] efficient matrix approx
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Thu Feb 19 22:01:34 CET 2004
Thomas Petzoldt <petzoldt at rcs.urz.tu-dresden.de> writes:
> Hello,
>
> I am looking for a highly efficient matrix version of linear
> interpolation (like approx). As an example I have data like follows:
>
> x<-data.frame(time=1:20, x=(1:20)/10, y=runif(20))
> t <- seq(1.5, 15.5 ,by=0.5)
>
> # and I found the following solution:
>
> nam <- names(x)
> app <- lapply(x[2:3],approx,x=x$time, xout=t)
> r <- c(1, 2*(1:(ncol(x)-1)))
> x.new <- as.data.frame(unlist(app[1:2], recursive=F)[r])
> names(x.new) <- nam
>
>
> As this routine should be called several times during an optimization
> procedure I wonder if there is a more direct one which I have overlooked?
I wouldn't know about efficiency, but the last bit of code is
equivalent to
app <- lapply(x[-1], approx, x=x$time, xout=t)
data.frame(time=t,lapply(app,"[[","y"))
which should at least be somewhat easier to read...
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list