[R] Ceiling to the nearest ten?

Greg Snow Greg.Snow at imail.org
Mon Jan 14 19:59:27 CET 2008


Well the help page for ceiling and round states that ceiling only works
on numeric vectors, but round can work on complex vectors as well.
Perhaps the extra overhead of checking for complex numbers is part of
the extra time taken by round.  

Also, ceiling rounds up to an integer, round needs to deal with the
digits specification and that will add some overhead as well.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111
 
 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Barry Rowlingson
> Sent: Monday, January 14, 2008 10:48 AM
> To: rob.robinson at bto.org
> Cc: r-help at stat.math.ethz.ch; 'Lauri Nikkinen'
> Subject: Re: [R] Ceiling to the nearest ten?
> 
> Rob Robinson wrote:
> > Perhaps not the most elegant but 10*ceiling(x/10) usually 
> works for me...
> 
>   I thought maybe adding five and rounding to the nearest -1 
> decimal places might be quicker:
> 
>   > round(a+5,-1)
>    [1] 10 10 20 20 20 30 30 40 40 40
> 
>   gives same as:
> 
>   > ceiling(a/10)*10
>    [1] 10 10 20 20 20 30 30 40 40 40
> 
> but it's not quicker:
> 
>   > system.time(for(i in 1:1000000){round(a+5,-1)})
>   [1] 17.505  0.116 19.538  0.000  0.000
>   > system.time(for(i in 1:1000000){ceiling(a/10)*10})
>   [1] 5.212 0.048 5.726 0.000 0.000
> 
> which surprised me, given that the ceiling example has a 
> multiply and a divide to do before working out the ceiling, 
> whereas the round just has an add to do before working out 
> the rounding, and I can't immediately see why ceilinging 
> should be faster than roundinging:
> 
>   > system.time(for(i in 1:1000000){ceiling(a)})
>   [1] 2.724 0.044 3.053 0.000 0.000
>   > system.time(for(i in 1:1000000){round(a)})
>   [1] 12.397  0.092 14.339  0.000  0.000
> 
>   but maybe if I think about it a bit more I'll see...
> 
> Barry
> 
> ______________________________________________
> 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.
> 




More information about the R-help mailing list