Subset data to contain only columns whose names DOESN´T match a condition

I can define a dataframe running this code:

set.seed(1)
df <- data.frame( ABC_1 = sample(0:1,3,repl = TRUE),
            ABC_2 = sample(0:1,3,repl = TRUE),
            XYZ_1 = sample(0:1,3,repl = TRUE),
            XYZ_2 = sample(0:1,3,repl = TRUE),
CDE_1 = sample(0:1,3,repl = TRUE) )

This is the command to subset data to contain only columns whose names match a condition - having ABC in the name.

df1 <- df[ , grepl( "ABC" , names( df ) ) ]

I want the inverse. I want to subset data to contain only columns whose names match a condition - not having ABC in the name.

Can you help me please?

df1 <- df[ , !grepl( "ABC" , names( df ) ) ]
1 Like

Thank you @martin.R ! I didn´t know where to put and which was the negative operator.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.