【发布时间】:2017-03-09 00:36:29
【问题描述】:
我有一个数据集,其中包含类似
的列 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。我现在正在使用以下代码
gsub(paste(replacement, collapse = "|"), replacement = replacement, x = string)
这是我用来查找案例的另一段代码
string[grepl(paste(replacement, collapse='|'), string, ignore.case=TRUE)]
我想更新我找到的那些 我希望输出像
Rstudio,Rstudio,'',Jupyter,spyder,R
我不想通过硬编码来做到这一点。我想编写一个可扩展的代码。
非常感谢任何帮助
提前致谢
【问题讨论】:
-
试试
ifelse((x <- sub('.*_', '', string)) %in% replacement, x, '') -
这个代码对我来说但是我不能替换任何东西 grep(paste(replacement, collapse='|'), string, ignore.case=TRUE)]
-
对不起。我会尝试编辑问题