介绍
R中的sub()
和gsub()
函数会用特定的字符串代替矢量或数据框中的字符串或字符串,这些函数在对大数据集进行更改时很有用。
在本文中,您将探索如何在 R 中使用 sub()
和 gsub()
函数。
前提条件
要完成本教程,您将需要:
「sub()」和「gsub()」的语法
sub()
的基本语法是:
1sub(pattern, replacement, x)
gsub()
的基本语法是:
1gsub(pattern, replacement, x)
对 sub()
和 gsub()
的语法需要一个模式、一个替代,以及矢量或数据框:
- pattern:您要替代的模式或字符串
- replacement: 替代字符串的输入字符串
- x:替代字符串的矢量或数据框
该模式也可以以规则表达式(regex)的形式进行。
现在你熟悉语法,你可以转到实施。
R 中的sub()
函数
R中的sub()
函数以输入或指定的字符串代替 vector 或 data frame 中的字符串。
然而,sub()
函数的局限性在于它只取代了第一个发生。
1、使用sub()
函数
在本示例中,了解如何用sub()
函数代替字符串模式。
1# the input vector
2df<-"R is an open-source programming language widely used for data analysis and statistical computing."
3
4# the replacement
5sub('R','The R language',df)
运行此命令会产生以下输出:
1[secondary_label Output]
2"The R language is an open-source programming language widely used for data analysis and statistical computing."
函数sub()
将矢量中的字符串R
代替为字符串R语言
。
在本示例中,有一个单一的模式匹配发生. 考虑如果有多个模式匹配发生时会发生什么。
1# the input vector
2df<-"In this tutorial, we will install R and show how to add packages from the official Comprehensive R Archive Network (CRAN)."
3
4# the replacement
5sub('R','The R language',df)
运行此命令会产生以下输出:
1"In this tutorial, we will install The R language and show how to add packages from the official Comprehensive R Archive Network (CRAN)."
在本示例中,您可以观察到sub()
函数用The R language
取代了字符串R
的第一个出现,但字符串中的下一个出现仍然相同。
使用sub()
函数与数据框架
sub()
函数也适用于数据框架。
1# creating a data frame
2df<-data.frame(Creature=c('Starfish','Blue Crab','Bluefin Tuna','Blue Shark','Blue Whale'),Population=c(5,6,4,2,2))
3
4# data frame
5df
这将创建以下数据框架:
1Creature Population
21 Starfish 5
32 Blue Crab 6
43 Bluefin Tuna 4
54 Blue Shark 2
65 Blue Whale 2
然后用绿色
代替蓝色
字符:
1# substituting the values
2sub('Blue','Green',df)
运行此命令会产生以下输出:
1[secondary_label Output]
2"c(\"Starfish\", \"Green Crab\", \"Bluefin Tuna\", \"Blue Shark\", \"Blue Whale\")"
3"c(5, 6, 4, 2, 2)"
您还可以指定一个特定的列,以用绿色
取代蓝色
的所有发生:
1# substituting the values
2sub('Blue','Green',df$Creature)
运行此命令会产生以下输出:
1[secondary_label Output]
2"Starfish"
3"Green Crab"
4"Greenfin Tuna"
5"Green Shark"
6"Green Whale"
蓝色
字符的所有实例都被绿色
字符所取代。
gsub()
函数在R中
R 中的 gsub()
函数用于替换操作. 该函数接收输入并替换给定的值。
与 sub()
函数不同, gsub()
适用于所有匹配的全球替代。
1、使用gsub()
函数
在本示例中,了解如何用gsub()
函数代替字符串模式。
1# the input vector
2df<-"In this tutorial, we will install R and show how to add packages from the official Comprehensive R Archive Network (CRAN)."
这是有R
写了好几次的数据。
1# substituting the values using gsub()
2gsub('R','The R language',df)
1[secondary_label Output]
2"In this tutorial, we will install The R language and show how to add packages from the official Comprehensive The R language Archive Network (CThe R languageAN)."
‘R’
的所有实例都被取代了(包括综合 R 档案网络
和CRAN
中的实例)。
使用gsub()
函数与数据框架
gsub()
函数也适用于数据框架。
1# creating a data frame
2df<-data.frame(Creature=c('Starfish','Blue Crab','Bluefin Tuna','Blue Shark','Blue Whale'),Population=c(5,6,4,2,2))
让我们以深海
为前缀生物
列中的值:
1# substituting the values
2gsub('.*^','Deep Sea ',df$Creature)
运行此命令会产生以下输出:
1[secondary_label Output]
2"Deep Sea Starfish"
3"Deep Sea Blue Crab"
4"Deep Sea Bluefin Tuna"
5"Deep Sea Blue Shark"
6"Deep Sea Blue Whale"
在本示例中,函数 gsub()
使用正规表达式 (regex): .*^
. 这是字符串开始位置的模式。
结论
在本文中,您探索了如何在 R 中使用 sub()
和 gsub()
函数. 这些函数用特定的字符串代替矢量或数据框中的字符串或字符串。
继续用 如何在 R 中使用 replace()
来学习。