[R] rounding up (always)
Clint Bowman
clint at ecy.wa.gov
Wed Oct 20 23:39:17 CEST 2010
x<-runif(10,1,100000) # generate 10 numbers
as.integer(log(x,10)+1)
--
Clint Bowman INTERNET: clint at ecy.wa.gov
Air Quality Modeler INTERNET: clint at math.utah.edu
Department of Ecology VOICE: (360) 407-6815
PO Box 47600 FAX: (360) 407-7534
Olympia, WA 98504-7600
On Wed, 20 Oct 2010, 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
>
> ______________________________________________
> 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