Re: [R] A more idiomatic way to write this

From: Andrew Robinson <A.Robinson_at_ms.unimelb.edu.au>
Date: Mon, 25 Feb 2008 12:48:47 +1100

How about

x <- x / rep(divs, rep(1000, 1000))

?

Cheers,

Andrew

On Sun, Feb 24, 2008 at 10:36:23PM -0300, 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.
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.

-- 
Andrew Robinson  
Department of Mathematics and Statistics            Tel: +61-3-8344-6410
University of Melbourne, VIC 3010 Australia         Fax: +61-3-8344-4599
http://www.ms.unimelb.edu.au/~andrewpr
http://blogs.mbs.edu/fishing-in-the-bay/

______________________________________________
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.
Received on Mon 25 Feb 2008 - 02:00:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 25 Feb 2008 - 02:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive