Round down a numeric - r

I have numeric's like this one:
a <- -1.542045
And I want to round them down (or round up the abs) to 2 digits after the decimal point.
signif(a,3) will round it down and give me 1.54 as a result but for this example the result I want is -1.55.
Any idea?

I think you are looking for floor(a * 100) / 100.
Quick Test
a <- c(-1.542045, 1.542045)
floor(a * 100) / 100
# [1] -1.55 1.54
I just noticed that you changed your question 7 hours ago. Then my answer is not doing exactly what you want (as I am assuming by "rounding down" you always want to round toward -Inf). But I have discussed this in first version of my answer. Now I am going to copy those relevant back here.
With sign(a) * ceiling(abs(a) * 100) / 100 you can round data toward Inf for positive values and -Inf for negative values.
With sign(a) * floor(abs(a) * 100) / 100, you round both positive and negative values toward 0.
A quick test
a <- c(-1.542045, 1.542045)
sign(a) * ceiling(abs(a) * 100) / 100
# [1] -1.55 1.55
sign(a) * floor(abs(a) * 100) / 100
# [1] -1.54 1.54

You misunderstand the issue. If the value is in -1.542045, it will always be.
Now you can print it to two decimals or get a two decimal char:
> print(a, digits=3)
[1] -1.54
> format(a, digits=3)
[1] "-1.54"
>
Should you really desire to create a new representation you can:
> b <- trunc(a*1e2)*1e-2
> b
[1] -1.54
>
A preferable way may be
> b <- round(a, digits=2)
> b
[1] -1.54
>

A combination of ceiling(), abs() and sign() can be used to round up the abs of the number, irrespective of its sign. Such a rounding at two decimal digits can be obtained with:
ceiling(abs(a)*100)/100*sign(a)
Example:
a <- c(-1.542045, 1.542045)
ceiling(abs(a)*100)/100*sign(a)
#[1] -1.55 1.55

Related

In R, how do I round down a decimal to the hundreth place? [duplicate]

I have numeric's like this one:
a <- -1.542045
And I want to round them down (or round up the abs) to 2 digits after the decimal point.
signif(a,3) will round it down and give me 1.54 as a result but for this example the result I want is -1.55.
Any idea?
I think you are looking for floor(a * 100) / 100.
Quick Test
a <- c(-1.542045, 1.542045)
floor(a * 100) / 100
# [1] -1.55 1.54
I just noticed that you changed your question 7 hours ago. Then my answer is not doing exactly what you want (as I am assuming by "rounding down" you always want to round toward -Inf). But I have discussed this in first version of my answer. Now I am going to copy those relevant back here.
With sign(a) * ceiling(abs(a) * 100) / 100 you can round data toward Inf for positive values and -Inf for negative values.
With sign(a) * floor(abs(a) * 100) / 100, you round both positive and negative values toward 0.
A quick test
a <- c(-1.542045, 1.542045)
sign(a) * ceiling(abs(a) * 100) / 100
# [1] -1.55 1.55
sign(a) * floor(abs(a) * 100) / 100
# [1] -1.54 1.54
You misunderstand the issue. If the value is in -1.542045, it will always be.
Now you can print it to two decimals or get a two decimal char:
> print(a, digits=3)
[1] -1.54
> format(a, digits=3)
[1] "-1.54"
>
Should you really desire to create a new representation you can:
> b <- trunc(a*1e2)*1e-2
> b
[1] -1.54
>
A preferable way may be
> b <- round(a, digits=2)
> b
[1] -1.54
>
A combination of ceiling(), abs() and sign() can be used to round up the abs of the number, irrespective of its sign. Such a rounding at two decimal digits can be obtained with:
ceiling(abs(a)*100)/100*sign(a)
Example:
a <- c(-1.542045, 1.542045)
ceiling(abs(a)*100)/100*sign(a)
#[1] -1.55 1.55

Create multiplication sequence

