我使用R,试图从一个字符中解析出多个文本实例,并将其绑定到关联的列。我能够使用str_match成功提取第一个实例,但是,我需要对其余实例执行相同的操作。我相信以某种方式利用str_extract_all可能是可行的方法,但我无法使其正常工作。
我的理想输出是:
Object Id rule_nbr
6603 1
10891 2
13520 2
15079 2
11085 3
11082 3
23221 4
创建伪数据帧的代码如下。
# create dummy data set
logic <- c("Blah blah blah (Object Id 6603) = YES (Y)",
"Blah Blah… (Object Id 10891) = Blah, Blah; (Object Id 13520) Blah -+ Blah (Object Id 15079) = Blah Blah---; $",
"Blah Blah (Object Id 11085) = Blah Blah (Object Id 11082) > $ Blah Blah",
"Blah Blah (Object Id 23221) = Blah Blah (Blah Blah) Blah Blah")
rule_nbr <- c("1","2","3","4")
logic <- as.data.frame(logic)
rule_nbr <- as.data.frame(rule_nbr)
x <- cbind(rule_nbr,logic)
# extract Object Id into a new column keeping it with the associated rule_nbr
x$Object_Id <- str_match(x$logic, '\\(Object Id (\\d+)\\)')[,2]