📜  R中字符串向量的逐元素串联

📅  最后修改于: 2022-05-13 01:55:14.080000             🧑  作者: Mango

R中字符串向量的逐元素串联

两个向量的元素级联意味着同时从两个向量中获取值并将它们连接或连接为一个。对于这个 paste()函数在 R 编程语言中使用。

示例 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))


输出:

我们可以一次组合 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))

输出:

如果我们不需要在两个连接的字符串之间出空隙,请使用paste0()函数。

句法:

例子:

电阻

# 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))

输出: