使用 RMySQL 获取的整数列无法转换为双精度

发布于 2024-10-06 17:00:51 字数 1186 浏览 7 评论 0原文

嗯,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2024-10-13 17:00:51

我现在觉得很愚蠢,但事实证明,数字实际上有小数部分,但由于我的 options(digits=3) 中的 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() 的一切。

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.

> options(digits=10)
> head(geo$total_time + 0.5)
[1] 1586.5  165.5 5339.5 1586.5 2895.5 1178.5

Moral of the story is: don't trust everything you print().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文