R中字符串向量的逐元素串联
两个向量的元素级联意味着同时从两个向量中获取值并将它们连接或连接为一个。对于这个 paste()函数在 R 编程语言中使用。
Syntax:
paste(vector1,vector2,….,vectorn)
Where, vectors are the inputs to paste function
示例 1:
R
# create a vector named as first_names
first_names=c("sravan","bobby","mohan","sai")
# create a vector named as middle_names
middle_names=c("kumar","kumari","pavan","kumar")
# apply paste function
print(paste(first_names,middle_names))
R
# create a vector named as first_names
first_names=c("sravan","bobby","mohan","sai")
# create a vector named as middle_names
middle_names=c("kumar","kumari","pavan","kumar")
# create a vector named as middle_names
last_names=c("H","B","G","M")
# apply paste function
print(paste(first_names,middle_names,last_names))
R
# create a vector named as first_names
first_names=c("sravan","bobby","mohan","sai")
# create a vector named as middle_names
middle_names=c("kumar","kumari","pavan","kumar")
# create a vector named as middle_names
last_names=c("H","B","G","M")
# apply paste0 function fro combining first and middlename
print(paste0(first_names,middle_names))
# apply paste0 function fro combining first middlename and last names
print(paste0(first_names,middle_names,last_names))
输出:
[1] “sravan kumar” “bobby kumari” “mohan pavan” “sai kumar”
我们可以一次组合 n 个向量元素
示例:2
电阻
# create a vector named as first_names
first_names=c("sravan","bobby","mohan","sai")
# create a vector named as middle_names
middle_names=c("kumar","kumari","pavan","kumar")
# create a vector named as middle_names
last_names=c("H","B","G","M")
# apply paste function
print(paste(first_names,middle_names,last_names))
输出:
[1] “sravan kumar H” “bobby kumari B” “mohan pavan G” “sai kumar M”
如果我们不需要在两个连接的字符串之间留出空隙,请使用paste0()函数。
句法:
paste0(vector1,vector2,…,vectorn)
例子:
电阻
# create a vector named as first_names
first_names=c("sravan","bobby","mohan","sai")
# create a vector named as middle_names
middle_names=c("kumar","kumari","pavan","kumar")
# create a vector named as middle_names
last_names=c("H","B","G","M")
# apply paste0 function fro combining first and middlename
print(paste0(first_names,middle_names))
# apply paste0 function fro combining first middlename and last names
print(paste0(first_names,middle_names,last_names))
输出:
[1] “sravankumar” “bobbykumari” “mohanpavan” “saikumar”
[1] “sravankumarH” “bobbykumariB” “mohanpavanG” “saikumarM”