当前位置:网站首页>R语言 sub()用法
R语言 sub()用法
2022-07-28 16:29:00 【皮肤科大白】
R语言 sub()用法
subR语言中的函数用于替换字符串中模式的第一个匹配项。如果有一个字符串元素向量,那么它将替换所有元素中模式的第一个匹配项。
用法: sub(pattern, replacement, string, ignore.case=TRUE/FALSE)
参数:
pattern:要匹配的字符串
replacement:替换字符串
string:字符串或字符串向量
忽略.case:区分大小写替换的布尔值
# Create a string
x <- "Geeksforgeeks"
# Calling sub() function
sub("eek", "ood", x)
# Calling sub() with case-sensitivity
sub("gee", "Boo", x, ignore.case = FALSE)
# Calling sub() with case-insensitivity
sub("gee", "Boo", x, ignore.case = TRUE)
输出:
[1] "Goodsforgeeks"
[1] "GeeksforBooks"
[1] "Booksforgeeks"
# Create a string
x <- c("Geekforgeek", "Geeksforgeeks", "geeksforGeeks")
# Calling sub() function
sub("Gee", "boo", x)
# Calling sub() with case-insensitivity
sub("Gee", "boo", x, ignore.case = TRUE)
输出:
[1] "bookforgeek" "booksforgeeks" "geeksforbooks"
[1] "bookforgeek" "booksforgeeks" "booksforGeeks"
sub()和gsub()的区别在于,前者只替换第一次匹配的字符串,而后者会替换掉所有匹配的字符串。
注意:gsub()是对向量里面的每个元素进行搜素,如果发现元素里面有多个位置匹配了模式,则全部进行替换,而grep()也是对向量里每个元素进行搜索,但它仅仅知道元素是否匹配了模式(并返回该元素在向量中的下标),但具体元素中匹配了多少次却无法知道。
边栏推荐
- Connection design and test platform -- Summary of SystemVerilog interface knowledge points
- Verilog 每日一题(VL8 使用generate…for语句简化代码)
- 蚂蚁金服移动测试工具solopi监控部分源码导读。。持续更新
- QR code generation of wechat applet with parameters
- PCA 报错Error in eigen(crossprod(t(X), t(X)), symmetric = TRUE) : ‘x‘里有无穷值或遗漏值
- Application system log structure of elastic stack
- Visual Object Class介绍PASCAL VOC数据集
- 数据库性能分析与优化(爱测未来团队内训材料)
- 在android开发过程中遇到.sqlite文件处理
- ionic 中遇到的一些东西
猜你喜欢

Shell脚本之AWK

Basic principle of asynchronous FIFO (simple implementation based on Verilog)

Verilog 每日一题(VL29 单端口RAM)

JVM性能调优
![[Presto] details of the new version upgrade of Presto](/img/12/61ed53ff7a8a810092d60501cca443.png)
[Presto] details of the new version upgrade of Presto

蚂蚁金服移动测试工具solopi监控部分源码导读。。持续更新

Vscode uses eslint prettier to format code automatically

数据库性能分析与优化(爱测未来团队内训材料)

Selection and application of capacitor in high speed circuit -- detailed explanation

With a total data volume of more than trillions of lines, Yuxi cigarette factory can easily deal with it by correctly selecting the timing database
随机推荐
将input type='file' 类型的图片文件转成base64
Firewalld防护墙
Verilog daily question (simple implementation of VL30 RAM)
Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)
Asynchronous circuit design -- principle and example of synchronous pulser
Mysql database development specification
MySQL PgSQL realizes the merging of multiple lines of records into one line, grouping and merging, and dividing with specified characters
2021 年全国大学生数据统计与分析竞赛
关于标准IO缓冲区的问题
Can‘t use an undefined value as an ARRAY reference at probe2symbol
Linear algebra and matrix theory (VIII)
Selection of resistance in high speed circuit
Iris framework practice of goweb development: project summary and review
Export word according to the template, generate compound format tables and variable column tables
Zero foundation uses unity3d to develop AR applications and download 3D models remotely
Wild pointer recurrence
Flat data to tree
Sed of shell programming
Verilog daily question (vl6 data series to parallel circuit)
Linear algebra and matrix theory (7)