在使用R提取和转换数据时,我遇到了两个问题,下面是数据集:
messageID | msg
1111111111 | hey id 18271801, fix it asap
2222222222 | please fix it soon id12901991 and 91222911. dissapointed
3333333333 | wow $300 expensive man, come on
4444444444 | number 2837169119 test问题是:
as.matrix(unlist(apply(df2,1 2,1,function(x){regmatches(x,gregexpr(‘(0-9){8},x)})
。
然而,这一行代码,消息444.包括在内,因为is包含8位以上的数字。
dput(df)输出结构(list( id = c(1111111111,2222222222,3333333333,4444444444 ),msg =c(“嗨id 18271801,尽快修复它”,“请尽快修复它id12901991和91222911。请取消”,“哇,300美元昂贵的男人,来吧”,“编号2837169119 test”),.Names = c("id","msg"),row.names = c(NA,4L),class = "data.frame")
谢谢
发布于 2015-03-22 06:45:04
使用rebus创建正则表达式,使用stringr提取匹配。
您可能需要使用正则表达式的确切形式。这段代码适用于您的示例,但您可能需要将其修改为您的数据集。
library(rebus)
library(stringr)
# Create regex
rx <- negative_lookbehind(DGT) %R%
dgt(8) %R%
negative_lookahead(DGT)
rx
## <regex> (?<!\d)[\d]{8}(?!\d)
# Extract the IDs
extracted_ids <- str_extract_all(df$msg, perl(rx))
# Stuff the IDs into a data frame.
data.frame(
messageID = rep(
df$id,
vapply(extracted_ids, length, integer(1))
),
extractedID = unlist(extracted_ids, use.names = FALSE)
)https://stackoverflow.com/questions/29191598
复制相似问题