Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse to starts_with() in dplyr

Tags:

r

dplyr

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

like image 479
Edu Avatar asked Sep 14 '17 15:09

Edu


People also ask

How do you select multiple variables in R?

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.


1 Answers

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
like image 87
Psidom Avatar answered Sep 24 '22 23:09

Psidom