[R] Can a data.frame column contain lists/arrays?

Christos Hatzis christos at nuverabio.com
Tue Feb 13 05:58:03 CET 2007


Why do you need to use a data frame?  A list will give you the flexibility
you want:

d <- list( x=list( c(1,2), c(5,2), c(9,1) ), y=c( 1, -1, -1) )

Then you can access the individual elements 

> d$x
[[1]]
[1] 1 2

[[2]]
[1] 5 2

[[3]]
[1] 9 1

> d$y
[1]  1 -1 -1

> d$x[[1]]
[1] 1 2
  
-Christos

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Christian Convey
> Sent: Monday, February 12, 2007 11:29 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Can a data.frame column contain lists/arrays?
> 
> I'd like to have a data.frame structured something like the following:
> 
> d <- data.frame (
>    x=list( c(1,2), c(5,2), c(9,1) ),
>    y=c( 1, -1, -1)
> )
> 
> The reason is this: 'd' is the training data for a machine 
> learning algorithm.  d$x is the independent data, and d$y is 
> the dependent data.
> 
> In general my machine learning code will work where each 
> element of d$x is a vector of one or more real numbers.  So 
> for instance, the same code should work when d$x[1] = 42, or 
> when d$x[1] = (42, 3, 5).
> All that matters is that all element within d$x are 
> lists/vectors of the same length.
> 
> Does anyone know if/how I can get a data.frame set up like that?
> 
> Thanks,
> Christian
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 
>



More information about the R-help mailing list