[R] Lists of data.frame
jim holtman
jholtman at gmail.com
Sun Apr 27 02:39:01 CEST 2008
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?
More information about the R-help
mailing list