dplyr中与starts_with()相反的函数

8

我有一个包含以下变量名称的df

> names(df)
 [1] "local authority: district / unitary (as of April 2015)" "oslaua"                                                
 [3] "December 2014"                                          "X__1"                                                  
 [5] "March 2015"                                             "X__2"                                                  
 [7] "June 2015"                                              "X__3"

我知道可以使用函数df %>% select(starts_with("X_"))来选择以X__开头的变量。

我的问题是,是否有一种函数可以选择那些不以X__开头的变量。输出应该类似于df %>% select(! starts_with("X_"))。谢谢。

1个回答

10

可以。您可以使用-来否定选择/放弃变量:

df <- data.frame(X_1 = 1:3, X_2 = 2:4, Y_1 = 1:3, Y_2 = 2:4)

df %>% select(-starts_with('X_'))

#  Y_1 Y_2
#1   1   2
#2   2   3
#3   3   4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接