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.
Related
This question already has answers here:
Count number of distinct values in a vector
(6 answers)
Closed 1 year ago.
I am stuck on a question in R, i need to sum a list of characters from the starwars dataset, which is the starwars eye color. The question asks how many different eye colours the character have. The answer is 15, which i derived from a table (table(starwars$eyecolor), but i cannot figure the code to get to 15.
Try any of below
> length(levels(factor(starwars$eye_color)))
[1] 15
> length(unique(starwars$eye_color))
[1] 15
> sum(!duplicated(starwars$eye_color))
[1] 15
length(unique(starwars$eye_color))
This question already has answers here:
How to sort a character vector where elements contain letters and numbers?
(6 answers)
Closed 3 years ago.
I have data that looks like the following, except the numbers are out of order:
dat<-
paste("Experience",1:20,sep="_")
Basically, I am trying to sort the columns in numerical order based on the ending number to order them as the code above produces. However, when I sort the values, it sorts based on the first digit as such:
"Experience_1" "Experience_10" "Experience_11" "Experience_12"
"Experience_13" "Experience_14" "Experience_15" "Experience_16"
"Experience_17" "Experience_18" "Experience_19" "Experience_2"
"Experience_20" "Experience_3" "Experience_4" "Experience_5"
"Experience_6" "Experience_7" "Experience_8" "Experience_9"
Thoughts?
The Stringr library, a part of the tidyverse, has str_sort() which sorts strings numerically in R.
library(stringr)
str_sort(dat, numeric = TRUE)
An option would be mixedsort from gtools
gtools::mixedsort(dat)
#[1] "Experience_1" "Experience_2" "Experience_3" "Experience_4" "Experience_5" "Experience_6"
#[7] "Experience_7" "Experience_8" "Experience_9" "Experience_10" "Experience_11" "Experience_12"
#[13] "Experience_13" "Experience_14" "Experience_15" "Experience_16" "Experience_17" "Experience_18"
#[19] "Experience_19" "Experience_20"
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
This question already has answers here:
How to format a number as percentage in R?
(10 answers)
Represent numeric value with typical dollar amount format
(4 answers)
Closed 5 years ago.
I want to know if it's possible to display values on a table as percentages but still be able to do arithmetic operations with them.
I have tried using percent() from scales package, but it seems like it transforms the values from numeric to character. I have tried to convert them back using as.numeric(), but it will not work either.
Any solutions? Thank you.
One very hacky way to do this would be as follows
x <- runif(10)
class(x) <- "percent"
print.percent <- function(x) print(scales::percent(as.numeric(x)))
This is probably useful for quick analyses or short scripts but I wouldn't put this into any kind of package or shared code.
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