R语言字符串替换:gsub()

编程知识 更新时间:2023-04-04 06:04:12
 gsub(pattern, replacement, x, 
 ignore.case = FALSE, 
 perl = FALSE,fixed = FALSE, 
 useBytes = FALSE)

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

举例如下:

x <- “R Tutorial”
gsub(“ut”,“ot”,x)
[1] “R Totorial”

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

举例vector:

x <- c(“R Tutorial”,“PHP Tutorial”, “HTML Tutorial”)
gsub(“Tutorial”,“Examples”,x)
[1] “R Examples” “PHP Examples” “HTML Examples”
还有其他的一些例子来灵活使用这个函数,更详细的说明看下面的Ref就可以了。

x <- “line 4322: He is now 25 years old, and weights 130lbs”
y <- gsub("\d+","—",x) #在x中凡是遇到数字就替换成“—”
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)
y
[1] “---- 4322: H- – — 25 ----- —, — ------- 130—”

http://www.endmemo/program/R/gsub.php

更多推荐

R语言字符串替换:gsub()

本文发布于:2023-04-04 06:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/5ab5ec429217b465b9264ebd45c6dd2e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   语言   gsub

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 41431文章数
  • 14阅读数
  • 0评论数