Re: [R] Lists of data.frame

From: jim holtman <jholtman_at_gmail.com>
Date: Sat, 26 Apr 2008 20:39:01 -0400

I think this is what you want. You need to look at the help file for ?'[' and ?'[['. Also understand the differences between a data.frame and a list. in the case of defining qn you need to use a list and not a data.frame because here is what happens with a data.frame:

> q1 <- data.frame(q=d1, n="a")
> str(q1)
'data.frame': 1 obs. of 3 variables:

 $ q.x: num 1
 $ q.y: num 2
 $ n  : Factor w/ 1 level "a": 1

Which is probably not want you want. So the following should give you the results you want

> d1 <- data.frame(x=1, y=2)
> d2 <- data.frame(x=1, y=3)
> d3 <- data.frame(x=21, y=3)
>
>
> q1 <- list(q=d1, n="a")
> q2 <- list(q=d2, n="a")
> q3 <- list(q=d3, n="a")
>
>
> v <- list()
> v[[1]] <- q1
> v[[2]] <- q2
> v[[3]] <- q3
> str(v)
List of 3
 $ :List of 2

  ..$ q:'data.frame':   1 obs. of  2 variables:
  .. ..$ x: num 1
  .. ..$ y: num 2
  ..$ n: chr "a"

 $ :List of 2
  ..$ q:'data.frame':   1 obs. of  2 variables:
  .. ..$ x: num 1
  .. ..$ y: num 3
  ..$ n: chr "a"

 $ :List of 2
  ..$ q:'data.frame':   1 obs. of  2 variables:
  .. ..$ x: num 21
  .. ..$ y: num 3
  ..$ n: chr "a"

> #access
> v[[2]]$q$x
[1] 1

On Sat, Apr 26, 2008 at 8:04 PM, Worik R <worikr_at_gmail.com> wrote:
> I cannot find out how to build data structures as lists of data structures.
>
> I want to do...
> r-help_at_r-project.org
>
> d1 <- data.frame(x=1, y=2)
> d2 <- data.frame(x=1, y=3)
> d3 <- data.frame(x=21, y=3)
>
>
> q1 <- data.frame(q=d1, n="a")
> q2 <- data.frame(q=d2, n="a")
> q3 <- data.frame(q=d3, n="a")
>
>
> v <- vector() or list() or whatever
>
> v[1] <- q1
> v[2] <- q2
> v[3] <- q3
>
> then access d2$x as: v[2]$q$x
>
> But I cannot get any thing like this.
>
> I can find no examples in the manuals.
>
>
> cheers
> Worik
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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.
Received on Sun 27 Apr 2008 - 00:41:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 27 Apr 2008 - 01:30:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive