Integer column fetched with RMySQL can't be converted to double
-
10-10-2019 - |
Question
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
Solution
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()
.