[R] Storing interpolation functions in R object

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Mar 4 08:54:41 CET 2004


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:

> 
> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list