使用 RMySQL 获取的整数列无法转换为双精度
嗯,使用 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
Well, the conversion with as.double
appears to work, but then adding a non-integral number gets truncated back to integer. For example, this is the setup:
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"
So far everything is as expected. But then when you try to add 0.5 to it, it says it's a double but it truncates the decimal part like an integer:
> 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"
The column in the MySQL database is: `total_time` int(32) default NULL
I really need to add some jitter to this vector, so this is driving me nuts. Any help would be appreciated. Finally, 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在觉得很愚蠢,但事实证明,数字实际上有小数部分,但由于我的
options(digits=3)
中的options(digits=3)
,它没有被打印出来。代码>.Rprofile。这个故事的寓意是:不要相信
print()
的一切。I feel stupid now, but this turns out to have been a case where the numbers did in fact have the decimal part, but it wasn't being printed due to
options(digits=3)
in my.Rprofile
.Moral of the story is: don't trust everything you
print()
.