嗯,使用 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 时,它会说它是一个 double ,但它会像整数一样截断小数部分:
> 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
最佳答案
我现在觉得很愚蠢,但事实证明,数字确实有小数部分,但由于选项(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()
的一切。
关于mysql - 使用 RMySQL 获取的整数列无法转换为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4383247/