识别列中的模式,并将它们添加到数据框中的列

时间:2018-06-04 09:19:28

标签: r regex text preprocessor

有一个包含50个关键字的列:

Keyword1 
Keyword2
Keyword3
KeywordN=50

此外,我得到了一个包含两列的数据框:Title和Abstract。

Title                    Abstract 
Rstudio Keyword1        A interesting program language keyword2  
Python Keyword3         A interesting program keyword3 language 

我想获得一个额外的列(让我们称之为关键字),其中关键字名称将出现在标题或摘要中,如下所示:

Title             Abstract                                   Keywords
Rstudio Keyword1 A interesting program language keyword2  Keyword1, keyword2
Python Keyword2  A interesting program keyword3 language  Keyword2, Keyword3

我唯一能解决的问题是'这是通过制作二进制列(如果模式匹配)。 (grepl功能),但这不是理想的解决方案...

4 个答案:

答案 0 :(得分:1)

基础{TemplateBinding}中的

  • 用于处理标点符号,空格,行的结束/开始。
  • 关键字可以包含空格和一些标点符号(但不是全部)
  • 新列中的
  • 关键字保留原始关键字矢量的大小写:

<强>码

R

数据

ind  <- sapply(paste0('(^|[ [:punct:]])',tolower(keywords),'($|[ [:punct:]])'),grep,tolower(paste(df$Title,df$Abstract)))
ind[lengths(ind)==0] <- NA # for cases where no keyword is found
ind2 <- do.call(rbind,Map(data.frame,keyword=keywords,i=ind))
ind3 <- aggregate(keyword ~ i,ind2,paste,collapse=', ')
df$keywords[ind3$i] <- ind3$keyword
df$keywords[is.na(df$keywords)] <- "" # replacing NAs with empty strings
#              Title                                Abstract           keywords
# 1 Rstudio Keyword1 A interesting program language keyword2 Keyword1, Keyword2
# 2  Python Keyword2 A interesting program keyword3 language Keyword2, Keyword3

答案 1 :(得分:1)

使用strsplit的另一种方法(也在基础R中):

ls <- strsplit(tolower(paste(df$Title, df$Abstract)), 
                       "(\\s+)|(?!')(?=[[:punct:]])", perl = TRUE)    

df$Keywords <- do.call("rbind", 
               lapply(ls, function(x) paste(unique(x[x %in% tolower(keywords)]), 
               collapse = ", ")))

#             Title                                Abstract           Keywords
#1 Rstudio Keyword1 A interesting program language keyword2 keyword1, keyword2
#2  Python Keyword2 A interesting program keyword3 language keyword2, keyword3

样本数据

df <- data.frame(Title = c("Rstudio Keyword1", "Python Keyword2"), 
                 Abstract = c("A interesting program language keyword2",  
                              "A interesting program keyword3 language"), 
                 stringsAsFactors = F)

keywords <- paste0("Keyword", 1:4)

答案 2 :(得分:1)

cbind(dat,Keywords=do.call(paste,c(sep=",",Map(sub,paste0(".*(",paste(keywords,collapse="|"),").*"),"\\1",dat,TRUE))))
             Title                                Abstract          Keywords
1 Rstudio Keyword1 A interesting program language keyword2 Keyword1,keyword2
2  Python Keyword3 A interesting program keyword3 language Keyword3,keyword3

其中keywords=paste0("Keyword",1:3)

dat=read.table(text="Title                    Abstract 
'Rstudio Keyword1'        'A interesting program language keyword2'  
'Python Keyword3'         'A interesting program keyword3 language'",h=T,strin=F)

这条线似乎很长:细分:

a=paste0(".*(",paste(keywords,collapse="|"),").*")
b=do.call(paste,c(sep=",",Map(sub,a,"\\1",dat,TRUE)))
cbind(dat,keywords=b)
             Title                                Abstract          keywords
1 Rstudio Keyword1 A interesting program language keyword2 Keyword1,keyword2
2  Python Keyword3 A interesting program keyword3 language Keyword3,keyword3

答案 3 :(得分:0)

Title<-as.character(c("Rstudio Keyword1","Python Keyword3"))
Abstract<-as.character(c("A interesting program language keyword2"," A interesting program keyword3 language"))
example1.data <- data.frame(Title,Abstract)


#loop answer
f<-length(example1.data)
example1.data$Keyword <- NA

for (i in 1:nrow(example1.data)){
testA[i]<-regmatches(example1.data$Title[i], regexpr("(Keyword|keyword) ([0-9])", example1.data$Title[i]))
testB[i]<-regmatches(example1.data$Abstract[i], regexpr("(Keyword|keyword)([0-9])", example1.data$Abstract[i]))
example1.data$Keyword[i]<-paste(testA[i],testB[i],  sep=", ")

}