此问题与Replace the string value with value in the find list in R有关。但是我的字符串和替换向量具有相似的字符:
replacement = c("10deg AC45", "10deg TA45", "15deg AC45", "AC45", "TA45")
string = c("10deg - 02 AC45", ""10deg -05 AC45" , "10deg-02 AC45", "10deg-05 TA45", "15deg-10 AC45", "AC45-10", "TA45-1")
因此尝试:
replaced=string
replaced=''
for (i in 1:length(replacement))
{
replaced[which(grepl(replacement[i],string))]=replacement[i]
}
replaced[is.na(replaced)]=''
结果:
[1] "AC45", "AC45", "AC45" "TA45" "AC45" "AC45" "TA45"
而不是:
“ 10deg AC45”,“ 10deg AC45”,“ 10deg AC45”,“ 10deg TA45”,“ 15deg AC45”,“ AC45”,“ TA45”
有什么建议吗? 谢谢