R-users, Is there a function for ceiling to the nearest ten? a <- 1:10*4 a [1] 4 8 12 16 20 24 28 32 36 40 The resulting vector should look like this ("ceiling to the nearest ten") [1] 10 10 20 20 20 30 30 40 40 40 Thanks in advance Lauri
Perhaps not the most elegant but 10*ceiling(x/10) usually works for me... Cheers rob *** Want to know about Britain's birds? Try www.bto.org/birdfacts *** Dr Rob Robinson, Senior Population Biologist British Trust for Ornithology, The Nunnery, Thetford, Norfolk, IP24 2PU Ph: +44 (0)1842 750050 E: rob.robinson at bto.org Fx: +44 (0)1842 750030 W: http://www.bto.org ==== "How can anyone be enlightened, when truth is so poorly lit" ====> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Lauri Nikkinen > Sent: 14 January 2008 17:18 > To: r-help at stat.math.ethz.ch > Subject: [R] Ceiling to the nearest ten? > > R-users, > > Is there a function for ceiling to the nearest ten? > > a <- 1:10*4 > a > [1] 4 8 12 16 20 24 28 32 36 40 > > The resulting vector should look like this ("ceiling to the > nearest ten") [1] 10 10 20 20 20 30 30 40 40 40 > > Thanks in advance > Lauri > > ______________________________________________ > 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. >
A suggestion for a family of such functions: ceilGenerator <- function(num) function(x) num * ceiling(x / num) ceil10 <- ceilGenerator(10) ceil20 <- ceilGenerator(20) ceil10(1:10 * 4) ceil20(1:10 * 4) Lauri Nikkinen wrote:> > R-users, > > Is there a function for ceiling to the nearest ten? > > a <- 1:10*4 > a > [1] 4 8 12 16 20 24 28 32 36 40 > > The resulting vector should look like this ("ceiling to the nearest ten") > [1] 10 10 20 20 20 30 30 40 40 40 > > Thanks in advance > Lauri > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Ceiling-to-the-nearest-ten--tp14806582p14806624.html Sent from the R help mailing list archive at Nabble.com.
Lauri Nikkinen wrote:> R-users, > > Is there a function for ceiling to the nearest ten? > > a <- 1:10*4 > a > [1] 4 8 12 16 20 24 28 32 36 40 > > The resulting vector should look like this ("ceiling to the nearest ten") > [1] 10 10 20 20 20 30 30 40 40 40 >Divide and conquer: > a [1] 4 8 12 16 20 24 28 32 36 40 > ceiling(a/10)*10 [1] 10 10 20 20 20 30 30 40 40 40 Barry
> a <- 1:10*4 > ceiling(a/10)*10[1] 10 10 20 20 20 30 30 40 40 40>-----Original Message----- Lauri Nikkinen Is there a function for ceiling to the nearest ten? a <- 1:10*4 a [1] 4 8 12 16 20 24 28 32 36 40 The resulting vector should look like this ("ceiling to the nearest ten") [1] 10 10 20 20 20 30 30 40 40 40 Thanks in advance Lauri
Oh, yeah, too simple... Thank you all! 2008/1/14, Richard M. Heiberger <rmh at temple.edu>:> > a <- 1:10*4 > > ceiling(a/10)*10 > [1] 10 10 20 20 20 30 30 40 40 40 > > > > -----Original Message----- > Lauri Nikkinen > > Is there a function for ceiling to the nearest ten? > > a <- 1:10*4 > a > [1] 4 8 12 16 20 24 28 32 36 40 > > The resulting vector should look like this ("ceiling to the nearest ten") > [1] 10 10 20 20 20 30 30 40 40 40 > > Thanks in advance > Lauri > >
On 1/14/08, Jim Price <price_ja at hotmail.com> wrote:> > A suggestion for a family of such functions: > > > ceilGenerator <- function(num) > function(x) num * ceiling(x / num) > > > ceil10 <- ceilGenerator(10) > ceil20 <- ceilGenerator(20) > > > ceil10(1:10 * 4) > ceil20(1:10 * 4)libraray(reshape) round_any(100, 40, floor) round_any(100, 40, ceiling) round_any(101, 40) # in reshape package round_any <- function (x, accuracy, f = round) { f(x/accuracy) * accuracy } which is a pretty similar idea. Hadley -- http://had.co.nz/