detecting word boundary with regex in data frame i

2019-01-15 16:14发布

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?

1条回答
做个烂人
2楼-- · 2019-01-15 16:42

In R, you need two times \:

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

Alternative solutions:

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

which(all[5] == "word")
查看更多
登录 后发表回答