Rounding all possible numbers below 1 to the first decimal after zeros

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Rounding all possible numbers below 1 to the first decimal after zeros



I have numbers which vary significantly. From really small to really big. I want to round them all and do not see a proper solution for me.



Usually people round to a certain number of decimals. Simply round(x, 2) or whatever.


round(x, 2)



My situation is different.



For instance, I have very different numbers. And numbers below 1 with multiple zeros are my biggest challenge, because I do not want them to be rounded to zero.


x <- c(100.56, 100.132554444123, 10044400000.2523324,
1.2343454555424, 0.04454443, 0.0000000000000000000000543)

lapply(x, signif)



It gives me


[[1]]
[1] 100.56

[[2]]
[1] 100.133

[[3]]
[1] 10044400000

[[4]]
[1] 1.23435

[[5]]
[1] 0.0445444

[[6]]
[1] 0.0000000000000000000000543



This is almost what I need. But I would like to limit numbers below 1 with some decimals. For instance, round it the the closest meaningful decimal (first non-zero number).



I also tried a couple of other options like


prettyNum(x)

formatC(x)



but they do not resolve my question.



P.S.
Sorry for confusion.
I want to round small numbers to the closest possible decimal (not sure if I explain in properly in English, sorry).



So it should be a custom function. I will round big number with certain decimals (from 0 to 3). Something like this


x <- c(100.56, 100.132554444123, 100444.2523324, 1.2343454555424, 0.04454443, 0.0000000000000000000000543)

rounded_value <- function(value)

if (is.na(value)) return (NA)
if (value >= 100) value <- round(value,2)
if (value >= 1000) value <- round(value,0)
if (value >= 1000000) value <- round(value,3)

return (value)



lapply(x, rounded_value)

[[1]]
[1] 100.56

[[2]]
[1] 100.13

[[3]]
[1] 100444

[[4]]
[1] 1.234345

[[5]]
[1] 0.04454443

[[6]]
[1] 0.0000000000000000000000543



My problem is with two last values - 0.04454443, 0.0000000000000000000000543.
I want to round values below 1 individually. For instance, into 0.04 and 0.00000000000000000000006. Trying to find a function which will help me to do this.



Updated. Thanks to everybody. I am trying to rewrite this function in a more intuitive way. Sorry if my initial question was not exactly you see here.


rounded_value <- function(value)

if (is.na(value)) return (NA)

decimals <- 1
value_out <- value
while ( (value_out<-round(value,(decimals+1))) == 0 & decimals < 10)
decimals <- decimals + 1

if (value_out > 100) value_out <- round(value_out,1)
if (value_out > 1000) value_out <- round(value_out,0)

value_out <- as.numeric(value_out)

return (value_out)



Update 3. My best guess now is


x <- c(100.56, 100.132554444123, 100444.2523324, 1.2343454555424, 0.04454443,
0.00543)

rounded_value <- function(value)

# if (is.na(value)) return (NA)
if (value >= 100) value <- round(value,2)
if (value >= 1000) value <- round(value,0)
if (value >= 1000000) value <- round(value,3)
while (value <= 1) value <- signif(x, 10)

return (value)


rounded_value(x)



It gives me


[1] 100.56 100.13 100444.25 1.23 0.04 0.01





Can you show your expected output for each of the numbers in your example?
– Marius
Aug 6 at 1:32





do you want signif(x,digits=1)?
– Ben Bolker
Aug 6 at 1:58


signif(x,digits=1)





Why would 0.044... round down to 0.04 but 0.0...054 round up to 0.0...06?
– keithpjolley
Aug 6 at 3:13





If I understand what you are saying 0.54 -> 0.6 but 0.44 -> 0.4. Why is one rounding up and the other down?
– keithpjolley
Aug 6 at 3:24





aren't these cases what signif() is meant for?
– PavoDive
Aug 6 at 4:52


signif()




1 Answer
1



Not exactly sure what you are asking. Maybe this will work - or get you closer?


R> x <- c(100.56, 100.132554444123, 10044400000.2523324,
+ 1.2343454555424, 0.04454443, 0.0000000000000000000000543)
R> ifelse(abs(x)>1, round(x), signif(x, digits=1))
[1] 1.01000e+02 1.00000e+02 1.00444e+10 1.00000e+00 4.00000e-02 5.00000e-23





why are you loading plyr ... ?
– Ben Bolker
Aug 6 at 2:05


plyr





Ah - a first iteration used it. Will update.
– keithpjolley
Aug 6 at 3:07





@keithpjolley, added more specific example, hopefully.
– Oleksiy
Aug 6 at 3:09





@Oleksiy At that point, you may need to convert to character class to get what you want... those extra digits are floating point precision problems. See the FAQ Why are these numbers not equal? for reading on that.
– Gregor
Aug 6 at 3:22


character





R> c(1/3, 1) gives you [1] 0.3333333 1.0000000. Changing this behavior in R is beyond my skills.
– keithpjolley
Aug 6 at 3:27



R> c(1/3, 1)


[1] 0.3333333 1.0000000


R






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard