首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R替换函数gsub

R替换函数gsub

作者头像
生信交流平台
发布2020-08-06 11:02:43
1.4K0
发布2020-08-06 11:02:43
举报
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
    fixed = FALSE, useBytes = FALSE)

其中pattern是要替换的字符,replacement是替换成的字符,x是对应的string或string vector。

string举例如下:

> gsub("ut","ot",x)

ignore.case表示是否忽视大小写。

vector举例如下:

> x <- c("R Tutorial","PHP Tutorial", "HTML Tutorial")
> gsub("Tutorial","Examples",x)
#将Tutorial替换成Examplers

[1] "R Examples"    "PHP Examples"  "HTML Examples"

还有其他的一些例子来灵活使用这个函数,结合正则表达式。

> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("\\d+","---",x)
#\\d表示一个任意的数字,+表示一个以上,所以4322和25都被替换成了---

> y
[1] "line ---: He is now --- years old, and weights ---lbs"
 

> x<- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("[[:lower:]]","-",x)
#[[:lower:]]匹配小写字母,将所有小写字母都替换成了-
> y
[1] "---- 4322: H- -- --- 25 ----- ---, --- ------- 130---"
转自:https://www.jianshu.com/p/7dfee5f1e884
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-01-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信交流平台 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档