detecting word boundary with regex in data frame in R [duplicate]
Asked Answered
U

1

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?

Usable answered 28/7, 2013 at 7:31 Comment(1)
Why not just all %>% filter(column %in% "word")?Sleuth
C
25

In R, you need two times \:

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

Alternative solutions:

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

which(all[5] == "word")
Calaverite answered 28/7, 2013 at 7:38 Comment(2)
Both of your solutions work, thank you. Do you know why "\bword\b" does not work in this case?Usable
+1 The pattern grep("^word$", ...) will match the whole string, not just words.. even though here they don't make a difference.Xerosis

© 2022 - 2024 — McMap. All rights reserved.