RCulloch
2010-Jun-01 11:51 UTC
[R] storing output data from a loop that has varying row numbers
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.
Ivan Calandra
2010-Jun-01 13:22 UTC
[R] storing output data from a loop that has varying row numbers
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
Joris Meys
2010-Jun-01 13:38 UTC
[R] storing output data from a loop that has varying row numbers
There's something very unlogic in your code. You have the whole time the same datafra On Tue, Jun 1, 2010 at 1:51 PM, RCulloch <ross.culloch@dur.ac.uk> wrote:> > 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. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 Joris.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]