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)?
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.
In this case, it is an exact match. So, use fixed = TRUE
grep(x, y, fixed = TRUE)
#[1] 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With