Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

Conversion of long values into double in R

I'm assigning the value below in R to var x which I read from a file and when I prints it, it shows a double format. I want to be able to store this long number without decimal because when I do a REST post the data gets posted as double. I've tried as.character(x), but then when doing POST to REST API it is treated as a string. Is there any way to keep this value as Long and not decimal?

x <- 1426643216897

print(x)
[1] 1.426643e+12

Thranks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd suggest using quote=FALSE in the print function if working with characters that you do not want enclosed. Since print also accepts a digits argument you can go either way:

> print(as.character(x), quote=FALSE)
[1] 1426643216897
> print(x,  digits=20)
[1] 1426643216897

Versus:

> print(as.character(x) )
[1] "1426643216897"

There is no R long-integer mode. You should understand that numbers with more than 9 base-10 digits are being "stored as decimals", i.e. stored with abscissa+mantissa, but the apparent increase of integer length is accomplished through printing of the exact conversion of the abscissa of the "double" to base-10 representation without the decimal point. Notice what happens if you explicitly attempt to "store as integer":

> x <- 1426643216897L
Warning message:
non-integer value 1426643216897L qualified with L; using numeric value 

If you needed to store a number with greater length than the 53 binary digits could handle, you would need to go with character storage, and then use the quote=FALSE option or use cat for output:

>  cat("test")
test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...