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
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/
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}}
x <- x/rep(divs, each = 1000) Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Andre Nathan Sent: Monday, 25 February 2008 11:36 AM To: r-help at r-project.org Subject: [R] A more idiomatic way to write this 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.