我正在寻找一种方法来使用R中的find和replace函数来替换字符串的整个值,而不仅仅是字符串的匹配部分.我有一个包含很多(非常)长名称的数据集,我正在寻找一种有效的方法来查找和更改它们的值.
所以,例如,我试图改变整个字符串
string <- "Generally.speaking..do.you.prefer.to.try.out.new.experiences.like.trying.things.and.meeting.new.people..or.do.you.prefer.familiar.situations.and.faces."
Run Code Online (Sandbox Code Playgroud)
至
"exp"
Run Code Online (Sandbox Code Playgroud)
用这个代码
string <- gsub("experiences", "exp", string)
Run Code Online (Sandbox Code Playgroud)
但是,这会导致将"exp"替换为仅匹配"experience"的字符串部分,并使长名称的其余部分保持原样(为清晰起见加粗):
"Generally.speaking..do.you.prefer.to.try.out.new.EXP ..like.trying.things.and.meeting.new.people..or.do.you.prefer.familiar.situations. and.faces".
在这种情况下,因为字符串包含"经验",所以应该用"exp"替换.
有没有办法告诉gsub或其他一些函数来替换整个值?我看了很多教程,看起来函数只能在一个字符串或整个值中运行,但不是在两者之间运行.