[R] rounding up (always)

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Thu Oct 21 03:59:32 CEST 2010


Thanks a lot, David - I'll try this solution.
Dimitri

On Wed, Oct 20, 2010 at 9:54 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Oct 20, 2010, at 8:38 PM, Dimitri Liakhovitski wrote:
>
>> Thank you for your help, everyone.
>> Actually, I am building a lot of graphs (in a loop) but the values on
>> the y axes from graph to graph could range from [-5; 5] to [-10,000;
>> 10,000].
>> So, I am trying to create ylim ranging from ymin to ymax such that
>> they look appropriate for the range.
>> For example, if we are taking the actual range from -4.28 to 6.45, I'd
>> like the range to be -5 to 7.
>> But if the range is from -1225 to 2248, then I'd like it to be from
>> -1500 to 2500 or from -2000 to 3000.
>> Hence, my original question.
>
> Except you did not express that desire correctly. And you still have not
> been very clear about the degree of rounding-ness needed. The first example
> above suggests to the next highest absolute value if the minimum is less
> than 10. (I didn't acheive that.)  You initially wanted the rounding to the
> next regular interval on the usual meaning of "greater than" when you wrote
> ... "always up"  ... and ... "higher" which would not lead most people to
> deliver an answer that satisfies your more recent specification.
>
> In particular any log()-ging would need to first apply an abs() function.
> See if this is any more applicable to the (new) problem statement. This
> rounds out on both the negative and positive sides to the next highest power
> of 10.
>
>> roundout3 <- function(x){k=floor(log(abs(x),10)+1);
>          sign(x)*ifelse(x==0, 0,  (floor( abs(x/10^k)+0.499999999999 )
> +1)*10^k)}
> # zero is a problem since log(0) and log(abs(0)) both return Inf.
>
>> roundout3(c(-1250,-250, -3, 0, 4, 250, 1250))
> [1] -10000  -1000    -10      0     10   1000  10000
>
> --
> David.
>
>
>
>
>> Dimitri
>>
>> On Wed, Oct 20, 2010 at 5:55 PM, Ted Harding <ted.harding at wlandres.net>
>> wrote:
>>>
>>> On 20-Oct-10 21:27:46, Duncan Murdoch wrote:
>>>>
>>>> On 20/10/2010 5:16 PM, Dimitri Liakhovitski wrote:
>>>>>
>>>>> Hello!
>>>>>
>>>>> I am trying to round the number always up - i.e., whatever the
>>>>> positive number is, I would like it to round it to the closest 10 that
>>>>> is higher than this number, the closest 100 that is higher than this
>>>>> number, etc.
>>>>>
>>>>> For example:
>>>>> x<-3241.388
>>>>>
>>>>> signif(x,1) rounds to the closest thousand, i.e., to 3,000, but I'd
>>>>> like to get 4,000 instead.
>>>>> signif(x,2) rounds to the closest hundred, i.e., to 3,200, but I'd
>>>>> like to get 3,300 instead.
>>>>> signif(x,3) rounds to the closest ten, i.e., to 3,240, but I'd like to
>>>>> get 3,250 instead.
>>>>>
>>>>> Of course, I could do:
>>>>> floor(signif(x,1)+1000)
>>>>> floor(signif(x,2)+100)
>>>>> floor(signif(x,3)+10)
>>>>>
>>>>> But it's very manual - because in the problem I am facing the numbers
>>>>> sometimes have to be rounded to a 1000, sometimes to a 100, etc.
>>>>
>>>> Write a function.  You have very particular needs, so it's unlikely
>>>> there's already one out there that matches them.
>>>> Duncan Murdoch
>>>
>>> As Duncan and Clint suggest, writing a function is straightforward:
>>> for the problem as you have stated it, on the lines of
>>>
>>>  function(x,k){floor(signif(x,k-as.integer(log(x,10)-1))) + 10^k}
>>>
>>> However, what do you *really* want to happen to 3000?
>>>
>>> Ted.
>>>
>>> --------------------------------------------------------------------
>>> E-Mail: (Ted Harding) <ted.harding at wlandres.net>
>>> Fax-to-email: +44 (0)870 094 0861
>>> Date: 20-Oct-10                                       Time: 22:55:47
>>> ------------------------------ XFMail ------------------------------
>>>
>>
>>
>>
>> --
>> Dimitri Liakhovitski
>> Ninah Consulting
>> www.ninah.com
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>



-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list