How to round a value that contains exponent? - r

Using below code I'm attempting to round a value to 2 decimal places. This works as expected when exponent is not contained in value. But when exponent is contained as in :
> toround <- 1.1234e-2
> round(toround , 2)
Returned is :
[1] 0.01
When I'm expecting :
[1] 1.12e-2
How to use round function for exponent ?
Update :
> ?signif
> ?round
Appear to return same help documentation so why does signif function (thanks to answer by Evan Weissburg) work for exponent but round does not ?

Since it's in exponential notation already, you know that rounding to a decimal place is the same as rounding to significant figures + 1; rounding to the hundredths place is just getting the first three significant figures.
> signif(1.326135235e-09, 3)
[1] 1.12e-2
Of course, the round function is also going to give you the correct result.
1.1234e-2 = 0.011234, so 0.01 is a perfectly correct result rounded to two decimal places. Decimal places != significant figures.

Related

Rounding to 2 decimal places whilst retaining scientific notation in R

I am sinking output from a linear model, and trying to tidy it up as it is sinked by rounding the parameters I am interested in to 2 decimal places. This is fine for most parameters like beta or Z-score, but I am having difficulty with P-value. As although I do want to round to 2 decimal places, I mean 2 decimal places whilst retaining scientific notation.
For example:
P = 2.60699382414341e-56
round(P,2)
#[1] 0
When really what I want to print is :
#2.61e-56
Is there a means of doing this?
Try
signif(2.60699382414341e-56, digits=3)
# 2.61e-56
Use format:
> P = 2.60699382414341e-56
> format(P, digits=3)
[1] "2.61e-56"
This right here:
> P = 2.60699382414341e-56
> options("scipen"=2, "digits"=3)
> P
[1] 2.61e-56
See also:
Force R not to use exponential notation (e.g. e+10)?

R addition of large and small numbers is ignoring the smaller values

I'm encountering a problem when adding larger numbers in R. The smaller values are getting ignored and it's producing an incorrect result.
For example, I've been using a binary to decimal converter found here: Convert binary string to binary or decimal value. The penultimate step looks like this:
2^(which(rev(unlist(strsplit(as.character(MyData$Index[1]), "")) == 1))-1)
[1] 1 2 32 64 256 2048 ...
I didn't include all number for length purposes, but when these numbers are summed, they will yield the integer value of the binary number. The correct result should be 4,919,768,674,277,575,011, but R is giving me a result of 4,919,768,674,277,574,656. Notice that this number is off by 355, which is the sum of the first 5 listed numbers.
I had thought it might have to do with a integer limit, but I tested it and R can handle larger numbers than what I need. Here's an example of something I tried, which again yielded an incorrect result:
2^64
[1] 18446744073709551616 #Correct Value
2^65
[1] 36893488147419103232 #Correct Value
2^64 + 2^65
[1] 55340232221128654858 #Correct Value
2^64 + 2^65 + 1
[1] 55340232221128654858 #Incorrect Value
It seems like there's some sort of problem with precision of large number addition, but I don't know how I can fix this so that I can get the desired result.
Any help would be greatly appreciated. And I apologize if anything is formatted improperly, this is my first post on the site.
For large integers, we could use as.bigz from gmp
library(gmp)
as.bigz(2^64) + as.bigz(2^65) + 1
# Big Integer ('bigz') :
#[1] 55340232221128654849

Get exact values of floats in R?

I want to get all different float values in a sample:
unique(c(0.100000000002, 0.100000000003))
But this only returns two times 0.1 which are not unique values:
[1] 0.1 0.1
How can I list the exact values that are saved?
That's just R's default printing limit of 7 significant figures. To see the true underlying values:
print(unique(c(0.100000000002, 0.100000000003), digits=15)
To change the default behaviour, see ?options; you want something like options(digits=15).
Use sprintf...
x <- unique(c(0.100000000002, 0.100000000003))
sprintf("%.20f", x)
#[1] "0.10000000000200000294" "0.10000000000299999470"
From the help page for sprintf...
f
Double precision value, in “fixed point” decimal notation of the
form "[-]mmm.ddd". The number of decimal places ("d") is specified by
the precision: the default is 6; a precision of 0 suppresses the
decimal point. Non-finite values are converted to NA, NaN or (perhaps
a sign followed by) Inf.
Here you go
options(digits=14)
unique(c(0.100000000002, 0.100000000003))

Round numbers in output to show small near-zero quantities as zero

I would like the output of my R console to look readable. To this end, I would like R to round all my numbers to the nearest N decimal places. I have some success but it doesn't work completely:
> options(scipen=100, digits=4)
> .000000001
[1] 0.000000001
> .1
[1] 0.1
> 1.23123123123
[1] 1.231
I would like the 0.000000001 to be displayed as simply 0. How does one do this? Let me be more specific: I would like a global fix for the entire R session. I realize I can start modifying things by rounding them but it's less helpful than simply setting things for the entire session.
Look at ?options, specifically the digits and scipen options.
try
sprintf("%.4f", 0.00000001)
[1] "0.0000"
Combine what Greg Snow and Ricardo Saporta already gave you to get the right answer: options('scipen'=+20) and options('digits'=2) , combined with round(x,4) .
round(x,4) will round small near-zero quantities.
Either you round off the results of your regression once and store it:
x <- round(x, 4)
... or else yes, you have to do that every time you display the small quantity, if you don't want to store its rounded value. In your case, since you said small near-zero quantities effectively represent zero, why don't you just round it?
If for some reason you need to keep both the precise and the rounded versions, then do.
x.rounded <- round(x, 4)

How to make "pretty rounding"?

I need to do a rounding like this and convert it as a character:
as.character(round(5.9999,2))
I expect it to become 6.00, but it just gives me 6
Is there anyway that I can make it show 6.00?
Try either one of these:
> sprintf("%3.2f", round(5.9999, digits=2))
[1] "6.00
> sprintf("%3.2f", 5.999) # no round needed either
[1] "6.00
There are also formatC() and prettyNum().
To help explain what's going on - the round(5.9999, 2) call is rounding your number to the nearest hundredths place, which gives you the number (not string) very close to (or exactly equal to, if you get lucky with floating-point representations) 6.00. Then as.character() looks at that number, takes up to 15 significant digits of it (see ?as.character) in order to represent it to sufficient accuracy, and determines that only 1 significant digit is necessary. So that's what you get.
As Dirk indicated, formatC() is another option.
formatC(x = 5.999, digits = 2, format = 'f')

Resources