[R] A more idiomatic way to write this

Rolf Turner r.turner at auckland.ac.nz
Mon Feb 25 02:49:28 CET 2008


x/rep(divs,each=1000)

	cheers,

		Rolf Turner

On 25/02/2008, at 2:36 PM, Andre Nathan wrote:

> Hello,
>
> I have a vector of 1,000,000 numbers and another vector of 1,000
> divisors. What I'd like to do is to divide the first 1,000 numbers of
> the first vector by the first divisor, then the next 1,000 by the  
> second
> divisor and so on. I came up with this, but I was wondering if  
> there is
> a more idiomatic, R-like way to write it:
>
> x <- ...
> divs <- ...
>
> for (i in seq(from = 1, to = 1000000, by = 1000)) {
>   x[i:(i - 1 + 1000)] <- x[i:(i - 1 + 1000)] / divs[i %/% 1000 + 1]
> }
>
> Any suggestions are welcome.
>
> Thanks in advance,
> Andre
>
> ______________________________________________
> R-help at 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.


######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list