Diggy Detroit
Diggy Detroit

Reputation: 137

Extract data frame columns based on multiple criteria on column names

I want to subset a data frame based on multiple column name criteria. I have a data frame as below:

id  team_col_code1   team_col_code2 ... team_col_code78   Gender State team_cost_code1   team_cost_code2 ... team_cost_code43 

I am trying to subset this data frame such that the new dataset contains only columns containing column names containing the word "col" or "id" or "Gender".

I am able to create a subset based on column names containing the keyword col as shown below

new_Df <- df[grep("col", names(df))]

I am not sure how to include the other two columns id and Gender, into this subset such that the new dataset looks like this below

id  team_col_code1   team_col_code2   ... team_col_code78   Gender

Any help is much appreciated. Thanks.

Upvotes: 2

Views: 842

Answers (2)

utubun
utubun

Reputation: 4520

Have to work this way:

df[,grepl("col|id|Gender",colnames(df))]

Upvotes: 0

Zheyuan Li
Zheyuan Li

Reputation: 73265

It can be as straightforward as

df[c("id", grep("col", names(df), value = TRUE), "Gender")]

Upvotes: 3

Related Questions