user25260
user25260

Reputation: 165

Subset based on column names containing certain words

I'm trying to subset columns containing the word "disagreement" but maintain the first three columns containing ID information. How can I adapt the code below to keep the ID columns?

df.disagreement <- df[ , grepl("disagreement", names( df ) )]

Upvotes: 1

Views: 3685

Answers (3)

cory
cory

Reputation: 6669

df.disagreement <- df[ , names(df) %in% c("index1", "index2", "index3") | 
                         grepl("disagreement", names( df ) )]

OR

df.disagreement <- df[ , names(df) %in% names(df)[1:3] | 
                         grepl("disagreement", names( df ) )]

Maybe an explanation would be valuable too... So, your grepl statement makes a vector of TRUE / FALSE with the length as the number of names in df. So, the solution above uses that TRUE / FALSE vector and uses a logical OR (|) with another TRUE / FALSE vector (names(df) %in% something). You can do this with another statement too, just add another | or & and keep rollin'.

Upvotes: 1

alexwhan
alexwhan

Reputation: 16056

This is the situation that select() from dplyr was born for:

library(dplyr)
select(df, 1:3, contains("disagreement"))

Upvotes: 2

Peter Diakumis
Peter Diakumis

Reputation: 4042

Not sure if this is what you mean: df.disagreement <- df[ , c(TRUE, TRUE, TRUE, grepl("disagreement", names( df )[-c(1:3)] ))]?

Upvotes: 2

Related Questions