Understanding grep with fixed = T in R

后端 未结 1 2003
臣服心动
臣服心动 2021-01-18 03:48

I checked different posts on this, but still couldn\'t figure out why this is not working:

c=c(\"HI\",\"NO\",\"YESS\")
grep(\"YES\",c,fixed=T)
[1] 3
<         


        
相关标签:
1条回答
  • 2021-01-18 04:09

    This just means that you're matching a string rather than a regular expression, but the string can still be a substring. If you want to match exact cases only, how about

    > x=c("HI","NO","YESS") #better not to name variables after common functions
    > grep("^YES$",x,fixed=F) 
    integer(0) 
    

    Edit per @nicola: This works b/c ^ means beginning and $ end of string, so ^xxxx$ forces the entire string to match xxxx.

    0 讨论(0)
提交回复
热议问题