how to deal with BigINT in R [duplicate] - r

This question already has answers here:
long/bigint/decimal equivalent datatype in R
(7 answers)
Closed 4 years ago.
I have a BigInt number.If I try to store it in R
R> a <- 9223372036854775807
R> a
[1] 9.223372e+18
As you can notice its loosing info of last few digits. I tried multiple other ways to solve this but no luck like increasing options(digits = 22) or changing to numeric, double, integer.
> as.integer(9223372036854775807)
[1] NA
Warning message:
NAs introduced by coercion to integer range
R> as.numeric(9223372036854775807)
[1] 9.223372e+18
R> as.double(9223372036854775807)
[1] 9.223372e+18
Can anyone help me with this problem. I want to retain the same original value. I also do not want to install any external package.

We can use as.integer64 from bit64
library(bit64)
as.integer64(as.character(a))
#integer64
#[1] 9223372036854775807

Related

Is there a way to transform a char-type vector to a numeric-type vector? [duplicate]

This question already has answers here:
as.numeric with comma decimal separators?
(7 answers)
Closed 2 months ago.
I have a character vector that stores numbers with 1 and 2 decimals. I would like to change it to a numeric vector that keeps all the decimals to be able to make mathematical computations with it.
a <- as.character(c('1,1','1,25','1,3','1,36'))
a
"1,1" "1,25" "1,3" "1,36"
a <- as.numeric(a)
Warning message:
NAs introduced by coercion
You're going to want to replace the comma with a decimal with gsub and then convert it to numeric with as.numeric
a <- as.character(c('1,1','1,25','1,3','1,36'))
as.numeric(gsub(',','\\.',a))
[1] 1.10 1.25 1.30 1.36

work with large numbers and decimals using R [duplicate]

This question already has answers here:
How to read data when some numbers contain commas as thousand separator?
(11 answers)
Closed 2 years ago.
I am currently importing a csv file that contains huge positive numbers separated by decimals in a column
tb <- read.csv("data.csv",dec = ";")
4,013,054,922
5,208,913,410
5,514,995,512
5,148,498,611
...
this data in R recognizes it as a character type and I cannot do operations
I have tried with
as.numeric(tb$large)
as.long(tb$large)
as.complex(tb$large)
but it returns rows with NA
and also try the gmp library too, to no avail
I appreciate your help
Is this what you are looking for?
d <- c('4,013,054,922','5,208,913,410',
'5,514,995,512',
'5,148,498,611')
class(d)
#> [1] "character"
library(stringr)
as.numeric(str_remove_all(d, ','))
#> [1] 4013054922 5208913410 5514995512 5148498611
Created on 2020-06-14 by the reprex package (v0.3.0)

How do I get the number of numbers in a text in R? [duplicate]

This question already has answers here:
count number of digits in a string in r
(2 answers)
Closed 3 years ago.
I want to know how many digits do I have in a text variable. For example, a function that in the text "ABC234" the answer would be 3.
I tried with this:
aa=gregexpr("[[:digit:]]+\\.*[[:digit:]]*","ABC234")
I almost have it, but honestly I still dont understand the lists, so I have no idea how to get it.
Any function? Or how to manage it with my almost-option?
Thanks
Match each digit and then take the length of the returned value:
lengths(gregexpr("\\d", "ABC234"))
## [1] 3
or replace each non-digit with a zero length string and take the length of what remains:
nchar(gsub("\\D", "", "ABC234"))
## [1] 3
As an option you can use stringi or stringr libraries as well:
stringi::stri_count('ABC234', regex = '\\d')
# [1] 3
stringr::str_count('ABC234', '\\d')
# [1] 3
You can use the dpylr and readr package as follows:
library(readr)
library(dplyr)
string = "ABC234"
parse_number(string) %>%
nchar()
[1] 3

as.numeric changes the last digits of large integers [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 5 years ago.
When I to convert large numbers formatted as character strings to numeric R changes the last digits. This also happens when I pass it the number itself.
For example:
> options(scipen = 999)
> as.numeric("3411190080123000215")
[1] 3411190080123000320
> as.numeric(3411190080123000215)
[1] 3411190080123000320
This also happens when I use other numeric functions:
> floor(3411190080123000215)
[1] 3411190080123000320
Could this be an issue with my settings?
Thank you!!
The issue is that you are not actually using integers, you are using floats.
x <- as.numeric("3411190080123000215")
is.integer(x)
However, your number is too large to be stored as an integer anyway. Check out the gmp R package for arbitrarily large integers.

R converting text to double precision [duplicate]

This question already has an answer here:
R floating point number precision being lost on coversion from character
(1 answer)
Closed 7 years ago.
How do I retain the full 16 digit precision when coercing a text to numeric in R?
My attempt below does not appear to do this...
x<-"0.501288104715059"
x<-as.double(x)
x
[1] 0.5012881
[Note this is similar to a previously asked question using as.numeric to convert a character to number but his question refers to the case of using as.double to convert a character to a number]
The code in fact does work - I just needed to set the number of digits to be displayed
x<-"0.501288104715059"
x<-as.double(x)
options(digits=16)
x
[1] 0.501288104715059
Might be useful to somebody else

Resources