首页 > 解决方案 > 在 R 中,如何将多个匹配的术语提取为字符串,如果 TRUE 与 Regex 或 Grep 匹配?

问题描述

我仍然是 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

标签: rregexgrepl

解决方案


您可以使用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 = FALSEstr_extract_all您的列matches将看起来正确,就像我们在sapply上面得到的结果一样。但是,如果您仔细检查,str()您会发现每个元素实际上都是它自己的列表,这会导致某些类型的分析出现问题。


推荐阅读