[R] strange algorithm needed
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Wed Nov 1 16:17:25 CET 2006
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
More information about the R-help
mailing list