I have a large binary value:
longbinary<-10110101110000111
But the value getting stored in longbinary is 10110101110000112.
How can i get the correct value without any changes in the end. I tried using the int64 package but it is not available for version 3.1.2 or 3.2.
Is there a way out ?
Thanks
Though I'm just starting to play around with R, I suspect that when you are doing
longbinary<-10110101110000111
you are effective saving a decimal number worth 10e+17. So that is near the maximum resolution of numeric() in R, and surely above integer(). I think you should save it's decimal value, and just convert it to print in binary if necessary.
10110101110000111 binary = 93063 decimal = strtoi("10110101110000111", 2)
The inverse (integer to binary string) is in R.utils: intToBin
intToBin(93063) gives the string "10110101110000111":
> library(R.utils)
> intToBin(93063)
[1] "10110101110000111"
Related
I have the following number in JSON in a character array:
[1544018118438041139,162.0,38.0,0.023529414,1.0,2131230815,1]
library(jsonlite)
fromJSON(
'[1544018118438041139,162.0,38.0,0.023529414,1.0,2131230815,1]'
)
returns:
[1] 1544018118438041088.000000000000000000000
162.000000000000000000000 38.000000000000000000000 [4] 0.023529413999999998497
1.000000000000000000000 2131230815.000000000000000000000 [7] 1.000000000000000000000
I want it to return the right exact time (the big number: 1544018118438041139)
Please advise how to make it work.
This appears to be a limit in precision inherent in R itself. Consider:
x <- 1544018118438041139
as.character(x)
[1] "1544018118438041088"
So, I'm not sure that the jsonlite package is responsible for this rounding error. Based on the #Konrad comment, this is a limit in precision of the IEEE data type.
There is a workaround if all you want to do is preserve the digits in the original number. You could instead treat the data as a string:
'["1544018118438041139",162.0,38.0,0.023529414,1.0,2131230815,1]'
Then, presumably jsonlite would read in the "number" as a string, without any precision issues coming into play. But then you would still have the problem should you decide to cast to numeric in R and do math with it.
I'm trying print to console or even inspect the numbers inside my dataframe object that contains big decimal numbers with 8 decimal places such as: "1054792997932.50564756" (the class of the number is numeric)
I tried using print() and cat() and View() to inspect a single number but the only result I get back is and integer "1054792997932" and the decimal places cannot be seen unless I use sprintf("%.8f", number) but the output I get back is the wrong number:
> sprintf("%.8f", 1054792997932.50564756)
[1] "1054792997932.50561523"
So from the looks of it sprintf is not a good method to use to check or format big decimal numbers.
I'm having problems validating and working with rounding such numbers any advice/help you can provide on how to deal with numbers in R would be appreciated as I am stuck
The system setup is:
R version: 3.4.0
I use pretty standard packages:
R stats and R Utils
You can change the number of digits displayed in the console with the option "digits".
To view your current setting, type
getOption("digits")
The default setting is 7. With
options("digits" = 22)
you can change the setting. 22 is the maximum amount of digits R can display.
I have an ID variable with 20 digits. Once i read the data in R , it changes to Scientific notation and then if i write the same id to csv file, the value of ID changes.
For example , running the below code should print me the value of x as "12345678912345678912",but it prints "12345678912345679872":
Code:
options(scipen=999)
x <- 12345678912345678912
print(x)
Output:
[1] 12345678912345679872
My questions are :
1) Why it is happening ?
2) How to fix this problem ?
I know it has to do with the storage of data types in R but still i think there should be some way to deal with this problem. I hope i am clear with this question.
I don't know if this question was asked or not in so point me to a link if its a duplicate.I will remove this post
I have gone through this, so i can relate with the issue of mine, but i am unable to fix it.
Any help would be highly appreciated. Thanks
R does not by default handle integers numerically larger than 2147483647L.
If you append an L to your number (to tell R its an integer), you get:
x <- 12345678912345678912L
#Warning message:
#non-integer value 12345678912345678912L qualified with L; using numeric value
This also explains the change of the last digits as R stores the number as a double.
I think the gmp-package should be able to handle large numbers in general. You should therefore either accept the loss of precision, store them as character stings, or use a data-type from the gmp package.
To circumvent the problem due to number storing/representation, you can import your ID variable directly as character with the option colClasses, for example, if using read.csv and importing a data.frame with the ÌD column and another numeric column:
mydata<-read.csv("file.csv",colClasses=c("character","numeric"),...)
Using readr you can do
mydata <- readr::read_csv("file.csv", col_types = list(ID=col_character()))
where "ID" is the name of your ID column
I read in text from a MySQL table into and R dataframe. (using RODBC, sqlFetch). Have two questions:
How do I figure out if R has read it in as utf-8? It's character
type but what's the function to show encoding?
How do I compute the number of characters in an Unicode string in R?
The length function does not work with Unicode and always returns 1 I think.
You should be able to read the encoding (assuming it is specified) with:
Encoding(x)
The number of characters can be determined with:
nchar(x)
I want to write some integers to a binary file and I want to force them to be written in two bytes even if a few of them don't fit (trim these). I get an error when I do this (below). Is there another way to do it? Thanks!
Error in writeBin(Info_Differences, file_differences, size = 2) :
size 2 is unknown on this machine
Yeah, you probably have doubles that look like integers. Try this:
writeBin(as.integer(Info_Differences), file_differences, size = 2)
or, to check what Info_Differences really is:
typeof(Info_Differences) # double or integer?