[R] Appending to list

LCOG1 jroll at lcog.org
Wed Jun 22 22:10:39 CEST 2011


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

--
View this message in context: http://r.789695.n4.nabble.com/Appending-to-list-tp3618093p3618093.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list