[R] Storing interpolation functions in R object

Itay Furman itayf at fhcrc.org
Thu Mar 4 07:26:56 CET 2004


Dear all,

I want to derive from a data set that I have a set of 9 
interpolation functions using approxfun() and store
them in an R object. The data has some structure that I would 
like to reflect in the storage, so ideally I would store them in 
a data.frame. So far I failed.

Here is what I tried:

# My actual data has similar structure:
> x <- 1:9
> y <- matrix(c(x*2, x*3, x*4), nr=3, nc=9)
> f.df <- list()
> cases <- c("case0", "case1", "case2")
> for (i in 1:3) {f.df[[cases[i]]] <- y*i}

# Prepare storage place
> funcs <- data.frame(NA, NA, NA)
> names(funcs) <- cases

# Try to store interpolation functions
> for (c in cases) {
+         for (i in 1:3) {
+           funcs[i,c] <- approxfun(x, f.df[[c]][i,])
+         }
+       }
Error in "[<-"(`*tmp*`, iseq, value = vjj) :
        incompatible types

# Failed to change the mode of a column:
> mode(f.df[["case0"]]) <- "function"
Error in as.function.default(x, envir) : list argument expected


My attempts to initialize a data.frame into "function" mode 
using, as.function(), led to more failures.

Is it possible to do?
and how?

Thank you for any suggestions or comments.
	Itay

--------------------------------------------------------------
itayf at fhcrc.org		Fred Hutchinson Cancer Research Center




More information about the R-help mailing list