johnsonzhj johnsonzhj - 3 months ago 8
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.

d<-c('AAAAAA','BBBBBBBBB','ABAABAAA','ABA','ABC','ABD','AABB?','A(ABBAAB','ABABABA')
grep('[A|B]',d,value=T)


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

c('AAAAAA','BBBBBBBBB','ABAABAAA','ABA','ABABABA')

Answer

Try

d[!grepl('[^AB]', d)]
# [1] "AAAAAA"    "BBBBBBBBB" "ABAABAAA"  "ABA"       "ABABABA"  

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