Dplyr 0.7 equivalent for obsolete mutate_

I cannot find in dplyr 0.7 way to replace the mutate_ function, which will become obsolete.

The mutate_ function is useful in my case: I store a lot of instructions (which can be filtered if necessary) in the database (string format) and apply these instructions to one or several data frames.

For instance:

 dplyr::tibble(test = " test@test ") %>% dplyr::mutate_(.dots = list("test2" = "substr(test, 1, 5)", "test3" = "substr(test, 5, 5)")) 

Is there a way to do this with dplyr 0.7, storing variables and instructions as a symbol?

+5
source share
2 answers

To expand the MrFlick example a bit , suppose you have several instructions stored as strings, as well as the corresponding names that you want to give to the resulting calculations:

 ln <- list( "test2", "test3" ) lf <- list( "substr(test, 1, 5)", "substr(test, 5, 5)" ) 

Match the names with their instructions and convert everything to quosures:

 ll <- setNames( lf, ln ) %>% lapply( rlang::parse_quosure ) 

According to aosmith's suggestion , the entire list can now be passed to mutate using a special operator !!! :

 tibble( test = " test@test " ) %>% mutate( !!! ll ) # # A tibble: 1 x 3 # test test2 test3 # <chr> <chr> <chr> # 1 test@test test@ @ 
+7
source

Here on an alternative

 a <- "test2" b <- "test3" dplyr::tibble(test = " test@test ") %>% dplyr::mutate(a := !!rlang::parse_quosure("substr(test, 1, 5)"), b := !!rlang::parse_quosure("substr(test, 5, 5)")) # # A tibble: 1 x 3 # test ab # <chr> <chr> <chr> # 1 test@test test@ @ 

We use the := operator to dynamically determine parameters with strings, and we analyze the string of the expression to convert and expand it with '!!'

+3
source

Source: https://habr.com/ru/post/1270295/


All Articles