使用 dplyr 重新编码 sqlite 列值
Recode sqlite column values using dplyr
我有一个非常大的数据保存为 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 是 table 中的一列并且具有这些值:-
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
我得到的错误是
Error in UseMethod("escape") :
no applicable method for 'escape' applied to an object of class "c('tbl_SQLiteConnection', 'tbl_dbi', 'tbl_sql', 'tbl_lazy', 'tbl')"
In addition: Warning message:
Named arguments ignored for SQL recode
非常感谢任何 help/explanation 解决此问题的人!
根据我对你的错误的理解,你的代码没有被 ::dbplyr()
正确地翻译回 SQL,我不确定这是否有效,但我已经尝试提供 mutate功能更像 SQL 替代 recode()
、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_
)
)
我有一个非常大的数据保存为 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 是 table 中的一列并且具有这些值:-
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
我得到的错误是
Error in UseMethod("escape") : no applicable method for 'escape' applied to an object of class "c('tbl_SQLiteConnection', 'tbl_dbi', 'tbl_sql', 'tbl_lazy', 'tbl')" In addition: Warning message: Named arguments ignored for SQL recode
非常感谢任何 help/explanation 解决此问题的人!
根据我对你的错误的理解,你的代码没有被 ::dbplyr()
正确地翻译回 SQL,我不确定这是否有效,但我已经尝试提供 mutate功能更像 SQL 替代 recode()
、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_
)
)