Element-wise concatenation of string vector in R
Last Updated :
07 Apr, 2021
Element wise concatenation of two vectors means concurrently taking values from two vectors and joining or concatenating them into one. For this paste() function is used in the R programming language.
Syntax:
paste(vector1,vector2,….,vectorn)
Where, vectors are the inputs to paste function
Example 1:
R
first_names= c ( "sravan" , "bobby" , "mohan" , "sai" )
middle_names= c ( "kumar" , "kumari" , "pavan" , "kumar" )
print ( paste (first_names,middle_names))
|
Output:
[1] “sravan kumar” “bobby kumari” “mohan pavan” “sai kumar”
We can combine n vector elements at a time
Example: 2
R
first_names= c ( "sravan" , "bobby" , "mohan" , "sai" )
middle_names= c ( "kumar" , "kumari" , "pavan" , "kumar" )
last_names= c ( "H" , "B" , "G" , "M" )
print ( paste (first_names,middle_names,last_names))
|
Output:
[1] “sravan kumar H” “bobby kumari B” “mohan pavan G” “sai kumar M”
If we do not need to give a gap between two concatenated strings use paste0() function.
Syntax:
paste0(vector1,vector2,…,vectorn)
Example:
R
first_names= c ( "sravan" , "bobby" , "mohan" , "sai" )
middle_names= c ( "kumar" , "kumari" , "pavan" , "kumar" )
last_names= c ( "H" , "B" , "G" , "M" )
print ( paste0 (first_names,middle_names))
print ( paste0 (first_names,middle_names,last_names))
|
Output:
[1] “sravankumar” “bobbykumari” “mohanpavan” “saikumar”
[1] “sravankumarH” “bobbykumariB” “mohanpavanG” “saikumarM”
Share your thoughts in the comments
Please Login to comment...