ifelse&使用dplyr进行SQL in-db操作时的grepl命令 [英] ifelse & grepl commands when using dplyr for SQL in-db operations
问题描述
在运行在R数据框上的dplyr中,很容易运行
In dplyr running on R data frames, it is easy to run
df <- df %>%
mutate(income_topcoded = ifelse(income > topcode, income, topcode)
现在使用大型SQL数据库,使用dplyr将命令发送到SQL服务器当我运行相同的命令时,我回到
I'm now working with a large SQL database, using dplyr to send commands to the SQL server. When I run the same command, I get back
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的某些功能。
How would you suggest implementing ifelse()
statements? I'd be fine with something in PivotalR (which seems to support ifelse()
, but I don't know how to integrate it with dplyr and couldn't find any examples on SO), some piece of SQL syntax which I can use in-line here, or some feature of dplyr which I was unaware of.
(我有同样的问题,我想使用 grepl()
作为一个in-db操作,但我不知道该怎么做。)
(I have the same problem that I'd like to use grepl()
as an in-db operation, but I don't know how to do so.)
推荐答案
根据@ hadley对此线程的回复,您可以使用SQL风格的<在dplyr的数据库数据框中,code> if()语句 mutate()
Based on @hadley's reply on this thread, you can use an SQL-style if()
statement inside mutate()
on dplyr's in-db dataframes:
df <- df %>%
mutate( income_topcoded = if (income > topcode) income else topcode)
只要使用 grepl()
去...好吧,你不行。但是您可以使用SQL ,如
operator:
As far as using grepl()
goes...well, you can't. But you can use the SQL like
operator:
df <- df %>%
filter( topcode %like% "ABC%" )
这篇关于ifelse&使用dplyr进行SQL in-db操作时的grepl命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!