R data.frame to SQL - 保留有序因子

时间:2015-02-04 15:52:17

标签: mysql sql r rmysql factors

我刚刚开始使用MySQL来处理当前在R数据帧对象中的数据。我希望能够在SQL之间进行简单的往返,以便完全重建R数据帧:

library("compare",pos=2)
library("RMySQL",pos=2)
conR <- dbConnect(MySQL(),
             user = '...',
             password = '...',
             host = '...',
             dbname='r2014')
a3 <- data.frame(x=5:1,y=letters[1:5],z=ordered(c("NEVER","ALWAYS","NEVER","SOMETIMES","NEVER"),levels=c("NEVER","SOMETIMES","ALWAYS")))
a3
dbWriteTable(conn = conR, name = 'a3', value = a3)
a4 <- dbReadTable(conn = conR, name = 'a3')
compare(a3,a4)$detailedResult
a3$z
a4$z

结果显示因子最终为字符串(列y和z),并且有序因子的排序信息丢失(列z):

> a3
  x y         z
1 5 a     NEVER
2 4 b    ALWAYS
3 3 c     NEVER
4 2 d SOMETIMES
5 1 e     NEVER
> compare(a3,a4)$detailedResult
    x     y     z 
  TRUE FALSE FALSE 
> a3$z
[1] NEVER     ALWAYS    NEVER     SOMETIMES NEVER    
Levels: NEVER < SOMETIMES < ALWAYS
> a4$z
[1] "NEVER"     "ALWAYS"    "NEVER"     "SOMETIMES" "NEVER" 
> a3$y
[1] a b c d e
Levels: a b c d e
> a4$y
[1] "a" "b" "c" "d" "e" 

在数据库中创建表a3时,是否有某种方法可以指定有序因子中的信息?

0 个答案:

没有答案