将字符串值替换为R中查找列表中的值 [英] Replace the string value with value in the find list in R
本文介绍了将字符串值替换为R中查找列表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据集,其中包含
I have a dataset that has a column like
string<-c('lib1_Rstudio_case1','lib2_Rstudio_case1and2','lib5_python_notthe correct_language','lib3_Jupyter_really_good','lib1_spyder_nice','lib1_R_the_core')
replacement<-c('Rstudio','Jupyter','spyder','R')
我要替换与替换中的值匹配的字符串值id.我现在正在使用以下代码
I want to replace the string value id they match the value in replacement. I am using the following code right now
gsub(paste(replacement, collapse = "|"), replacement = replacement, x = string)
这是我用来查找案例的另一段代码
This in another piece of code which i am using to find the cases
string[grepl(paste(replacement, collapse='|'), string, ignore.case=TRUE)]
我想更新我发现的那些 我希望输出像
I want to update the ones that I find I want the output to be like
Rstudio,Rstudio,'',Jupyter,spyder,R
我不想通过硬编码来做到这一点.我想写一个可扩展的代码.
I don't want to do it by hard coding it. I want to write a code that is scalable.
我们非常感谢您的帮助
提前感谢
推荐答案
这是我使用的另一个简单代码.不需要regex函数.感谢帮助
This another simple code I used. That doesn't need the regex function.Thanks for the help
string<-c('lib1_Rstudio_case1','lib2_Rstudio_case1and2','lib5_python_notthe correct_language','lib3_Jupyter_really_good','lib1_spyder_nice','lib1_R_the_core')
replacement<-c('R','Jupyter','spyder','Rstudio')
replaced=string
replaced=''
for (i in 1:length(replacement))
{
replaced[which(grepl(replacement[i],string))]=replacement[i]
}
replaced[is.na(replaced)]=''
这篇关于将字符串值替换为R中查找列表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文