[R] list of lists question
Liaw, Andy
andy_liaw at merck.com
Wed Nov 24 04:02:35 CET 2004
> From: Karla Sartor
>
> Hello all,
> As a general programming question I can't seem to figure out
> how to make
> a list of lists in R.
> As matrices won't work as they have to be rectangular.
>
> I am sure that there is an easy solution but...
>
> the specific situation is this:
> - I have created a Tukey confidence interval table and have
> listed the
> means that are not significantly different
> - then using these not significantly different pairs I have
> created the
> groups of means that are not significantly different from each other
> the issue then is that many of these lists are subsets of other lists
> and I need to check for this.
>
> Below is a little program is illustrate the issue
>
> > a=c(1,1,1,1,1) # generate the first list
> > b=c(2,2,2) # generate a second list
> > c=c(a,b) #combine them
> > cat(c, "\n") # and print
> 1 1 1 1 1 2 2 2 # this is 1-D!!! ahh
> > d=list(a,b) # make a list of a and b
> > d # and print
> [[1]] #this is exactly
> what I want,
> but continue
> [1] 1 1 1 1 1
>
> [[2]]
> [1] 2 2 2
>
> > e=list(d,a) # now on the next
> iteration I
> need to add another list to this list of lists
> > e # and print
> [[1]] # ahh all hell has broken
> loose and this is not what I want
> [[1]][[1]] # desired result below
> [1] 1 1 1 1 1
>
> [[1]][[2]]
> [1] 2 2 2
>
>
> [[2]]
> [1] 1 1 1 1 1
>
> -------------
> desired result
>
> #wrong code but this is what I want to happen
> a=c(1,1,1,1,1,1,1)
> for(i in 1:5) {
> a=list(a,1:5)
> }
>
> output I want is (something like)
> [1] 1 1 1 1 1 1 1
> [2] 1 2 3 4 5
> [3] 1 2 3 4 5
> [4] 1 2 3 4 5
> [5] 1 2 3 4 5
> [6] 1 2 3 4 5
>
> so then I could call cat(a[1]) and get 1 1 1 1 1 1 1 1 and
> cat(a[2]) and
> get 1 2 3 4 5
>
> Anyone know the answer (hopefully simple)
Indeed: Lists are vectors. You use c() to concatenate vectors, so you also
use it for lists. E.g.,
> a <- list(rep(1, 5))
> for (i in 1:2) a <- c(a, list(1:5))
> a
[[1]]
[1] 1 1 1 1 1
[[2]]
[1] 1 2 3 4 5
[[3]]
[1] 1 2 3 4 5
Andy
> Cheers,
>
> Karla Sartor
>
>
>
> ----------------------------------------------------------
> Karla Sartor
> Montana State University - LRES
> ksartor at montana.edu
>
> ______________________________________________
> 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
>
>
More information about the R-help
mailing list