9

I have a data.frame named all that has a column of factors, these factors include "word","nonword" and some others. My goal is to select only the rows that have the factor value "word".

My solution grep("\bword\b",all[,5]) returns nothing.

How come word boundaries are not recognized?

Thomas
  • 43,637
  • 12
  • 109
  • 140
Daniel Kislyuk
  • 956
  • 10
  • 11

1 Answers1

25

In R, you need two times \:

grep("\\bword\\b", all[5])

Alternative solutions:

grep("^word$", all[5])

which(all[5] == "word")
Arun
  • 116,683
  • 26
  • 284
  • 387
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • 1
    Both of your solutions work, thank you. Do you know why "\bword\b" does not work in this case? – Daniel Kislyuk Jul 28 '13 at 07:45
  • 4
    +1 The pattern `grep("^word$", ...)` will match the whole string, not just words.. even though here they don't make a difference. – Arun Jul 28 '13 at 09:09