替换字符串中的特定字符 [英] Replace specific characters within strings
本文介绍了替换字符串中的特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从向量中的字符串中删除特定字符,类似于Excel中的查找和替换功能.
I would like to remove specific characters from strings within a vector, similar to the Find and Replace feature in Excel.
以下是我开始使用的数据:
Here are the data I start with:
group <- data.frame(c("12357e", "12575e", "197e18", "e18947")
我从第一列开始;我想通过删除e
来产生第二列:
I start with just the first column; I want to produce the second column by removing the e
's:
group group.no.e
12357e 12357
12575e 12575
197e18 19718
e18947 18947
推荐答案
带有正则表达式和函数gsub()
:
With a regular expression and the function gsub()
:
group <- c("12357e", "12575e", "197e18", "e18947")
group
[1] "12357e" "12575e" "197e18" "e18947"
gsub("e", "", group)
[1] "12357" "12575" "19718" "18947"
gsub
在这里所做的是用空字符串""
替换每次出现的"e"
.
What gsub
does here is to replace each occurrence of "e"
with an empty string ""
.
有关更多帮助,请参见?regexp
或gsub
.
See ?regexp
or gsub
for more help.
这篇关于替换字符串中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文