[R] Storing interpolation functions in R object

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Mar 4 08:41:20 CET 2004


Itay Furman wrote:

> 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
> 



You cannot store a function that way. You might want to make "func" a 
list of lists as in:


# Prepare storage place

  funcs <- vector(mode = "list", length = 3)
  names(funcs) <- cases


# Try to store interpolation functions

  for (c in cases) {
      funcs[[c]] <- vector(mode = "list", length = 3)
      for (i in 1:3) {
          funcs[[c]][[i]] <- approxfun(x, f.df[[c]][i,])
      }
  }



Uwe Ligges




More information about the R-help mailing list