Skip to content
Related Articles

Related Articles

Improve Article

Concatenate Vector of Character Strings in R

  • Last Updated : 16 Jun, 2021

In this article, we will discuss how to concatenate the strings present in two or more vectors in R Programming Language. 

Discussed below are various methods of doing so.

Method 1: Using paste()

paste() function is used to combine strings present in vectors passed to it  an argument.

Syntax: paste(vector1,vector2,.,vector n,sep=”symbol”)

Parameter:



  • vectors are the input vectors to be concatenate
  • sep is the separator symbol that separates the strings present in the vector.

Example 1:

R




# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
 
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
 
# concatenate these two vectors
# using paste function
print(paste(a,b,sep="--"))

Output:

[1] “manoj–vijayawada” “sravan–ponnur”    “harsha–hyd” 

Example 2:

R




# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
 
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
 
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
 
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
 
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
 
# concatenate these five vectors
# using paste function
print(paste(a,b,d,e,f,sep="--"))

Output:

[1] "manoj--vijayawada--java--iit--cse"      
[2] "sravan--ponnur--.net--srm-ap--food tech"
[3] "harsha--hyd--python--vignan--ece"       

Without a separator, the vectors will combine without any space or any symbol.



Example 3:

R




# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
 
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
 
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
 
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
 
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
 
# concatenate these five vectors
# using paste function
print(paste(a,b,d,e,f))

Output:

[1] “manoj vijayawada java iit cse”       “sravan ponnur .net srm-ap food tech”

[3] “harsha hyd python vignan ece”    

Method 2: Using cbind()

cbind() function is used to combine the vectors column-wise i.e. it places the first vector in the first column and second vector in column 2 and so on.

Syntax: cbind(x1, x2, …, deparse.level = 1)

Parameters:
x1, x2: vector, matrix, data frames
deparse.level: This value determines how the column names generated. The default value of deparse.level is 1.

Example:

R




# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
 
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
 
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
 
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
 
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
 
# concatenate these five vectors
# using cbind function
print(cbind(a,b,d,e,f))

Output:

              a                 b           d        e        f          



[1,] “manoj”  “vijayawada” “java”   “iit”    “cse”      

[2,] “sravan” “ponnur”     “.net”   “srm-ap” “food tech”

[3,] “harsha” “hyd”        “python” “vignan” “ece”      

Method 3: Using rbind() 

rbind() concatenates the strings of vectors row-wise i.e. row 1 is for vector 1, row2 is vector 2, and so on.

Syntax: rbind(x1, x2, …, deparse.level = 1)

Parameters:
x1, x2: vector, matrix, data frames
deparse.level: This value determines how the column names generated. The default value of deparse.level is 1.

Example:

R




# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
 
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
 
# create a vector with character
# strings(subjects)
d=c("java",".net","python")
 
# create a vector with character
# strings (college)
e=c("iit","srm-ap","vignan")
 
# create a vector with character
# strings(department)
f=c("cse","food tech","ece")
 
# concatenate these five vectors
# using rbind function
print(rbind(a,b,d,e,f))

Output:

  [,1]         [,2]        [,3]    
a "manoj"      "sravan"    "harsha"
b "vijayawada" "ponnur"    "hyd"    
d "java"       ".net"      "python"
e "iit"        "srm-ap"    "vignan"
f "cse"        "food tech" "ece"  

Method 4: Using cat() 

cat() function is used to concatenate the given vectors.

Syntax:

cat(vector1,vector2,.,vector n)



Where, the vector is the input vector

It results in a one-dimensional vector with the last value as NULL

Example 1:

R




# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
 
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
 
# concatenate these two vectors
# using cat function
print(cat(a,b))

Output:

manoj sravan harsha vijayawada ponnur hydNULL

Example 2:

R




# create a vector with character
# strings(names)
a=c("manoj","sravan","harsha")
 
# create a vector with character
# strings (address)
b=c("vijayawada","ponnur","hyd")
 
# create a vector with numeric data
d=c(1,2,3,4,5)
 
# create a vector with numeric data
e=c(1.6,2.2,3.78,4.4456,5.4)
 
# concatenate these four vectors using
# cat function
print(cat(a,b,d,e))

Output:

manoj sravan harsha vijayawada ponnur hyd 1 2 3 4 5 1.6 2.2 3.78 4.4456 5.4NULL




My Personal Notes arrow_drop_up
Recommended Articles
Page :