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

RCulloch ross.culloch at dur.ac.uk
Tue Jun 1 13:51:08 CEST 2010


Hi All, 

I am trying to run a loop that will have varying numbers of rows with each
output.

Previously I have had the same number of rows so I would use (and I
appreciate that this will no doubt achieve some gasps as being thoroughly
inefficient!):

xdfrow<-(0)
xdfrow1<-(1:32)
xdfrow2<-(33:64)
xdfrow3<-(65:96)
xdfrow4<-(97:128)
xdfrow5<-(129:160)
xdfrow6<-(161:192)
xdfrow7<-(193:224)

and so on....

xdf <- matrix(999, nrow=1024, ncol=7)
xdf <- as.data.frame(xdf)
NAM <- c("NAME","ID2","DAY","BEH", "B_FALSE", "B_TRUE","TOTAL")
colnames(xdf)<-NAM

I then use this matrix and then run the loop and assign the data to each of
the xdfrows just doing +1 on each loop. (If that makes sense? Not really
important, just trying to show that I do try and solve some of my own
problems, albeit perhaps not in the best manner!)

_________

However, the data I'm working with now has a very varied number of rows
(0:2500) over a large data set and I can't work out how is best to do this.

So my loop would be:

for (i in 1:33){
	SEL_DAY<-seal_dist[seal_dist[,10]==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)]}
}

where i is day and s is the hr within the day, the loop works fine because
it prints as i expect it too. I have not given any info on the data because
I assume this is more of a method question and will be very straight forward
to most people on here!? But I am happy to post data if it is needed.

I assume I need to set up a matrix before the loop,

e.g. DIST_LOOP<-matrix(NA,1000,ncol=11)

and then I should be able to put something before the first } that allows me
to add to the matrix, but everything I have tried doesn't work

e.g. DIST_LOOP[[i]]<-SEL_HR 

Any help would be much appreciated,

Best wishes,

Ross





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



More information about the R-help mailing list