@leoncio you can use the below syntax for this. Different variants of mutate, e.g. mutate_at
, mutate_if
, mutate_all
, mutate_each
all are intended to accomplish this task. Alternatively, if you have an updated version of dplyr, I believe the function across
can also work here.
library(tidyverse)
mtcars %>%
select(contains("d")) %>% # not neccessary unless you only want to see these columns
mutate_at(vars(contains("d")),list(~.*(-1)))
#> disp drat
#> 1 -160.0 -3.90
#> 2 -160.0 -3.90
#> 3 -108.0 -3.85
#> 4 -258.0 -3.08
#> 5 -360.0 -3.15
#> 6 -225.0 -2.76
#> 7 -360.0 -3.21
#> 8 -146.7 -3.69
#> 9 -140.8 -3.92
#> 10 -167.6 -3.92
#> 11 -167.6 -3.92
#> 12 -275.8 -3.07
#> 13 -275.8 -3.07
#> 14 -275.8 -3.07
#> 15 -472.0 -2.93
#> 16 -460.0 -3.00
#> 17 -440.0 -3.23
#> 18 -78.7 -4.08
#> 19 -75.7 -4.93
#> 20 -71.1 -4.22
#> 21 -120.1 -3.70
#> 22 -318.0 -2.76
#> 23 -304.0 -3.15
#> 24 -350.0 -3.73
#> 25 -400.0 -3.08
#> 26 -79.0 -4.08
#> 27 -120.3 -4.43
#> 28 -95.1 -3.77
#> 29 -351.0 -4.22
#> 30 -145.0 -3.62
#> 31 -301.0 -3.54
#> 32 -121.0 -4.11
Created on 2020-12-14 by the reprex package (v0.3.0)