【发布时间】:2021-02-17 03:20:21
【问题描述】:
我正在尝试编写一个函数来检测全部大写的大写单词
目前,代码:
df <- data.frame(title = character(), id = numeric())%>%
add_row(title= "THIS is an EXAMPLE where I DONT get the output i WAS hoping for", id = 6)
df <- df %>%
mutate(sec_code_1 = unlist(str_extract_all(title," [A-Z]{3,5} ")[[1]][1])
, sec_code_2 = unlist(str_extract_all(title," [A-Z]{3,5} ")[[1]][2])
, sec_code_3 = unlist(str_extract_all(title," [A-Z]{3,5} ")[[1]][3]))
df
输出在哪里:
| title | id | sec_code_1 | sec_code_2 | sec_code_3 |
|---|---|---|---|---|
| THIS is an EXAMPLE where I DONT get the output i WAS hoping for | 6 | DONT | WAS |
第一个 3-5 个字母大写的单词是“THIS”,第二个应该跳过示例 (>5) 并且是“DONT”,第三个示例应该是“WAS”。 即:
| title | id | sec_code_1 | sec_code_2 | sec_code_3 |
|---|---|---|---|---|
| THIS is an EXAMPLE where I DONT get the output i WAS hoping for | 6 | THIS | DONT | WANT |
有谁知道我哪里出错了?具体来说,我如何使用 stringr 在逻辑上表示“空格或字符串开头”或“空格或字符串结尾”。
【问题讨论】: