在 R 中查找并替换整个值 [英] Find and replace entire value in R
问题描述
我正在寻找一种方法来使用 R 中的查找和替换函数来替换字符串的整个值,而不仅仅是字符串的匹配部分.我有一个包含很多(非常)长名称的数据集,我正在寻找一种有效的方法来查找和更改它们的值.
I'm looking for a way to use the find and replace function in R to replace the entire value of a string, rather than just the matching part of the string. I have a dataset with a lot of (very) long names, and I'm looking for an efficient way to find and change their values.
例如,我尝试更改整个字符串
So, for instance, I tried to change this entire string
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."
到
"exp"
使用此代码
string <- gsub("experiences", "exp", string)
然而,这会导致仅用匹配experiences"的字符串部分替换exp",并且保留长名称的其余部分完整(为清晰起见加粗):
However, this results in substituting "exp" with only the part of string that matches "experiences", and it leaves the rest of the long name intact (bolded for clarity):
一般来说.speaking..do.you.prefer.to.try.out.new.exp..like.trying.things.and.meeting.new.people..or.do.you.prefer.familiar.situations.and.faces."
"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."
在这种情况下,由于字符串包含experiences",因此应将其替换为exp".
In this case, because the string contains "experiences", it should be replaced with "exp."
有没有办法告诉 gsub 或其他一些函数来替换整个值?我看了很多教程,似乎函数只能在字符串内或整个值上运行,而不能在两者之间运行.
Is there a way to tell gsub or some other function to replace the entire value? I looked a lot of tutorials and it seems like functions only operate within a string or on whole values, but not between the two.
推荐答案
您可以使用 gsub
如下:
gsub(".*experiences.*", "exp", string, perl=TRUE)
# As @rawr notes, set perl=TRUE for improved efficiency
此正则表达式匹配包含任何字符 0 次或多次(即 .*
)后跟experiences",后跟任何字符 0 次或多次的字符串.
This regex matches strings that have any characters 0 or more times (i.e. .*
) followed by "experiences", followed by any characters 0 or more times.
在这种情况下,您仍然用exp"替换整个匹配,但是通过使用正则表达式,您可以扩展匹配的定义(从experience"到.*experience.*")以实现所需的替换.
In this case, you are still replacing the entire match with "exp" but by using regex, you expand the definition of the match (from "experience" to ".*experience.*") to achieve the desired substitution.
这篇关于在 R 中查找并替换整个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!