【发布时间】:2019-06-18 05:13:18
【问题描述】:
我正在使用str_extract() 和str_extract_all() 来查看一下正则表达式。有零个、一个或多个结果,所以我想unnest() 将多个结果分成多行。由于 ab_all 中的字符(0)(我假设),unnest 不会给出输出中的所有行。
library(tidyverse)
my_tbl <- tibble(clmn = c("abcd", "abef, abgh", "xkcd"))
ab_tbl <- my_tbl %>%
mutate(ab = str_extract(clmn, "(?<=ab)[:alpha:]*\\b"),
ab_all = str_extract_all(clmn, "(?<=ab)[:alpha:]*\\b"),
cd = str_extract(clmn, "[:alpha:]*(?=cd)"))
ab_tbl %>% unnest(ab_all, .drop = FALSE)
# A tibble: 3 x 4
clmn ab cd ab_all
<chr> <chr> <chr> <chr>
1 abcd cd ab cd
2 abef, abgh ef NA ef
3 abef, abgh ef NA gh
编辑: 预期输出:
# A tibble: 3 x 4
clmn ab cd ab_all
<chr> <chr> <chr> <chr>
1 abcd cd ab cd
2 abef, abgh ef NA ef
3 abef, abgh ef NA gh
4 xkcd NA xk NA
输出中没有给出带有 xkccd 的行。是否与 str_extract_all 或 unnest 有关,还是我应该改变我的方法?
【问题讨论】:
-
您好 nmoorenz,您能否在您的问题中添加一个数据框,以明确您正在寻找的输出?