【问题标题】:Recode sqlite column values using dplyr使用 dplyr 重新编码 sqlite 列值
【发布时间】:2021-10-15 00:50:22
【问题描述】:

我有一个过大的数据保存为 SQLite 数据库。我需要重新编码某些列的值,但不幸的是我的代码生成了我真的无法调试的错误。这是我正在使用的代码。

library(RSQLite) 
library(inborutils)
library(dplyr)
library(dbplyr)
db <- dbConnect(SQLite(), dbname = "ukbb.sqlite")
dbListTables(db)
bd <- tbl(db, "Uk_Bb")
bd %>%
  mutate(f.19.0.0 = recode(bd, f.19.0.0, '1' = "Direct entry",
                        '2' = "Manual entry",
                        '3' = "Not performed",
                        '6' = "Not performed - equipment failure",
                        '7' = "Not performed - other reason", 
                        .default = NULL))

f.19.0.0 是表中的一列,具有以下值:-

bd %>% select(f.19.0.0)
# Source:   lazy query [?? x 1]
# Database: sqlite 3.36.0 [C:\Users\*****\Downloads\UkBB\ukbb.sqlite]
f.19.0.0
       <dbl>
1        1
2        1
3        1
4       NA
5        1
6       NA
7       NA
8        1
9        1
10        1
# ... with more rows

我得到的错误是

UseMethod("escape") 中的错误: 没有适用于“escape”的方法应用于类“c('tbl_SQLiteConnection','tbl_dbi','tbl_sql','tbl_lazy','tbl')”的对象 另外:警告信息: SQL 重新编码忽略命名参数

非常感谢任何解决此问题的帮助/解释!

【问题讨论】:

    标签: r dplyr rsqlite


    【解决方案1】:

    根据我对您的错误的了解,::dbplyr() 没有将您的代码正确地转换回 SQL,我不确定这是否可行,但我已尝试为 mutate 函数提供更像 SQL 的替代 @ 987654322@,case_when()。试一试,看看效果如何:

    library(tidyverse)
    bd %>% 
       mutate(
          f.19.0.0 = 
             case_when(
                f.19.0.0 == 1 ~ "Direct entry",
                f.19.0.0 == 2 ~ "Manual entry",
                f.19.0.0 == 3 ~ "Not performed",
                f.19.0.0 == 6 ~ "Not performed - equipment failure",
                f.19.0.0 == 7 ~  "Not performed - other reason",
                TRUE ~ NA_character_
             )
       )
    

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2018-05-11
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多