Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: grep returns 0 when x clearly in y (I checked no spaces)

Tags:

string

r

mismatch

I am clearly missing something grep is returning 0 when the term I am ‘greping’ is clearly in the string that is being ‘grepped’:

In this example I am checking whether the string x is in the string y:

x
[1] "c.3963+1G>T"

y
[1] "c.3963+1G>T"

grep(x, y)
integer(0)

x == y
[1] TRUE

The strings were made from a series of strsplits I did on a vector. What are some reasons that one would see this behavior where grep returns 0 even when x is clearly in y (and they even are considered equivalent as in this example)?

like image 589
Katherine V. Wendelsdorf Avatar asked Mar 10 '17 02:03

Katherine V. Wendelsdorf


2 Answers

To elaborate on the answer by akrun. The first argument to grep is a pattern (in the absence of fixed = TRUE). In your example, x contains 2 characters with special meaning when used as a pattern. The . means "match anything". The + means "match the preceding pattern one or more times". So those characters are not being compared directly with y in the grep.

== is testing for equivalence of the strings, which is different.

like image 187
neilfws Avatar answered Oct 06 '22 02:10

neilfws


In this case, it is an exact match. So, use fixed = TRUE

grep(x, y, fixed = TRUE)
#[1] 1
like image 24
akrun Avatar answered Oct 06 '22 02:10

akrun