Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dplyr

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

library(dplyr)
library(lubridate)

Lab_import_sql <- Lab_import %>%
    select_if(~sum(!is.na(.)) > 0) %>%
    mutate_if(is.factor, as.character) %>%
    mutate_if(is.character, funs(ifelse(is.character(.), trimws(.),.))) %>%
    mutate_at(.vars = Lab_import %>% select_if(grepl("'",.)) %>% colnames(),
                 .funs = gsub,
                 pattern = "'",
                 replacement = "''") %>%
    mutate_if(is.character, funs(ifelse(is.character(.), paste0("'", ., "'"),.))) %>%
    mutate_if(is.Date, funs(ifelse(is.Date(.), paste0("'", ., "'"),.)))

Edit:

Thanks everyone for the input, here's reproducible code and my solution:

library(dplyr)
library(lubridate)

import <- data.frame(Test_Name = "Fir'st Last", 
                     Test_Date = "2019-01-01", 
                     Test_Number = 10)

import_sql <-import %>%
  select_if(~!all(is.na(.))) %>%
  mutate_if(is.factor, as.character) %>%
  mutate_if(is.character, trimws) %>%
  mutate_if(is.character, list(~gsub("'", "''",.))) %>% 
  mutate_if(is.character, list(~paste0("'", ., "'"))) %>%
  mutate_if(is.Date, list(~paste0("'", ., "'")))
like image 945
SCDCE Avatar asked Feb 22 '19 19:02

SCDCE


People also ask

What can I use instead of Dplyr fun?

funs() is deprecated; please use list() instead. We deprecated this function because it provided a unique way of specifying anonymous functions, rather than adopting the conventions used by purrr and other packages in the tidyverse.

What does funs do in R?

funs() provides a flexible way to generate a named list of functions for input to other functions like summarise_at() .


1 Answers

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.

like image 61
Calum You Avatar answered Oct 18 '22 11:10

Calum You