ifelse & grepl commands when using dplyr for SQL in-db operations

前端 未结 2 918
情书的邮戳
情书的邮戳 2021-02-09 15:27

In dplyr running on R data frames, it is easy to run

df <- df %>% 
    mutate(income_topcoded = ifelse(income > topcode, income, topcode)
2条回答
  •  情话喂你
    2021-02-09 15:53

    I had a similar problem. The best I could do was to use an in-db operation as you suggest:

    topcode <- 10000
    queryString <- sprintf("UPDATE db.table SET income_topcoded = %s WHERE income_topcoded > %s",topcode,topcode)
    dbGetQuery(con, queryString)
    

    In my case, I was using MySQL with dplyr, but it wasn't able to translate my ifelse() into valid SQL.

提交回复
热议问题