简体   繁体   English

如何基于名称的向量(dplyr)从R data.frame中省略变量

[英]How to omit variables from R data.frame based on vector of names (dplyr)

I have data.frame and a long list of variables I need to omit. 我有data.frame和需要省略的一长串变量。 It would be convenient to use the vector of variable names. 使用变量名的向量将很方便。 Additionally, I'd preferably do this with dplyr or tidyr. 另外,我最好还是用dplyr或tidyr。

I've already tried this: 我已经尝试过了:

df <- data.frame(A = runif(10), B = runif(10))

omits <- c("B")

df %>% select_(.dots = -omits)

That did not work though and I get Invalid argument to unary operator error. 但是,这没有用,并且我得到一元运算符错误的Invalid参数。 Therefore I already know that I'm not doing correctly with the negative sign, but I have not found workable alternative either. 因此,我已经知道我没有正确处理负号,但也没有找到可行的替代方法。 The example is very simple, but my actual problem more complicated with much longer omits (and selected) vector. 这个例子很简单,但是我的实际问题却变得更加复杂,而省略(和选择)了更长的向量。 I really want to avoid typing them separately. 我真的想避免单独键入它们。

You can use one_of to select or in this case omit variables provided in a character vector (those included in the vector omits ): 您可以使用one_of选择或在这种情况下省略一个特征向量(这些包含在载体中提供变量omits ):

df %>% select(-one_of(omits))

Output: 输出:

           A
1  0.9930896
2  0.4075611
3  0.3654101
4  0.2161043
5  0.4034992
6  0.8350059
7  0.4828840
8  0.1134290
9  0.2902616
10 0.4052897

We can use 我们可以用

 df %>% 
    select_(.dots= setdiff(names(.), omits))
#          A
#1  0.5479797
#2  0.7071427
#3  0.9020869
#4  0.4815137
#5  0.8413017
#6  0.5813052
#7  0.1528620
#8  0.4352227
#9  0.8802945
#10 0.9828040

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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