在R中,如何将多个匹配项提取为字符串,并在Regex或Grep中为TRUE时进行匹配?

时间:2019-01-03 17:33:28

标签: r regex grep grepl

我仍然是R语言的初学者。我需要一些代码帮助,这些代码需要在向量中搜索列表中的术语并返回TRUE。如果为TRUE,则返回匹配项的字符串。

我设置为告诉我术语是否匹配并返回第一个匹配的术语,但是我不确定如何获得其余匹配的术语。

在所附的代码中,我有Desired_Output和不完善的Final_Output。

#create dataset of 2 columns/vectors. 1st column is "Job Title", 2nd column is "Work Experience"
'Work Experience' <- c("cooked food; cleaned house; made beds", "analyzed data; identified gaps; used sql, python, and r", "used tableau to make dashboards for clients; applied advanced macro excel functions", "financial planning and strategy; consulted with leaders and clients")
'Job Title' <- c("dad", "research analyst", "business intelligence consultant", "finance consultant")
Job_Hist   <- data.frame(`Job Title`, `Work Experience`)

#create list of terms to search for in Job_Hist
Term_List <- c("python", " r", "sql", "tableau", "excel")

#use grepl to search the Work Experience vector for terms in CS_Term_List THEN return TRUE or FALSE
Term_TF<- grepl(paste(Term_List, collapse = '|'),Job_Hist$Work.Experience)

#add a new column to our final output dataframe that shows if the job experience matched our terms  
Final_Output<-Job_Hist
Final_Output$Term_Test <- Term_TF


#Let's see what what terms caused the TRUE Flag in the Final_Output
m<-regexpr(paste(Term_List, collapse = '|'),
       Job_Hist$Work.Experience, perl=TRUE)
T_Match <- regmatches(Job_Hist$Work.Experience,m)



#Compare Final_Output to my Desired_Output and please help me :)
Desired_T_Match <- c("NA", "sql, python, r", "tableau, excel", "NA")
Desired_Output <- data.frame(`Job Title`, `Work Experience`, Term_TF, Desired_T_Match)

#I need 2 things. 
 #1) a way to tie T_Match back to Final_Output... something like if, TRUE then match
 #2) a way to return every term matched in a coma delimited string. Example: research analyst   analyzed data...    TRUE    sql, python

1 个答案:

答案 0 :(得分:1)

您可以使用stringr::str_extract_all从每一行中获取匹配项列表:

library(stringr)
library(tidyverse)

Job_Hist$matches <- str_extract_all(Job_Hist$Work.Experience, 
  paste(Term_List, collapse = '|'), simplify = TRUE)

                                                                      Work.Experience  Term matches.1 matches.2
1                                               cooked food; cleaned house; made beds FALSE                    
2                             analyzed data; identified gaps; used sql, python, and r  TRUE       sql    python
3 used tableau to make dashboards for clients; applied advanced macro excel functions  TRUE   tableau     excel
4                 financial planning and strategy; consulted with leaders and clients FALSE                    
  matches.3
1          
2         r
3          
4       

编辑:如果您希望在一列中以逗号分隔的字符串作为匹配项,则可以使用:

str_extract_all(Job_Hist$Work.Experience, paste(Term_List, collapse = '|')) %>% 
  sapply(., paste, collapse = ", ")

           matches
1                
2 sql, python,  r
3  tableau, excel
4                

请注意,如果您在simplify = FALSE中使用默认参数str_extract_all,则列matches将看起来正确,就像上面sapply所得到的结果一样。但是,如果使用str()进行检查,您会看到每个元素实际上都是它自己的列表,这将对某些类型的分析造成问题。