importing date correctly from MySQL into R

本秂侑毒 提交于 2020-01-03 18:50:49

问题


My question is pretty much the same as this one. In short, I'm using dplyr (along with RMySQL) to import a MySQL table with a datetime column, and I want to convert it to date in R. In the question linked above, no answer was posted, only a comment by Hadley. The OP apparently found a solution based on the comment made. I understood from Hadley's comment that I could solve the issue by properly importing the column as date into R. Then there would be no need to use as.Date. However, I couldn't find how to do that with dplyr and RMySQL.

I don't know how to post a fully reproducible example, because it's a MySQL database connection, but I'll post my code:

If I try:

pedidos <-  my_db %>%
tbl("pedidos") %>%
mutate(test = as.Date(DateSent))

And then I run:

pedidos

R returns:

FUNCTION AS.DATE does not exist

Like the OP in the other question, I can workaround the issue using collect(), but then it defeats the purpose of not copying the data into memory. Here´s some info about data type in MySQL DateSent is datetime

and R sessionInfo():

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=Portuguese_Brazil.1252 
[2] LC_CTYPE=Portuguese_Brazil.1252   
[3] LC_MONETARY=Portuguese_Brazil.1252
[4] LC_NUMERIC=C                      
[5] LC_TIME=Portuguese_Brazil.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] RMySQL_0.10.11 DBI_0.5-1      dplyr_0.5.0   
[4] digest_0.6.12 

loaded via a namespace (and not attached):
[1] lazyeval_0.2.0 magrittr_1.5   R6_2.2.0      
[4] assertthat_0.1 rsconnect_0.5  tools_3.3.1   
[7] tibble_1.2     Rcpp_0.12.8  

回答1:


I've coped with the datetime column problem as well, however a short answer to say, replace your current MySQL DBI Driver with RMariaDB is the solution.

As the comments mentioned under this question, this problem has actually solved in the development upstream of RMySQL from 0.11, but the only problem is this version is not available from official CRAN repository. In another words, If you don't mind installing a package manually through devtools, installation from the RMySQL 0.11 source code is exactly an answer for this question.

But as stated officially in the readme, RMariaDB package is going to be as a replacement to the RMySQL driver. Surely, the function that Datetime Support is also included in the mainstream of RMariaDB package. What's more, the newest release of Version 1.0.6 is available from CRAN official repository.



来源:https://stackoverflow.com/questions/43762009/importing-date-correctly-from-mysql-into-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!