[R] storing output data from a loop that has varying row numbers
Ivan Calandra
ivan.calandra at uni-hamburg.de
Tue Jun 1 15:22:04 CEST 2010
Hi,
I might be a bit tired so that I don't understand everything but I'm a
bit confused.
How would you like your DIST_LOOP matrix to look like?
What do you intend to use xdfrow1...xdfrow7? As I said, I might not be
at the best of my shape!
A sample dataset would really help to see what you have and what you
want to do (maybe using dput).
But maybe just:
DIST_LOOP[i, ]<-SEL_HR
would be enough, if you want to fill rows, as I understood.
HTH,
Ivan
Le 6/1/2010 13:51, RCulloch a écrit :
> 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
>
>
>
>
>
>
--
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