Rename across equivalent

Hi all

Getting to grips with the new across function and was wondering what would be the equivalent way to paste a string to the end of numerical columns only which I can achieve as follows right now

  rename_if(is.numeric, .funs = funs(paste0(.,"_total")))
      

thanks in advance

The dplyr 1.0.0 equivalent is rename_with().

library(dplyr, warn.conflicts = FALSE)

iris <- as_tibble(iris) # for nicer printing

# Old way
rename_if(iris, .predicate = is.numeric, .funs = ~ paste0(., "_total"))
#> # A tibble: 150 x 5
#>    Sepal.Length_tot~ Sepal.Width_total Petal.Length_to~ Petal.Width_tot~ Species
#>                <dbl>             <dbl>            <dbl>            <dbl> <fct>  
#>  1               5.1               3.5              1.4              0.2 setosa 
#>  2               4.9               3                1.4              0.2 setosa 
#>  3               4.7               3.2              1.3              0.2 setosa 
#>  4               4.6               3.1              1.5              0.2 setosa 
#>  5               5                 3.6              1.4              0.2 setosa 
#>  6               5.4               3.9              1.7              0.4 setosa 
#>  7               4.6               3.4              1.4              0.3 setosa 
#>  8               5                 3.4              1.5              0.2 setosa 
#>  9               4.4               2.9              1.4              0.2 setosa 
#> 10               4.9               3.1              1.5              0.1 setosa 
#> # ... with 140 more rows

# New way
rename_with(iris, .fn = ~ paste0(., "_total"), .cols = where(is.numeric))
#> # A tibble: 150 x 5
#>    Sepal.Length_tot~ Sepal.Width_total Petal.Length_to~ Petal.Width_tot~ Species
#>                <dbl>             <dbl>            <dbl>            <dbl> <fct>  
#>  1               5.1               3.5              1.4              0.2 setosa 
#>  2               4.9               3                1.4              0.2 setosa 
#>  3               4.7               3.2              1.3              0.2 setosa 
#>  4               4.6               3.1              1.5              0.2 setosa 
#>  5               5                 3.6              1.4              0.2 setosa 
#>  6               5.4               3.9              1.7              0.4 setosa 
#>  7               4.6               3.4              1.4              0.3 setosa 
#>  8               5                 3.4              1.5              0.2 setosa 
#>  9               4.4               2.9              1.4              0.2 setosa 
#> 10               4.9               3.1              1.5              0.1 setosa 
#> # ... with 140 more rows

Created on 2020-07-23 by the reprex package (v0.3.0)

Also, funs() has been deprecated for quite some time so you should use list() instead. However, if you're only applying one transformation, you don't need to wrap it in list().

2 Likes

Perfect, thanks so much for your help

If your issue has been solved, please consider marking my post as a solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.