Hello fellow R's, I'm sure there must be an easy way to do this. But after digging in the documentation and thinking about it for a while I couldn't figure it out. I need to get a decreasing recursive vector in. I mean something like this: if starting at 2, and ending at 6, the vector should be 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 An easy way would be to do this x <- integer(0) for (i in 5) x <- c(x, i:5) But I need to create really long vectors (where the ending value is in the order of 6500) , and using loops is way to slow. I'm looking for a vectorized method. Any help will be welcomed. Julian Julian M. Burgos Fisheries Acoustics Research Lab School of Aquatic and Fishery Science University of Washington 1122 NE Boat Street Seattle, WA 98105 Phone: 206-221-6864
On Fri, 2006-10-20 at 12:51 -0700, Julian Burgos wrote:> Hello fellow R's, > > I'm sure there must be an easy way to do this. But after digging in the > documentation and thinking about it for a while I couldn't figure it > out. I need to get a decreasing recursive vector in. I mean something > like this: if starting at 2, and ending at 6, the vector should be > > 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 > > An easy way would be to do this > > x <- integer(0) > for (i in 5) x <- c(x, i:5) > > But I need to create really long vectors (where the ending value is in > the order of 6500) , and using loops is way to slow. I'm looking for a > vectorized method. Any help will be welcomed.How about this: Range <- c(2:6)> unlist(sapply(seq(along = Range), function(x) Range[x]:max(Range)))[1] 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 Then: Range2 <- 2:6500> str(Range2)int [1:6499] 2 3 4 5 6 7 8 9 10 11 ... system.time(res <- unlist(sapply(seq(along = Range2), function(x) Range2[x]:max(Range2)))) [1] 0.492 0.136 0.647 0.000 0.000> str(res)int [1:21121750] 2 3 4 5 6 7 8 9 10 11 ... HTH, Marc Schwartz
try this: start.val <- 2 end.val <- 6500 system.time(res <- unlist(lapply(start.val:end.val, ":", to = end.val))) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting Julian Burgos <jmburgos at u.washington.edu>:> Hello fellow R's, > > I'm sure there must be an easy way to do this. But after digging in the > documentation and thinking about it for a while I couldn't figure it > out. I need to get a decreasing recursive vector in. I mean something > like this: if starting at 2, and ending at 6, the vector should be > > 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 > > An easy way would be to do this > > x <- integer(0) > for (i in 5) x <- c(x, i:5) > > But I need to create really long vectors (where the ending value is in > the order of 6500) , and using loops is way to slow. I'm looking for a > vectorized method. Any help will be welcomed. > > Julian > > Julian M. Burgos > > Fisheries Acoustics Research Lab > School of Aquatic and Fishery Science > University of Washington > > 1122 NE Boat Street > Seattle, WA 98105 > > Phone: 206-221-6864 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm