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.
If your question has been answered, don't forget to mark the solution!
How do I mark a solution?
Find the reply you want to mark as the solution and look for the row of small gray icons at the bottom of that reply. Click the one that looks like a box with a checkmark in it:
[image]
Hovering over the mark solution button shows the label, "Select if this reply solves the problem". If you don't see the mark solution button, try clicking the three dots button ( ••• ) to expand the full set of options.
When a solution is chosen, the icon turns green and the hover label changes to: "Unselect if this reply no longer solves the problem". Success!
[solution_reply_author]
…
system
Closed
July 31, 2020, 5:35am
5
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.