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
  • 42,067
  • 12
  • 102
  • 136
Daniel Kislyuk
  • 828
  • 10
  • 10

1 Answers1

24

In R, you need two times \:

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

Alternative solutions:

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

which(all[5] == "word")
Arun
  • 113,200
  • 24
  • 277
  • 373
Sven Hohenstein
  • 78,180
  • 16
  • 134
  • 160
  • 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