Reputation: 569
how do I delete columns based on a condition, when the condition is a character contained in the column name? For instance, following a series of inner joins I get a lot of redundant columns:
client.x, client.y, age.y. age.x
132 132 23 23
112 112 12 12
I would like a one liner to remove all columns containing for instance the ".x", to end up with:
client.y, age.y.
132 23
112 12
Thanks
Upvotes: 1
Views: 374
Reputation: 887028
We can use grep
to identify the column names that have .x
and with the use of invert=TRUE
, it returns the index of other columns
df1[grep("\\.x", names(df1), invert=TRUE)]
# client.y age.y.
#1 132 23
#2 112 12
Upvotes: 1