[R] storing output data from a loop that has varying row numbers

RCulloch ross.culloch at dur.ac.uk
Tue Jun 1 15:32:22 CEST 2010


Hi Ivan, 

Thanks for your help, your initial suggestion did not work, but that is no
doubt down to my lack of making sense! 

Here is a short example of my dataset. Basically the loop is set up to match
the ID with the TO column based on DIST = 0. So A1 = 2, A1.1 =1, A2 = 4,
A2.1 = 3. That is fine for HR 9, but for HR 10 the numbers no longer match
those IDs so I need to loop the data and store each loop - if that makes
sense. 


  FROM TO     DIST      ID HR DD MM YY ANIMAL DAY
1     1  1  2.63981    'A1'  9 30  9  7      1   1
2     1  2  0.00000    'A1'  9 30  9  7      1   1
3     1  3  6.95836    'A1'  9 30  9  7      1   1
4     1  4  8.63809    'A1'  9 30  9  7      1   1
5     1  1  0.00000  'A1.1'  9 30  9  7      7   1
6     1  2  2.63981  'A1.1'  9 30  9  7      7   1
7     1  3  8.03071  'A1.1'  9 30  9  7      7   1
8     1  4  8.90896  'A1.1'  9 30  9  7      7   1
9     1  1  8.90896    'A2'  9 30  9  7      1   1
10    1  2  8.63809    'A2'  9 30  9  7      1   1
11    1  3  2.85602    'A2'  9 30  9  7      1   1
12    1  4  0.00000    'A2'  9 30  9  7      1   1
13    1  1  8.03071  'A2.1'  9 30  9  7      7   1
14    1  2  6.95836  'A2.1'  9 30  9  7      7   1
15    1  3  0.00000  'A2.1'  9 30  9  7      7   1
16    1  4  2.85602   A2.1'  9 30  9  7      7   1
17    1  1  3.53695    'A1' 10 30  9  7      1   1
18    1  2  4.32457    'A1' 10 30  9  7      1   1
19    1  3  0.00000    'A1' 10 30  9  7      1   1
20    1  4  8.85851    'A1' 10 30  9  7      1   1
21    1  5 12.09194    'A1' 10 30  9  7      1   1
22    1  1  7.44743  'A1.1' 10 30  9  7      7   1
23    1  2  0.00000  'A1.1' 10 30  9  7      7   1
24    1  3  4.32457  'A1.1' 10 30  9  7      7   1
25    1  4 13.16728  'A1.1' 10 30  9  7      7   1
26    1  5 16.34761  'A1.1' 10 30  9  7      7   1
27    1  1  6.13176    'A2' 10 30  9  7      1   1
28    1  2 13.16728    'A2' 10 30  9  7      1   1
29    1  3  8.85851    'A2' 10 30  9  7      1   1
30    1  4  0.00000    'A2' 10 30  9  7      1   1
31    1  5  3.40726    'A2' 10 30  9  7      1   1
32    1  1  9.03345  'A2.1' 10 30  9  7      7   1
33    1  2 16.34761  'A2.1' 10 30  9  7      7   1
34    1  3 12.09194  'A2.1' 10 30  9  7      7   1
35    1  4  3.40726  'A2.1' 10 30  9  7      7   1
36    1  5  0.00000  'A2.1' 10 30  9  7      7   1
37    1  1  0.00000 'MALE1' 10 30  9  7     12   1
38    1  2  7.44743 'MALE1' 10 30  9  7     12   1
39    1  3  3.53695 'MALE1' 10 30  9  7     12   1
40    1  4  6.13176 'MALE1' 10 30  9  7     12   1
41    1  5  9.03345 'MALE1' 10 30  9  7     12   1


So the loop is:

DIST_LOOP<-matrix(NA,NA,ncol=11)

for (i in 1:33){
	SEL_DAY<-seal_dist[seal_dist[,10]==i,]
	SEL_DAY[i]=dist[i]
	print(paste("DAY", i, "of 33"))	
	for (s in 1:11){
			SEL_HR<-SEL_DAY[SEL_DAY[,5]==s,]
	print(paste("HR", s, "of 11"))		
		indx <- subset(SEL_HR, SEL_HR$DIST == 0)
		SEL_HR$TO_ID <- indx$ID[match(SEL_HR$TO, indx$TO)]
	DIST_LOOP[i,]<-SEL_HR
	}
	}	

But storing the data in the DIST_LOOP matrix doesn't work, I am just told in
another post that a list might be better than a matrix? 

I hope this makes more sense!? 

Many thanks,

Ross
-- 
View this message in context: http://r.789695.n4.nabble.com/storing-output-data-from-a-loop-that-has-varying-row-numbers-tp2238396p2238483.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list