R converting text to double precision [duplicate] - r

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

Related

Big numbers value is changing itself [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Why are these numbers not equal?
(6 answers)
Closed last month.
Suppose I have big numbers such as this: 10.295.419.305.791.578.917.358.173.519.057.891.283.897.623.986
library(gmp)
as.bigz(10295419305791578917358173519057891283897623986)
And this might outputs:
Big Integer ('bigz') :
[1] 10295419305791579051260295299235939155628261376
Visually the value 10.295.419.305.791.579.051.260.295.299.235.939.155.628.261.376 is already different compared the input (begins after 15 digits from beginning)
My question: are there any explanation to this? is it because of numeric error before it being converted to big integer?
While I still find a way to get the accurate value by adding quotes.. such as:
as.bigz("10295419305791578917358173519057891283897623986")
Big Integer ('bigz') :
[1] 10295419305791578917358173519057891283897623986

R can not interpret big numbers correctly [duplicate]

This question already has answers here:
long/bigint/decimal equivalent datatype in R
(7 answers)
how to deal with BigINT in R [duplicate]
(1 answer)
Closed 2 years ago.
In R language, I have a problem regarding the big integers.
I type the number in the console, but the response is not equal to my input.
7241562255469626002
[1] 7241562255469626368
I also tried to set the digits option but it did not work:
options("digits" = 22)
7241562255469626002
[1] 7241562255469626368
What should I do to make it interpret the big numbers correctly?

R - substring by number [duplicate]

This question already has answers here:
Extracting numbers from vectors of strings
(12 answers)
Closed 3 years ago.
what is the most easiest way how to get number from string? I have huge list of links like this, I need to get that number 98548 from it.
https://address.com/admin/customers/98548/contacts
Note that number cant have different count of numbers and can start from 0 to 9
This is the most easiest that I know :
str <- "https://address.com/admin/customers/98548/contacts"
str_extract_all(str, "\\d+")[[1]]
Using stringr:
no="https://address.com/admin/customers/98548/contacts"
unlist(stringr::str_extract_all(no,"\\d{1,}"))
[1] "98548"

How to create a sequence of text IDs in r? [duplicate]

This question already has answers here:
How to add leading zeros?
(8 answers)
Closed 6 years ago.
My problem is to create a sequence of IDs in a vector. The vector will contain 001 to 020 then 030 to 100.
I can generate numbers by
x <- c(1:20,30:100)
but this is not in the format I am interested.
x <- c(paste("00", 1:9, sep=""),paste("0", 10:99, sep=""),100)
As suggested by the Frank... Use sprinf for formatted output. I like the %f formatter to format numbers. It is designed to format floating point numbers. %f will be replaced by the number. You can add 0 in front of the f to get leading numbers. Or you can also define how many digits you want to have overall (in your case 3) and how many should be decimal (0 after the .). Play a little with it. It is great for formatted output, filename etc.
sprintf('%03.0f', c(1:20,30:100))

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.

Resources