删除列的方式 [英] Remove columns the tidyeval way

查看:124
本文介绍了删除列的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用dplyr> = 0.7删除列向量

I would like to remove a vector of columns using dplyr >= 0.7

library(dplyr)
data(mtcars)

rem_cols <- c("wt", "qsec", "vs", "am", "gear", "carb")
head(select(mtcars, !!paste0("-", rem_cols)))

错误:字符串必须与列名匹配.未知列:-wt,-qsec,-vs,-am,-gear,-carb

Error: Strings must match column names. Unknown columns: -wt, -qsec, -vs, -am, -gear, -carb

dplyr< 0.7的工作方式如下:

dplyr < 0.7 worked as follows:

head(select_(mtcars, .dots = paste0("-", rem_cols)))
#                    mpg cyl disp  hp drat
# Mazda RX4         21.0   6  160 110 3.90
# Mazda RX4 Wag     21.0   6  160 110 3.90
# Datsun 710        22.8   4  108  93 3.85
# Hornet 4 Drive    21.4   6  258 110 3.08
# Hornet Sportabout 18.7   8  360 175 3.15
# Valiant           18.1   6  225 105 2.76

我已经尝试过rlang:syms(),!!,!!!,quo和enquo的所有组合,可以想到...帮助!?

I've tried about all combinations of rlang:syms(), !!, !!!, quo and enquo that I can think of... help!?

推荐答案

我们可以使用one_of

 mtcars %>%
        select(-one_of(rem_cols))

这篇关于删除列的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