How to change the now deprecated dplyr::funs() which includes an ifelse argument?

前端 未结 1 1344
死守一世寂寞
死守一世寂寞 2020-12-03 15:01

Pretty basic but I don\'t think I really understand the change:

library(dplyr)
library(lubridate)

Lab_import_sql <- Lab_import %>%
    select_if(~sum(         


        
相关标签:
1条回答
  • 2020-12-03 15:15

    As of dplyr 0.8.0, the documentation states that we should use list instead of funs, giving the example:

    Before:

    funs(name = f(.))

    After:

    list(name = ~f(.))

    So here, the call funs(ifelse(is.character(.), trimws(.),.)) can become instead list(~ifelse(is.character(.), trimws(.),.)). This is using the formula notation for anonymous functions in the tidyverse, where a one-sided formula (expression beginning with ~) is interpreted as function(x), and wherever x would go in the function is represented by .. You can still use full functions inside list.

    Note the difference between the .funs argument of mutate_if and the funs() function which wrapped other functions to pass to .funs; i.e. .funs = gsub still works. You only needed funs() if you needed to apply multiple functions to selected columns or to name them something by passing them as named arguments. You can do all the same things with list().

    You also are duplicating work by adding ifelse inside mutate_if; that line could be simplified to mutate_if(is.character, trimws) since if the column is character already you don't need to check it again with ifelse. Since you apply only one function, no need for funs or list at all.

    0 讨论(0)
提交回复
热议问题