Exclude columns by name in mutate_at in dplyr

I am trying to make something very simple and still can not determine the correct way to indicate. I just want to exclude some named columns from mutate_at. It works fine if I specify a position, but I don't want hard code positions.

For example, I want to get the same result as this:

mtcars %>% mutate_at(-c(1, 2), max)

But by specifying the column names mpgand cyl.

I have tried many things, including:

mtcars %>% mutate_at(-c('mpg', 'cyl'), max)

Is there a way to work with names and exceptions in mutate_at?

+4
source share
2 answers

You can use varsto specify columns that work the same way select(), and allow you to exclude columns with -:

mtcars %>% mutate_at(vars(-mpg, -cyl), max)
+14

- one_of

mtcars %>% 
     mutate_at(vars(-one_of("mpg", "cyl")), max)
0

Source: https://habr.com/ru/post/1679193/


All Articles