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

Ivan Calandra ivan.calandra at uni-hamburg.de
Tue Jun 1 16:32:18 CEST 2010


Hi Ross,

I'm still really confused about your question.

Let me ask you some specific questions (maybe someone more experienced 
would understand at once, but I'm no expert; I hope I can still help 
you! In any case, I would like to understand for myself ;) )

- is "seal_dist" the name of your data.frame?
- what do you want to do with
SEL_DAY[i]=dist[i]
? What is "dist"? If I understand well, you want to replace the values 
in FROM (then TO, then DIST...) with the values from the same column 
number in dist?
- Since I still haven't understood your goal completely, I still don't 
understand why you add the column TO_ID to SEL_HR.

- In any case, a matrix cannot work because you want to store data of 
different classes in DIST_LOOP (ID is character and the others are 
numeric). You can either use a data.frame (if you really want to have 
the table-like structure, which is a list) or a list.

- Moreover, the output from dput(your data) would really help to see 
what you have!

HTH,
Ivan


Le 6/1/2010 15:32, RCulloch a écrit :
> 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
>    

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php



More information about the R-help mailing list