[R] Lists and functions in data.frame?

jim holtman jholtman at gmail.com
Wed Dec 8 11:22:42 CET 2010


It sounds like you want to use a "list" instead of a dataframe,
especially if the elements are a different length.

> d <- list()  # initialize
> d[[length(d) + 1]] <- list()  # extend
> d[[length(d)]]$fun <- sin  # add a function
> d[[length(d) + 1]] <- list()  # extend again
> d[[length(d)]]$fun <- cos  # another function
> d[[length(d)]]$val <- 42  # some value
> d
[[1]]
[[1]]$fun
function (x)  .Primitive("sin")


[[2]]
[[2]]$fun
function (x)  .Primitive("cos")

[[2]]$val
[1] 42


On Wed, Dec 8, 2010 at 5:08 AM, Rainer M Krug <r.m.krug at gmail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi
>
> I would like to have a data.frame, where one column contains functions,
> and another one lists. i.e.:
>
> FUN <- function(l) {return(l$a+l$b+l$c}
> LIST <- list(a=1, b=2, c=3)
>
> d <- data.frame(fun=FUN, no=LIST, value=2, b=TRUE)
>
> FUN <- function(l) {return(l$a*l$b*l$c}
> LIST <- list(a=4, b=5, c=6)
>
> d <- rbind(d, data.frame(fun=FUN, no=LIST, value=2, b=FALSE)
>
> I would like
> d$FUN[1](d$l[1] == 6
> to be TRUE
>
> I want to use it to save a parameterset for a simulation model, which
> consists of functions, lists, and vectors of different length.
>
> Is this possible?
>
>
> Cheers,
>
> Rainer
>
> - --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> Biology, UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology
> Natural Sciences Building
> Office Suite 2039
> Stellenbosch University
> Main Campus, Merriman Avenue
> Stellenbosch
> South Africa
>
> Tel:        +33 - (0)9 53 10 27 44
> Cell:       +27 - (0)8 39 47 90 42
> Fax (SA):   +27 - (0)8 65 16 27 82
> Fax (D) :   +49 - (0)3 21 21 25 22 44
> Fax (FR):   +33 - (0)9 58 10 27 44
> email:      Rainer at krugs.de
>
> Skype:      RMkrug
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkz/WSIACgkQoYgNqgF2egocEwCeIErcuxDxVa8RFs1SlWiY/hQp
> T84An1XAtE3tFNKajBnGQngTig4srlIb
> =6AG3
> -----END PGP SIGNATURE-----
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list