1

我最近开始使用 sqldf 包,所以我可能错过了这里的重点。
我正在尝试制作“选择案例”而不是嵌套的 ifelse()。我第一次做了一个,它工作。

    cohort$activite = 
sqldf("select case activite when 1 then 'actif a plein temps'  
                            when 2 then 'actif a temps partiel'  
                            when 3 then 'actif en milieu'  
                            ELSE 'Données manquantes' END   
        from cohort") 

然后在我的脚本中运行另一个,我收到了这个错误:

    cohort$motifs_ini=
sqldf("select case motifs_ini when 1 then 'contre indication medicale'  
                              when 2 then 'refus du patient'   
                              when 4 then 'bilan en cours'   
                              when 3 then 'autre motif'  
                              when 99 then 'Non precise'  
                              ELSE 'Données Manquantes' END  
      from cohort") 

Erreur dans sqliteExecStatement(con, statement, bind.data) :   
RS-DBI driver: (error in statement: no such table: cohort)  
De plus : Message d'avis :  
In value[[3L]](cond) :  
RAW() can only be applied to a 'raw', not a 'character'   

我不明白为什么我有这个错误。数据框存在,并且我已经使用相同类型的命令修改了一个 viariable。是因为我试图攻击同一个 data.frame 吗?
任何帮助将不胜感激。

编辑:这是我的 9213 行数据框的前 6 行。

structure(list(dure_ttt = c(1402L, 556L, 778L, 30L, 123L, 241L
), sexe = c(1L, 2L, 1L, 1L, 2L, 1L), nephro_ini1 = structure(c(68L, 
68L, 68L, 68L, 68L, 68L), .Label = c("0108NL", "0401NL", "0404NL", 
"0405NL", "0406NL", "0407NL", "0408NL", "0409NL", "0410NL", "0411NL", 
"0412NL", "0413NL", "0415NL", "0416NL", "0417NL", "0418NL", "0419NL", 
"0420NL", "0421NL", "0422NL", "0423NL", "0424NL", "0425NL", "0441NL", 
"0442NL", "0443NL", "0446NL", "0447NL", "0448NL", "0449NL", "0450NL", 
"0455NL", "0456NL", "0457NL", "0458NL", "0502NL", "0521NL", "0522NL", 
"0523NL", "0524NL", "0525NL", "0526NL", "0527NL", "0528NL", "0529NL", 
"0530NL", "0531NL", "0532NL", "0533NL", "0534NL", "0535NL", "0536NL", 
"0537NL", "0601NL", "0602NL", "0603NL", "0604NL", "0605NL", "0606NL", 
"0607NL", "0608NL", "0701NL", "0702NL", "0703NL", "0801NL", "0802NL", 
"0804NL", "0NL", "1107NL", "1108NL", "1141NL", "1145NL", "1308NL", 
"1337NL", "1352NL", "1414NL", "1415NL", "1416NL", "1417NL", "1435NL", 
"1501NL", "1502NL", "1503NL", "1505NL", "1506NL", "1507NL", "1521NL", 
"1522NL", "1523NL", "1531NL", "1535NL", "1536NL", "1540NL", "1542NL", 
"1551NL", "1552NL", "1554NL", "1558NL", "1749NL", "1801NL", "1804NL", 
"1805NL", "1806NL", "1812NL", "1813NL", "1814NL", "1817NL", "1823NL", 
"2213NL", "2215NL", "2216NL", "2307NL", "2311NL", "2314NL", "2318NL", 
"3202NL"), class = "factor"), catheter = c(1L, 1L, 1L, 0L, 1L, 
0L), urgence = c(0L, 0L, 1L, 0L, 0L, 0L), activite = c(8L, 4L, 
4L, 10L, 10L, 0L), nb_comorbidite_ini = c(2L, 6L, 6L, 3L, 2L, 
1L), presence_comorbidite_ini = c(1L, 1L, 1L, 1L, 1L, 1L), nb_acv_ini = c(1L, 
1L, 3L, 3L, 0L, 0L), presence_acv_ini = c(1L, 1L, 1L, 1L, 0L, 
0L), diabete_ini = c(0L, 0L, 0L, 0L, 0L, 0L), type_diabete_ini = c(99L, 
99L, 99L, 99L, 99L, 99L), insuline_ini = c(0L, 0L, 0L, 0L, 0L, 
0L), hta_ini = c(1L, 1L, 1L, 0L, 0L, 1L), hypercholesterolemie_ini = c(0L, 
1L, 0L, 0L, 1L, 0L), fumeur_ini = c(0L, 1L, 1L, 0L, 0L, 0L), 
    ex_fumeur_ini = c(0L, 0L, 0L, 0L, 0L, 0L), insuff_respiratoire_ini = c(0L, 
    1L, 0L, 0L, 0L, 0L), oxygenotherapie_ini = c(0L, 0L, 0L, 
    0L, 0L, 0L), insuffisance_cardiaque_ini = c(0L, 0L, 1L, 1L, 
    0L, 0L), stade_IC_ini = c(99L, 99L, 1L, 3L, 99L, 99L), infarctus_ini = c(0L, 
    0L, 1L, 1L, 0L, 0L), angor_ini = c(0L, 0L, 0L, 0L, 0L, 0L
    ), arythmie_ini = c(1L, 0L, 0L, 1L, 0L, 0L), arterite_MI_ini = c(0L, 
    1L, 1L, 0L, 0L, 0L), stade_arterite_MI_ini = c(99L, 1L, 3L, 
    99L, 99L, 99L), avc_ini = c(0L, 0L, 0L, 0L, 0L, 0L), ait_ini = c(0L, 
    0L, 0L, 0L, 0L, 0L), cancer_ini = c(0L, 0L, 0L, 0L, 1L, 0L
    ), cirrhose_ini = c(0L, 0L, 0L, 0L, 0L, 0L), cirrhose_A_ini = c(0L, 
    0L, 0L, 0L, 0L, 0L), cirrhose_BC_ini = c(0L, 0L, 0L, 0L, 
    0L, 0L), hepatite_ini = c(0L, 0L, 0L, 0L, 0L, 0L), hepatite_B_ini = c(0L, 
    0L, 0L, 0L, 0L, 0L), hepatite_C_ini = c(0L, 0L, 0L, 0L, 0L, 
    0L), VIH_ini = c(0L, 0L, 0L, 0L, 0L, 0L), sida_ini = c(0L, 
    0L, 0L, 0L, 0L, 0L), transpl_ini = c(0L, 0L, 0L, 0L, 0L, 
    0L), nb_handicap_ini = c(0L, 1L, 1L, 0L, 0L, 0L), amputat_ini = c(0L, 
    0L, 0L, 0L, 0L, 0L), hemipl_ini = c(0L, 0L, 0L, 0L, 0L, 0L
    ), cecite_ini = c(0L, 0L, 0L, 0L, 0L, 0L), compor_ini = c(0L, 
    1L, 1L, 0L, 0L, 0L), inscri_ini = c(0L, 0L, 0L, 0L, 0L, 0L
    ), motifs_ini = c(1L, 1L, 1L, 1L, 3L, 99L), hospit_ini = c(0L, 
    1L, 0L, 1L, 1L, 0L), nbr_hospit_ini = c(0, 2, 0, 1, 1, 0), 
    duree_hospit_ini = c(99L, 2L, 99L, 2L, 2L, 99L), marche_ini = c(3L, 
    3L, 99L, 1L, 3L, 99L), structure_ini = c(1L, 1L, 1L, 1L, 
    1L, 1L), methode_ini = c(1L, 1L, 1L, 1L, 1L, 1L), voie_abord_ini = c(1L, 
    3L, NA, 4L, 3L, 1L), nb_seances_ini = c(3L, 3L, NA, 3L, 3L, 
    3L), erythropoietine_ini = c(0L, 1L, 0L, 0L, 0L, 0L)), .Names = c("dure_ttt", 
"sexe", "nephro_ini1", "catheter", "urgence", "activite", "nb_comorbidite_ini", 
"presence_comorbidite_ini", "nb_acv_ini", "presence_acv_ini", 
"diabete_ini", "type_diabete_ini", "insuline_ini", "hta_ini", 
"hypercholesterolemie_ini", "fumeur_ini", "ex_fumeur_ini", "insuff_respiratoire_ini", 
"oxygenotherapie_ini", "insuffisance_cardiaque_ini", "stade_IC_ini", 
"infarctus_ini", "angor_ini", "arythmie_ini", "arterite_MI_ini", 
"stade_arterite_MI_ini", "avc_ini", "ait_ini", "cancer_ini", 
"cirrhose_ini", "cirrhose_A_ini", "cirrhose_BC_ini", "hepatite_ini", 
"hepatite_B_ini", "hepatite_C_ini", "VIH_ini", "sida_ini", "transpl_ini", 
"nb_handicap_ini", "amputat_ini", "hemipl_ini", "cecite_ini", 
"compor_ini", "inscri_ini", "motifs_ini", "hospit_ini", "nbr_hospit_ini", 
"duree_hospit_ini", "marche_ini", "structure_ini", "methode_ini", 
"voie_abord_ini", "nb_seances_ini", "erythropoietine_ini"), row.names = c(NA, 
6L), class = "data.frame")
4

2 回答 2

4

嗯,我从来没有使用过 sqldf,但只要它返回一个数据框,将第一个查询的结果分配给原始数据框的列,就会破坏其结构并且不允许其他查询。我尝试不分配整个结果,而只分配第一列并且它有效:

df2$activite<- sqldf("SELECT CASE activite 
                         WHEN 1 TEHN 'actif a plein temps'  
                         WHEN 2 TEHN 'actif a temps partiel'  
                         WHEN 3 TEHN 'actif en milieu'  
                         ELSE 'Données manquantes' END   
                      from df2")[[1]]

(其中 df2 是您的群组),然后其他查询起作用。也许有一种更规范的方式来获得你需要的东西......

于 2013-02-12T14:00:19.747 回答
3

@vodka 答案是很好的答案。sqldf 返回一个 data.frame。因此,直接分配它会损坏您的初始 data.frame (成为列表列表)。

这里只是一种返回更漂亮结果的方法。你可以用AS它来分配新的列名,然后用它来分配旧的列。

cohort <- data.frame(motifs_ini=c(1,2,3,4,99,100))
cohort$motifs_ini <- sqldf("SELECT CASE  motifs_ini
                               WHEN 1 THEN 'contre indication medicale'  
                               WHEN 2 THEN 'refus du patient'   
                               WHEN 4 THEN 'bilan en cours'   
                               WHEN 3 THEN 'autre motif'  
                               WHEN 99 THEN 'Non precise'  
                               ELSE 'Données Manquantes' 
                          END AS newcol
                          FROM cohort")$newcol


cohort
                  motifs_ini
1 contre indication medicale
2           refus du patient
3                autre motif
4             bilan en cours
5                Non precise
6         Données Manquantes
于 2013-02-12T14:21:31.867 回答