如何将 str_extract_all 变成多列 [英] How to turn str_extract_all into multiple columns
本文介绍了如何将 str_extract_all 变成多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是正文:
data$charge[1]
[1] "Count #1 as Filed: In Violation of; 21 O.S. 645; Count #2 as Filed: In Violation of; 21 O.S. 1541.1;Docket 1"
我目前正在尝试从法律数据中提取法规.我的代码如下所示:
I am currently trying to extract statutes from legal data. My code looks like this:
str_extract_all(data$charge[1:3], "(?<=Violation of;)(\\D|\\d){4,20}(?=;Count |;Docket)")
[[1]]
[1] "21 O.S. 645" "21 O.S. 1541.1"
[[2]]
[1] "21 O.S. 1435 "21 O.S. 1760(A)(1)
[[3]]
[1] "21 O.S. 1592"
我想将它们作为列添加到这样的数据框中:
And I'd like to add them as columns to a data frame like this:
id name statute1 statute2 statute3
1 BLACK, JOHN 21 O.S. 645 21 O.S. 1541.1 NA
2 DOE, JANE 21 O.S. 1435 21 O.S. 1760(A)(1) NA
3 ROSS, BOB 21 O.S. 1592 NA NA
谢谢!有意义吗?
推荐答案
由于您没有包含数据或预期输出的可重现示例,我无法确定,但我认为您正在寻找的是str_extract_all
的 simplify = TRUE
参数.
Since you haven't included a reproducible example of your data or expected output, I can't be sure, but I think what you're looking for is the simplify = TRUE
argument for str_extract_all
.
来自 ?str_extract_all
上的示例:
shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2")
# without simplify = TRUE
str_extract_all(shopping_list, "\\b[a-z]+\\b")
[[1]]
[1] "apples"
[[2]]
[1] "bag" "of" "flour"
[[3]]
[1] "bag" "of" "sugar"
[[4]]
[1] "milk"
# with simplify = TRUE
str_extract_all(shopping_list, "\\b[a-z]+\\b", simplify = TRUE)
[,1] [,2] [,3]
[1,] "apples" "" ""
[2,] "bag" "of" "flour"
[3,] "bag" "of" "sugar"
[4,] "milk" "" ""
使用您添加的示例:
dat <- "Count #1 as Filed: In Violation of; 21 O.S. 645; Count #2 as Filed: In Violation of; 21 O.S. 1541.1;Docket 1"
str_extract_all(dat, "(?<=Violation of;)(\\D|\\d){4,20}(?=;Count |;Docket)",
simplify = TRUE)
[,1]
[1,] " 21 O.S. 1541.1"
这篇关于如何将 str_extract_all 变成多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文