I'm trying to create a list of numbers from 0.001 to 1000 with multiplication of 10. So the list would be (0.001, 0.1, 10.....1000)
Is there any function for it?
a <- 0.0001
b <- 0.0001
for (i in 1:5) {
b = b*100
print(c)
a <- c(a, b)
}
Can we replace the loop here with something more simple?
It should be simple as:
0.001 * 10^(seq(0,6,2))
# > 0.001 * 10^(seq(0,6,2))
# [1] 0.001 0.100 10.000 1000.000
Alternatively, you can use the GeometricSequence function from the bsts package in such way:
GeometricSequence(5, initial.value = 0.0001, discount.factor = 100)
First argument - length (in an example it is 5) is a positive integer giving the length of the desired sequence. initial.value - the first term in the sequence.
discount.factor - the ratio between a sequence term and the preceding term.

function to get the power of a number

looking for a way to get from an floating point number the power of 10 to which it is noted
6.45e-8 - would be 8
3.21e-4 would be 4
0.013 would be 2
or minus in all
is ther e a function which would do the following
instead of multiplying with 6.45e_8 it would be at first dividing by 1e-8 and then multiply with (6.45e-8/1e8=...).
How about
floor(log10(x))
? log10 computes the log base 10, floor finds the next smaller integer.
tenexp <- function(x){c <- trunc(log10(abs(x))); return(abs(c-1*(c<0)))}
Here's the (desired?) result:
> tenexp(0.0134)
[1] 2
> tenexp(6.45e-8)
[1] 8
> tenexp(6.45e+3)
[1] 3
> tenexp(-1.28e+4)
[1] 4

R rounding off numbers like 8.829847e-07

How can I round off a number like 0.0000234889 (or in the form 8.829847e-07) to a power of ten, either below or above (whichever is my choice), ie here 0.00001 or 0.0001 ?
I tried round(...., digits=-100000) but it returns an error NaN error.
Ex: round(2e-07, digits=6) gives 0, while I would like 1e-06 and another function to give 1e-07.
# Is this what you're looking for?
# find the nearest power of ten for some number
x <- 0.0000234889 # Set test input value
y <- log10(x) # What is the fractional base ten logarithm?
yy <- round(y) # What is the nearest whole number base ten log?
xx <- 10 ^ yy # What integer power of ten is nearest the input?
print(xx)
# [1] 1e-05
The digits argument to the round() function must be positive. If you want your number to show up in scientific notation with an exponent n, just just do
round(value, 10^n)
However, this will only get you what you want up to a point. For example, you can do round(0.0000234889, 10^6) but you still get 2.34889e-05. (Notice that an exponent of 6 was specified but you got 5.)
Use options("scipen" = ) like this:
num <- 0.0000234889
> num
[1] 2.34889e-05
options("scipen" = 10)
options()$scipen
> num
[1] 0.0000234889
This will change the global option for the session. Read documentation here:https://stat.ethz.ch/R-manual/R-devel/library/base/html/options.html

Transform Numbers - Trim Integral Part from Floating Point Number

Given a column of data (of the type 39600.432, 39600.433, etc) I would like to drop the integer part of the number and keep only the decimals (transforming 39600.432 into 432, and 39600.433 into 433). How can I do this?
Let's say your column is the vector x.
> x <- c(39.456, 976.902)
> x <- x - as.integer(x)
> x
[1] 0.456 0.902
That should work. You can then just multiply by 1000 to convert the current x to integers. You will need some more processing if you want 3.9 to become 9.
> x <- 1000*x
> x
[1] 456 902
Hope the helps!
Many good answers, here's one more using regular expressions.
> g <- c(134.3412,14234.5453)
> gsub("^[^\\.]*\\.", "", g)
[1] "3412" "5453"
To strip the integral part without a subtraction or regex, you can use the modulus operator.
x <- (10000:10010)/100
x
## [1] 100.00 100.01 100.02 100.03 100.04 100.05 100.06 100.07 100.08 100.09 100.10
x %% 1
## [1] 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10
%% 1 is meaningful in R. This does leave the value as fractional, which may not be ideal for your use.
You are looking for the floor function. But you could do as.integer as well.
Here is an approach using regular expressions
g<-c(134.3412,14234.5453)
r<-regexpr("[0-9]+$",g)
as.numeric(regmatches(g,r))
This should do it:
g <- c(134.3412,14234.5453)
h <- floor(g)
g - h

Resources