R语言 stringr包的str_c()函数
在这篇文章中,我们将讨论stringr包中的str_c()函数。在使用这个方法之前,我们必须先安装并导入stringr包。
语法
Install – install.packages(“stringr”)
Import – library(“stringr”)
str_c()方法
该函数用于通过分隔符合并多个字符串。
语法
str_c("string1", "string2",...............,sep)
其中
strings是用逗号分隔的输入字符串,sep是第二个参数,用于用分隔符分隔每个字符串。
例1 :
在这个例子中,我们要连接5个没有分隔符的字符串。
# load the stringr library
library(stringr)
# combine 5 strings without separator
print(str_c("Welcome","to","geeks",
"for","geeks"))
输出
[1] "Welcometogeeksforgeeks"
例2 :
在这个例子中,我们用不同的分隔符连接了5个字符串。
# load the stringr library
library(stringr)
# combine 5 strings with comma separator
print(str_c("Welcome","to","geeks",
"for","geeks",sep=","))
# combine 5 strings with space separator
print(str_c("Welcome","to","geeks",
"for","geeks",sep=" "))
# combine 5 strings with & separator
print(str_c("Welcome","to","geeks",
"for","geeks",sep="&"))
[1] "Welcome,to,geeks,for,geeks"
[1] "Welcome to geeks for geeks"
[1] "Welcome&to&geeks&for&geeks"