Reputation: 2606
suppose I have data X, an n x m matrix, and an n-vector, time, fed into a function. In this function I want to create functions
fun1 = approxfun(time, X[,1], rule=2)
fun2 = approxfun(time, X[,2], rule=2)
. . .
funm = approxfun(time, X[,m], rule=2)
Ideally if there was something like apply that could apply approxfun, that would be great but I couldn't get apply to work. I have looked into paste and parse but I have been told that a major tenant of R is to avoid parse. List seems promising because you can make a list of functions, if there was a function that worked liked rbind or cbind does on matrices only for lists that might do the trick, but because m is arbitrary I am kind of stuck here. I'm really sorry if something similar has been asked before and I missed it.
Upvotes: 1
Views: 72
Reputation: 77096
I don't know where you went wrong with apply
, it should work fine
x <- matrix(rnorm(40), ncol=4)
time <- 1:10
lfun <- apply(x, 2, approxfun, x = time, rule = 2, method="linear")
# using those functions in plots
time2 <- seq(1, 10, length=100)
par(mfrow=c(2,2))
for(ii in 1:4){
plot(time, x[,ii])
lines(time2, lfun[[ii]](time2))
}
Upvotes: 5