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)
Cheers,
Karla Sartor
----------------------------------------------------------
Karla Sartor
Montana State University - LRES
ksartor at montana.edu
> 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 > >
Is the following more like what you want:
a=c(1,1,1,1,1) # generate the first list
b=c(2,2,2) # generate a second list
d=list(a,b) # make a list of a and b
(
e=c(d,a)
)
[[1]]
[1] 1 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 1
[[4]]
[1] 1
[[5]]
[1] 1
[[6]]
[1] 1
[[7]]
[1] 1
If no, have you read sec. 6 in "An Introduction to R" [the first
option available from help.start()]?
In this example, note that e[1] is a list with only one
attributes, namely the vector 1 1 1 1 1; e[[1]] is not a list but that
vector itself.
hope this helps. spencer graves
Karla Sartor wrote:
> 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)
>
> 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
--
Spencer Graves, PhD, Senior Development Engineer
O: (408)938-4420; mobile: (408)655-4567