Gopala Gopala -4 years ago 220
R Question

Exclude columns by names in mutate_at in dplyr

I am trying to do something very simple, and yet can't figure out the right way to specify. I simply want to exclude some named columns from

mutate_at
. It works fine if I specify position, but I don't want to hard code positions.

For example, I want the same output as this:

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


But, by specifying
mpg
and
cyl
column names.

I tried many things, including:

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


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

Answer Source

You can use vars to specify the columns, which works the same way as select() and allows you to exclude columns using -:

mtcars %>% mutate_at(vars(-mpg, -cyl), max)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download