问题描述
我正在从数据库中检索包含大整数的列到R中(使用RJDBC dbGetQuery方法).对于一个测试用例,可以考虑以下数字
I am retrieving a column containing big integers from a data base into R (using RJDBCs dbGetQuery method). For a test case, one can consider the following numbers
1000522010609612
1000522010609613
1000522010609614
1000522010609615
1000522010609616
1000522010609617
**971000522010609612
1501000522010819466
971000522010943717
1501000522010733490**
R似乎错误地读取了内容.在R中对我可用的方式(在我使用RJDBC从数据库中读取之后)是:
R seems to be reading the contents wrongly. The way in which it is available to me in R (after I read from the database using RJDBC) is:
1000522010609612
1000522010609613
1000522010609614
1000522010609615
1000522010609616
1000522010609617
**971000522010609664
1501000522010819584
971000522010943744
1501000522010733568**
查看最后4个数字.他们错了!似乎是自动将数据转换为具有损坏的数字(对于bigints)的数据框(很好-但是).关于如何解决上述问题,特别是在使用RJDBC包使用dbGetQuery时,有什么建议吗?
See the last 4 numbers. They are wrong! It seems to be automatically converting the data into a dataframe (which is fine - but) with the corrupted numbers (for bigints). Any suggestion on how we can solve the above problem particularly when we are using dbGetQuery using RJDBC package?
推荐答案
您的数据将作为浮点数读取:
Your data is read in as floating point numbers:
DF <- read.table(text="1000522010609612
1000522010609613
1000522010609614
1000522010609615
1000522010609616
1000522010609617
971000522010609612
1501000522010819466
971000522010943717
1501000522010733490")
class(DF[,1])
#[1] "numeric"
sprintf("%20f", DF[10, 1])
#[1] "1501000522010733568.000000"
您可以将其读取为字符串并转换为大整数,也可以直接读取为大整数:
You could read it as strings and convert to big integers or read in as big integers directly:
library(bit64)
DF <- read.table(text="1000522010609612
1000522010609613
1000522010609614
1000522010609615
1000522010609616
1000522010609617
971000522010609612
1501000522010819466
971000522010943717
1501000522010733490", colClasses = "integer64")
# V1
#1 1000522010609612
#2 1000522010609613
#3 1000522010609614
#4 1000522010609615
#5 1000522010609616
#6 1000522010609617
#7 971000522010609612
#8 1501000522010819466
#9 971000522010943717
#10 1501000522010733490
对于您的数据库应用程序我无能为力,但这应该为解决您的问题提供一个起点.
I can't help you with your database application, but this should provide a starting point for solving your problem.
这篇关于RJDBC错误地从数据库表中读取bigintegers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!