r - 在 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
解决方案
您可以使用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()
您会发现每个元素实际上都是它自己的列表,这会导致某些类型的分析出现问题。
推荐阅读
- tomcat - Oracle Apex 版本 4.2.0.00.27 升级到 19.1 - 旧应用程序没有 CSS,新应用程序没问题
- jquery - 选择不同的单选按钮后如何获得不同的输出?
- javascript - Three.JS 错误 - 纹理不是二的幂/无法更改纹理
- angularjs - Froala Angularjs 添加自定义按钮
- javascript - 使用 Chrome 扩展程序发送会话 Cookie
- hana - SAP Cloud Platform 试用版 - 无法打开 HDI 容器
- python - 如何在python中使用修改后的输出大小绘制混淆矩阵并输出为.svg图像?
- webpack - 尝试设置 Webpack 时如何解决“错误:您需要提供目标平台”
- sharepoint - 如何根据前一个屏幕过滤 powerapps 库
- scala - GroupBy 多列作为键并对多列求和,如 sql?