Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine accross() condition with a col name condition

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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(.)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading