Reputation: 903
I have df
that has variables with the following names:
> 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"
I know I can select those variables that start with X__
by using a function df %>% select(starts_with("X_"))
.
My question is whether there would be a function that makes it possible to select exactly those variables that do not start with X__
. The output should give something similar to df %>% select(! starts_with("X_"))
. Thanks in advance
Upvotes: 8
Views: 2391
Reputation: 214957
Yes. You can use -
to negate the selection/drop variables:
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
Upvotes: 10