用RMYSQL获取的整数列无法转换为double
-
10-10-2019 - |
题
好吧,与 as.double
似乎有效,但是添加一个非综合号码会被截断回整数。例如,这是设置:
geo <- fetch.data.from.mysql(...)
> head(geo$total_time)
[1] 1586 165 5339 1586 2895 1178
> typeof(geo$total_time)
[1] "integer"
> typeof(as.double(geo$total_time))
[1] "double"
到目前为止,一切都是预期的。但是,当您尝试向其添加0.5时,它说是双重的,但它像整数一样截断了小数零件:
> head(geo$total_time + 0.5)
[1] 1586 166 5340 1586 2896 1178
> head(as.double(geo$total_time) + 0.5)
[1] 1586 166 5340 1586 2896 1178
> typeof(geo$total_time + 0.5)
[1] "double"
MySQL数据库中的列是: `total_time` int(32) default NULL
我真的需要在此矢量中添加一些抖动,所以这让我发疯了。任何帮助,将不胜感激。最后,sessioninfo():
> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] RMySQL_0.7-5 DBI_0.2-5 ggplot2_0.8.8 proto_0.3-8 reshape_0.8.3 plyr_1.2.1
loaded via a namespace (and not attached):
[1] tools_2.12.0
解决方案
我现在感到很愚蠢,但是事实证明这是数字确实有小数部分的情况,但并非由于 options(digits=3)
在我的 .Rprofile
.
> options(digits=10)
> head(geo$total_time + 0.5)
[1] 1586.5 165.5 5339.5 1586.5 2895.5 1178.5
故事的寓意是:不要相信你的一切 print()
.
不隶属于 StackOverflow