Filter rows with a certain suffix

1.5k views Asked by At

I have 12 columns of data in a table called df, the first column contains several thousand strings such as AA150502-01, AA150502-02, BB150502-01, BB150502-03, etc.

I want to filter the table so that I only see the rows ending with the suffix "-01", how do I do this?

I so far have:

myd <- subset(df, Date_ID == 'AA150502-01') 

I need to use some kind of wildcard characters for the prefix which precedes "-01".

2

There are 2 answers

0
BrodieG On BEST ANSWER

Use a regular expression. For example:

myd <- subset(df, grepl("-01$", Date_ID))

or

myd <- df[grep("-01$", df$Date_ID),]
0
SabDeM On

Here is the dplyr solution just in case you want to use it:

data %>% filter(grepl("-01$", Date_ID))