R round exponential number - r

I just trying to round in R number like:
> round(1.327076e-09)
I would like it to result in
> 1.33e-09
but results in
> 0
which function can use?

Try signif:
> signif(1.326135235e-09, digits = 3)
[1] 1.33e-09

Use signif:
x <- 1.327076e-09
signif(x,3)
[1] 1.33e-09
or sprintf:
sprintf("%.2e",x)
[1] "1.33e-09"

The function round will do rounding and you can specify the number of decimals:
x <- 1.327076e-09
round(x, 11)
[1] 1.33e-09
Rising to the challenge set by #Joris and #GavinSimpson - to use trunc on this problem, do the following:
library(plyr)
round_any(x, 1e-11, floor)
[1] 1.32e-09

Related

why does as.integer in R decrement the value?

I am doing a simple operation of multiplying a decimal number and converting it to integer but the result seems to be different than expected. Apologies if this is discussed else where, I am not able to find any straight forward answers to this
> as.integer(1190.60 * 100)
[1] 119059
EDIT:
So, I have to convert that to character and then do as.integer to get what is expected
> temp <- 1190.60
> temp2 <- 1190.60 * 100
> class(temp)
[1] "numeric"
> class(temp2)
[1] "numeric"
> as.character(temp2)
[1] "119060"
> as.integer(temp2)
[1] 119059
> as.integer(as.character(temp2))
[1] 119060
EDIT2: According to the comments, thanks #andrey-shabalin
> temp2
[1] 119060
> as.integer(temp2)
[1] 119059
> as.integer(round(temp2))
[1] 119060
EDIT3: As mentioned in the comments the question is related to behaviour of as.integer and not about floating calculations
The answer to this is "floating point error". You can see this easily by checking the following:
> temp <- 1190.60
> temp2 <- 1190.60 * 100
> temp2 - 119060
[1] -1.455192e-11
Due to floating point errors, temp2 isn't exactly 119060 but :
> sprintf("%.20f", temp2)
[1] "119059.99999999998544808477"
If you use as.integer on a float, it works the same way as trunc, i.e. it does round the float in the direction of 0. So in this case that becomes 119059.
If you convert to character using as.character(), R will make sure that it uses maximum 15 significant digits. In this example that would be "119059.999999999". The next digit is another 9, so R will round this to 119060 before conversion. I avoid this in the code above by using sprintf() instead of as.character().

Rounding Error when converting from character to numeric

I have a data.table of data numbers in character format that I am trying to convert to numeric numbers. However the issue is that the numbers are very long and I want to retain all of the numbers without any rounding from R. For examle the first 5 elements of the data.table:
> TimeO[1]
[1] "20110630224701281482"
> TimeO[2]
[1] "20110630224701281523"
> TimeO[3]
[1] "20110630224701281533"
> TimeO[4]
[1] "20110630224701281548"
> TimeO[5]
[1] "20110630224701281762"
I wrote a function to convert from a character into numeric:
convert_time_fast <- function(tim){
b <- tim - tim%/%10^12*10^12
# hhmmssffffff
ms <- b%%10^6; b <-(b-ms)/10^6
ss <- b%%10^2; b <-(b-ss)/10^2
mm <- b%%10^2; hh <-(b-mm)/10^2
# if hours>=22, subtract 24 (previous day)
hh <- hh - (hh>=22)*24
return(hh+mm/60+ss/3600+ms/(3600*10^6))
}
However the rounding occurs in R so datapoints now have the same time. See first 5 elements after converting:
TimeOC <--convert_time_fast(as.numeric(TimeO))
> TimeOC[1]
[1] 1.216311
> TimeOC[2]
[1] 1.216311
> TimeOC[3]
[1] 1.216311
> TimeOC[4]
[1] 1.216311
> TimeOC[5]
[1] 1.216311
Any help figuring this out would be greatly appreciated!
You should test to see if they are really equal (all.equal()).
Usually R limits the number of digits it prints (usually to 7), but they are still there.
See also this example:
> as.numeric("1.21631114")
[1] 1.216311
> as.numeric("1.21631118")
[1] 1.216311
> all.equal(as.numeric("1.21631114"), as.numeric("1.21631118"))
[1] "Mean relative difference: 3.288632e-08" # which indicates they're not the same

How to compare two vectors in R

I want to compare two vectors but it is not working, kindly tell me how two vectors can be compared:
x <- c(1,2,3,4)
y <- c(5,6,7,8)
if (x==y) print("same") else print("different")
Use all can work here.
> all(x==y)
[1] FALSE
> y1=c(5,6,7,8)
> all(y==y1)
[1] TRUE
EDIT
best is to use isTRUE(all.equal(x,y)) to avoid recycling
recycling
> x=c(5,6,5,6)
> y=c(5,6)
> all(x==y)
[1] TRUE
better way
> isTRUE(all.equal(x,y))
[1] FALSE
> isTRUE(all.equal(y,y1))
[1] TRUE
> x=c(5,6,5,6)
> y=c(5,6)
>isTRUE(all.equal(x,y))
[1] FALSE
When it comes to array comparison, all and any are your friends. If you do not really mean geometric vector but array of values, sort should also be necessary:
> all(sort(x)==sort(y))
Try:
x <- c(1,2,3,4)
y <- c(5,6,7,8)
if(identical(x,y)) print("identical") else print("not identical")

A more precise sum() in R for big values?

I'm trying to do a simple sum over a large column in R. The answer comes back all right, but not to the specificity that I want. For example:
> tail(x)
[,1]
[1999995,] 1999995
[1999996,] 0
[1999997,] 1999997
[1999998,] 0
[1999999,] 1999999
[2e+06,] 0
If I do a sum(x), I get:
> sum(x)
[1] 1e+12
Which is fine, but I'd like it to print out something with more significant figures like 158683269821 or something. Is there an option in sum() to specify how many sigfigs I want?
The options I wound up using were thus:
> options("scipen"=100, "digits"=4)
> sum(x)
[1] 1000000000000
> sum(x)
[1] 1000000000000
> sum(x)+1
[1] 1000000000001
> sum(x)+2
[1] 1000000000002
> sum(x)-1
[1] 999999999999

R floating point number precision being lost on coversion from character

I have a large floating point number as a character like so
x<-"5374761693.91823";
On doing
as.numeric(x);
I get the following output
5374761694
I would like to preserve the floating point nature of the number while casting.
use digits argument in print to see the actual number:
> print(as.numeric(x), digits=15)
[1] 5374761693.91823
options is another alternative:
> options(digits=16)
> as.numeric(x)
[1] 5374761693.91823
> # assignments
> options(digits=16)
> y <- as.numeric(x)
> y
[1] 5374761693.91823
z <- print(as.numeric(x), digits=15)
z

Resources