Skip to content
Back to formatted view

Raw Message

Message-ID: <x2r7wqhjgx.fsf@biostat.ku.dk>
Date: 2004-02-19T21:01:34Z
From: Peter Dalgaard
Subject: efficient matrix approx
In-Reply-To: <4034F277.20509@rcs.urz.tu-dresden.de>

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