johnsonzhj johnsonzhj - 1 year ago 70
R Question

How to extract the cases with certain characters

I want to ask a question about regular expression. I want to extract the cases that include only the certain characters.
For example, in the following vector, I want to extract the cases that only include A or B.


What I expect to get is the following, how I can achieve this?


Answer Source


d[!grepl('[^AB]', d)]

which says, exclude all the elements in d that contain anything other than "A" or "B".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download