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

前端 未结 2 977
情书的邮戳
情书的邮戳 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:54

    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)
    

    As far as using grepl() goes...well, you can't. But you can use the SQL like operator:

    df  <- df %>%
        filter( topcode %like% "ABC%" )
    

提交回复
热议问题