[R] Appending to list
Sarah Goslee
sarah.goslee at gmail.com
Wed Jun 22 23:42:40 CEST 2011
You lost me with the TRN and TRN_CLUSTER bits, but are you trying to
make a list of lists?
> Data_ <- list()
> SN <- 1:12
> for(sn in SN) {
+ Data_[[sn]] <- list()
+ for(i in 1:10) {
+ Data.X <- rnorm(5, 0, 10)
+ Data_[[sn]][[i]] <- Data.X
+ }}
>
> Data_[[3]][[2]]
[1] -5.687070 -2.365589 12.378359 10.797234 -3.815883
> Data_[[5]][[9]]
[1] 3.705303 -6.955137 -0.730882 2.108603 -2.898769
By the way, calling your data Data_ is generally a really bad idea.
Sarah
On Wed, Jun 22, 2011 at 4:10 PM, LCOG1 <jroll at lcog.org> wrote:
> So im here now b/c im incredibly frustrated. Please consider the following:
>
> #Try 1
>
> Data_<-list()
> Sn<-1:12
> for(sn in Sn){
> for(i in 1:10){
> Data.X <- rnorm(100,0,10)
> Data_[[paste(sn,i,sep="-")]]<-Data.X
> }
> }
>
> ##Try 2
> Data_<-list()
> Sn<-1:12
> for(sn in Sn){
> for(i in 1:10){
> Data.X <- rnorm(100,0,10)
> Data_[[sn]][[i]]<-Data.X
> }
> }
>
> In Try 1 i am able to append separately each of the 120 different
> combinations of element types. But if i want to retrieve the data i have to
> create a separate value by 're' pasting the two element names (sn,i)
> together instead of just referencing them via subscripts [[sn]][[i]]. I
> have code for some other things where this works fine and the code im
> working on currently only craps out on certain data and its all the same
> (between different element types) so i cant figure out what the deal is.
>
> Doing things the way my code is written in Try 2 i return:
>
> $TRN[[11]]
> [1] 3488.300 384592.030 33478.449 20542.162 28967.105 9667.843
> 39702.814 250780.450 55615.836 12023.944
> [11] 2060.849 3001.797 9252.429 86008.546 1209.302 26470.770
> 11700.330 7575.689 328187.431
>
> $TRN[[12]]
> [1] 2645.294 70933.649 34832.911 203584.014 7040.440 49305.850
> 53736.759 22394.943 223590.439 26145.437
> [11] 42278.920 41736.813 40478.030
>
>
> $TRN_CLUST
> [1] 0 0 0 0 0 0 0 0 0 0 0 0
>
> Where $TRN[[n]] goes from 1-12 (only show 11,12 here) and $TRN_CLUST should
> do the same but there are times when i have missing values, hence the zero,
> but it doesnt store it the same (e.g $TRN_CLUST[[1]]
> [1] 0
>
> $TRN_CLUST[[2]]
> [1] 0
>
> ....
>
> What nuance am i missing here? Hope my question and issue are clear.
> Thanks
>
> Josh
>
> ----
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list