# 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[] <- q1
> v[] <- q2
> v[] <- 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[]\$q\$x
 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 <- q1
> v <- q2
> v <- q3
>
> then access d2\$x as: v\$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
> 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