使用 dplyr 进行 SQL 数据库内操作时的 ifelse 和 grepl 命令
ifelse & grepl commands when using dplyr for SQL in-db operations
在dplyr 运行ning上R数据帧,很容易运行
df <- df %>%
mutate(income_topcoded = ifelse(income > topcode, income, topcode)
我现在正在使用大型 SQL 数据库,使用 dplyr 将命令发送到 SQL 服务器。当我 运行 相同的命令时,我返回
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR:
function ifelse (boolean, numeric, numeric) does not exist
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
您建议如何实施 ifelse()
语句?我对 PivotalR 中的某些东西很满意(它似乎支持 ifelse()
,但我不知道如何将它与 dplyr 集成并且在 SO 上找不到任何示例),一些 SQL 我可以在这里直接使用的语法,或者我不知道的 dplyr 的一些特性。
(我有同样的问题,我想使用 grepl()
作为数据库内操作,但我不知道该怎么做。)
我遇到了类似的问题。我能做的最好的就是按照你的建议使用数据库内操作:
topcode <- 10000
queryString <- sprintf("UPDATE db.table SET income_topcoded = %s WHERE income_topcoded > %s",topcode,topcode)
dbGetQuery(con, queryString)
就我而言,我将 MySQL 与 dplyr
一起使用,但无法将我的 ifelse()
转换为有效的 SQL。
根据@hadley 对 this thread 的回复,您可以在 dplyr 的 in-db 数据帧上的 mutate()
中使用 SQL 风格的 if()
语句:
df <- df %>%
mutate( income_topcoded = if (income > topcode) income else topcode)
就使用 grepl()
而言......好吧,你不能。但是您可以使用 SQL like
运算符:
df <- df %>%
filter( topcode %like% "ABC%" )
在dplyr 运行ning上R数据帧,很容易运行
df <- df %>%
mutate(income_topcoded = ifelse(income > topcode, income, topcode)
我现在正在使用大型 SQL 数据库,使用 dplyr 将命令发送到 SQL 服务器。当我 运行 相同的命令时,我返回
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR:
function ifelse (boolean, numeric, numeric) does not exist
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
您建议如何实施 ifelse()
语句?我对 PivotalR 中的某些东西很满意(它似乎支持 ifelse()
,但我不知道如何将它与 dplyr 集成并且在 SO 上找不到任何示例),一些 SQL 我可以在这里直接使用的语法,或者我不知道的 dplyr 的一些特性。
(我有同样的问题,我想使用 grepl()
作为数据库内操作,但我不知道该怎么做。)
我遇到了类似的问题。我能做的最好的就是按照你的建议使用数据库内操作:
topcode <- 10000
queryString <- sprintf("UPDATE db.table SET income_topcoded = %s WHERE income_topcoded > %s",topcode,topcode)
dbGetQuery(con, queryString)
就我而言,我将 MySQL 与 dplyr
一起使用,但无法将我的 ifelse()
转换为有效的 SQL。
根据@hadley 对 this thread 的回复,您可以在 dplyr 的 in-db 数据帧上的 mutate()
中使用 SQL 风格的 if()
语句:
df <- df %>%
mutate( income_topcoded = if (income > topcode) income else topcode)
就使用 grepl()
而言......好吧,你不能。但是您可以使用 SQL like
运算符:
df <- df %>%
filter( topcode %like% "ABC%" )