Skip to content

Storing interpolation functions in R object

3 messages · Itay Furman, Uwe Ligges, Brian Ripley

#
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:
# Prepare storage place
# Try to store interpolation functions
+         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:
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
#
Itay Furman wrote:

            
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
#
A column of a data frame is a vector, and all columns should have the same 
length.  You cannot meet those basic requirements if you make a column 
class to be "function", but you can have a list of functions.

However, in your case you seem to want a 3x3 array of functions, so the 
natural structure would be a matrix and not a data frame. As in

funcs <- matrix(vector("list", 9), 3, 3)
colnames(funcs) <- cases
for (c in cases)
    for (i in 1:3)
        funcs[[i,c]] <- approxfun(x, f.df[[c]][i,])

Since this is a list, you access elements as funcs[[i,j]].
On Wed, 3 Mar 2004, Itay Furman wrote: