I have a dplyr chain with mutate like so:
df %>%
mutate(across(where(is.factor), ~ as.numeric(levels(.))[.]))
I want to add a condition, in english ‘mutate where the col is factor AND where the column name does not equal bla’.
How can I combine these here?
>Solution :
You can combine where() with other tidyselect helpers e.g. matches or with other conditions, e.g. in your case you could use !COLUMN_NAME (Thx to @DarranTsai for pointing that out) to account for the column on which you don’t want to apply the function:
library(dplyr, warn = FALSE)
iris2 <- iris[c(1:5, 51:55, 101:105), ]
iris2$species2 <- iris2$Species
iris2 %>%
mutate(across(where(is.factor) & !matches("^Species$"), ~ as.numeric(.)))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species species2
#> 1 5.1 3.5 1.4 0.2 setosa 1
#> 2 4.9 3.0 1.4 0.2 setosa 1
#> 3 4.7 3.2 1.3 0.2 setosa 1
#> 4 4.6 3.1 1.5 0.2 setosa 1
#> 5 5.0 3.6 1.4 0.2 setosa 1
#> 51 7.0 3.2 4.7 1.4 versicolor 2
#> 52 6.4 3.2 4.5 1.5 versicolor 2
#> 53 6.9 3.1 4.9 1.5 versicolor 2
#> 54 5.5 2.3 4.0 1.3 versicolor 2
#> 55 6.5 2.8 4.6 1.5 versicolor 2
#> 101 6.3 3.3 6.0 2.5 virginica 3
#> 102 5.8 2.7 5.1 1.9 virginica 3
#> 103 7.1 3.0 5.9 2.1 virginica 3
#> 104 6.3 2.9 5.6 1.8 virginica 3
#> 105 6.5 3.0 5.8 2.2 virginica 3
Or using !:
iris2 %>%
mutate(across(where(is.factor) & !Species, ~ as.numeric(.)))