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
To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With