在R数据帧上运行的dplyr中,很容易运行
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 集成,并且找不到任何示例)、我可以在此处内联使用的一些 SQL 语法,或者我不知道的 dplyr 的某些功能。
(我有同样的问题,我想使用grepl()
作为数据库内操作,但我不知道该怎么做。)
基于 @hadley 的回复这个线程 https://stackoverflow.com/questions/23528862/summarize-with-conditions-in-dplyr,您可以使用 SQL 样式if()
里面的声明mutate()
在 dplyr 的数据库内数据帧上:
df <- df %>%
mutate( income_topcoded = if (income > topcode) income else topcode)
就使用而言grepl()
去...好吧,你不能。但你可以使用 SQLlike
操作员:
df <- df %>%
filter( topcode %like% "ABC%" )
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)