简体   繁体   中英

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 ?

您可以使用vars指定列,其工作方式与select()相同,并允许您使用-排除列:

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

One option is to pass the strings inside one_of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM