【发布时间】:2021-06-11 11:00:43
【问题描述】:
我有一个包含一列字符串的数据框,我尝试过滤掉其中包含日期的字符串。
| ID | headline | SOURCE | domain |
|---|---|---|---|
| 21 | Cool text with a date 20.01.2009 | 0 | howtomakelessthanminimumwagebybuyingthisbook.com |
| 22 | not so cool text without date | 0 | lars.com |
| 23 | also a cool text but without a date :( | 0 | somecryptostuff.com |
| 24 | long text with a date like this 3. march 2021 | 0 | blockchainmasterclassforpeoplewithouttechnicalbackground.com |
| 25 | other long text with this kind of date in the text 03/21/99 and other stuff afterwards | 0 | someother.url |
我已经编写了这样做的代码。
首先,我使用 str_detect() 过滤所有包含日期的行的 df。
代码如下所示:
data <- origin%>%
filter(str_detect(headline, yyyy_mm_dd)|
str_detect(headline, mm_dd_yyyy)|
str_detect(headline, mm_dd_yy)|
str_detect(headline, dd_mm_yyyy)|
str_detect(headline, dd_mm_yy)|
str_detect(headline, annoying_dates)|
str_detect(headline, monthnum_year)|
str_detect(headline, monthname_year)|
str_detect(headline," 20(1|2)\\d\\s"))
mm_dd_yyyy 等是我分配正则表达式的变量。它们看起来像最后一行。
我的代码工作正常,但我经常使用这些过滤条件,重复使用一个函数有点烦人,而且肯定不是好习惯。
我试图想出一个更好的解决方案,但最终未能如愿。你们有什么想法吗?我想过使用一个可以循环的矢量槽,但我不知道这是否可以使用str_detect
【问题讨论】:
-
您可以将各个正则表达式组合成一个表达式,方法是将每个单独的表达式包装成
(…)并用|分隔符连接它们。 -
@KonradRudolph 我之前尝试过,但这样做有很多不合理的问题。出于某种原因,拆分正则表达式修复了无法正确匹配的错误。我在 regex101 上对其进行了测试,它可以工作,但使用 r 时它被破坏了
-
您能否显示共享数据的预期输出?此外,如果您创建一个可以直接复制到 R 中的小型可重复示例,这将更容易提供帮助。阅读how to give a reproducible example。
-
str_detect 已矢量化,标题不准确