i need a strange algorithm that i can easily do in a loop but need o do without looping. suppose i have a vector of length y filled with zeros and a number x. then, i want a new vector which is (1,......x,1......x,1.....x,1.....y mod x ) so if y was of length 17 and x was 3, the resultant vector should be (1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2) so basically , the algorithm should repeat seq(1,x) as many times as needed to fill y but then , if the last one doesn;'t fit perfectly, it should stop whenever it hits the end of y ? rep(seq(1:x,length(y)/x) will work when there is a perfect fit but it can't handle the non fit cases. it just cuts off the non integer part of length(y)/x and then fills the vector do that the resultant vector is less than the length of y. thanks.
Try: rep(1:3, length = 17) or as.numeric(gl(3, 1, 17)) or y <- 1:17 replace(y, TRUE, 1:3) # ignore warning On 11/1/06, Mark.Leeds at morganstanley.com <Mark.Leeds at morganstanley.com> wrote:> > > i need a strange algorithm that i can easily do in a loop but need > o do without looping. > > suppose i have a vector of length y filled with zeros and a number x. > > then, i want a new vector which is (1,......x,1......x,1.....x,1.....y mod > x ) > > so if y was of length 17 and x was 3, the resultant vector should be > > (1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2) > > so basically , the algorithm should repeat seq(1,x) as many times as > needed to fill y but > then , if the last one doesn;'t fit perfectly, it should stop whenever it > hits the end of y ? > > rep(seq(1:x,length(y)/x) will work when there is a perfect fit but > it can't handle the non fit cases. it just cuts > off the non integer part of length(y)/x and then fills the vector > do that the resultant vector is less than the length of y. > > thanks. > > ______________________________________________ > 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. >
On Wed, 2006-11-01 at 09:36 -0500, Mark.Leeds at MorganStanley.com wrote:> > i need a strange algorithm that i can easily do in a loop but need > o do without looping. > > suppose i have a vector of length y filled with zeros and a number x. > > then, i want a new vector which is (1,......x,1......x,1.....x,1.....y mod > x ) > > so if y was of length 17 and x was 3, the resultant vector should be > > (1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2) > > so basically , the algorithm should repeat seq(1,x) as many times as > needed to fill y but > then , if the last one doesn;'t fit perfectly, it should stop whenever it > hits the end of y ? > > rep(seq(1:x,length(y)/x) will work when there is a perfect fit but > it can't handle the non fit cases. it just cuts > off the non integer part of length(y)/x and then fills the vector > do that the resultant vector is less than the length of y.?rep, in particular the length.out argument. Using this, your desired output is easily achieved:> Y <- rep(0, 17) > X <- 3 > Y <- rep(1:X, length.out = length(Y)) > Y[1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2> yours <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2) > all.equal(yours, Y)[1] TRUE HTH G> > thanks. > > ______________________________________________ > 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.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK [w] http://www.ucl.ac.uk/~ucfagls/ WC1E 6BT [w] http://www.freshwaters.org.uk/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
probably you want to use the 'length' argument of rep(), e.g.,
rep(1:3, length = 17)
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
----- Original Message -----
From: <Mark.Leeds at MorganStanley.com>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, November 01, 2006 3:36 PM
Subject: [R] strange algorithm needed
>
>
> i need a strange algorithm that i can easily do in a loop but need
> o do without looping.
>
> suppose i have a vector of length y filled with zeros and a number
> x.
>
> then, i want a new vector which is
> (1,......x,1......x,1.....x,1.....y mod
> x )
>
> so if y was of length 17 and x was 3, the resultant vector should be
>
> (1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2)
>
> so basically , the algorithm should repeat seq(1,x) as many times as
> needed to fill y but
> then , if the last one doesn;'t fit perfectly, it should stop
> whenever it
> hits the end of y ?
>
> rep(seq(1:x,length(y)/x) will work when there is a perfect fit but
> it can't handle the non fit cases. it just cuts
> off the non integer part of length(y)/x and then fills the vector
> do that the resultant vector is less than the length of y.
>
> thanks.
>
> ______________________________________________
> 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
Mark.Leeds at MorganStanley.com wrote:> > i need a strange algorithm that i can easily do in a loop but need > o do without looping. > > suppose i have a vector of length y filled with zeros and a number x. > > then, i want a new vector which is (1,......x,1......x,1.....x,1.....y mod > x ) > > so if y was of length 17 and x was 3, the resultant vector should be > > (1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2) > > so basically , the algorithm should repeat seq(1,x) as many times as > needed to fill y but > then , if the last one doesn;'t fit perfectly, it should stop whenever it > hits the end of y ? > > rep(seq(1:x,length(y)/x) will work when there is a perfect fit but > it can't handle the non fit cases. it just cuts > off the non integer part of length(y)/x and then fills the vector > do that the resultant vector is less than the length of y.rep(seq(1:x),length(y)/x+1)[1:length(y)] -- --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k