Vectorize vectorize vectorize! if(x[-length(x)] < x[-1]) {...}
(where x is the whole vector of entries) Bill Dunlap has posted some elegant code within the last month or 2 aimed at this sort of thing, so search on his posts in the archive. -- Bert On Tue, Jul 3, 2012 at 12:10 PM, jcrosbie <ja...@crosb.ie> wrote: > > I would like to remove a loop to speed up my code. > > I want to remove a loop which references the last row. > > In general I want to a remove a loop which looks something like this: > for 2 to number of rows in a matrix do{ > if indextrow-1 is < currentIndexRow then do something. > } > > > My R code: > > for (i in 2:length(tUnitsort$Hour)){ > ifelse(tUnitsort[i,4]>=tUnitsort[i-1,4],(tempMC > =tUnitsort[i,7]),tempMC ) #col. 4 = BlockNumber; note tests to see if the > offers have change to the next set of blocks. > ifelse(tUnitsort[i,4]>=tUnitsort[i-1,4],(tempAC > =tUnitsort[i,7]-(tUnitsort[i,8]-tUnitsort[i,9])),tempAC ) > tUnitsort$MC[i] <- tempMC > tUnitsort$AC[i] <- tempAC > tUnitsort$PercentofMC[i] <- tUnitsort$Size[i]/tempMC > tUnitsort$PercentofAC[i] <- tUnitsort$AvailableMW[i]/tempAC > } > > -- > View this message in context: > http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327.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. > -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]] ______________________________________________ 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.