【发布时间】:2020-02-11 16:17:41
【问题描述】:
这是一些数据。
library(stringr)
library(dplyr)
df <- tibble(sentences)
我想识别所有带有“她”这个词的句子。但这当然也会返回带有“那里”和“这里”之类的词的句子。
df %>% filter(str_detect(sentences, "her"))
# A tibble: 43 x 1
sentences
<chr>
1 The boy was there when the sun rose.
2 Help the woman get back to her feet.
3 What joy there is in living.
4 There are more than two factors here.
5 Cats and dogs each hate the other.
6 The wharf could be seen at the farther shore.
7 The tiny girl took off her hat.
8 Write a fond note to the friend you cherish.
9 There was a sound of dry leaves outside.
10 Add the column and put the sum here.
stringr::str_detect 的文档说:“将字符、单词、行和句子边界与 boundary() 匹配。”我无法弄清楚如何做到这一点,也无法在任何地方找到示例。所有文档示例都涉及str_split 或str_count 函数。
我的问题与this question有关,但我特别想了解如何使用stringr::boundary函数。
【问题讨论】: