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
a= c ( "manoj" , "sravan" , "harsha" )
b= c ( "vijayawada" , "ponnur" , "hyd" )
print ( paste (a,b,sep= "--" ))
|
Output:
[1] “manoj–vijayawada” “sravan–ponnur” “harsha–hyd”
Example 2:
R
a= c ( "manoj" , "sravan" , "harsha" )
b= c ( "vijayawada" , "ponnur" , "hyd" )
d= c ( "java" , ".net" , "python" )
e= c ( "iit" , "srm-ap" , "vignan" )
f= c ( "cse" , "food tech" , "ece" )
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
a= c ( "manoj" , "sravan" , "harsha" )
b= c ( "vijayawada" , "ponnur" , "hyd" )
d= c ( "java" , ".net" , "python" )
e= c ( "iit" , "srm-ap" , "vignan" )
f= c ( "cse" , "food tech" , "ece" )
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
a= c ( "manoj" , "sravan" , "harsha" )
b= c ( "vijayawada" , "ponnur" , "hyd" )
d= c ( "java" , ".net" , "python" )
e= c ( "iit" , "srm-ap" , "vignan" )
f= c ( "cse" , "food tech" , "ece" )
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
a= c ( "manoj" , "sravan" , "harsha" )
b= c ( "vijayawada" , "ponnur" , "hyd" )
d= c ( "java" , ".net" , "python" )
e= c ( "iit" , "srm-ap" , "vignan" )
f= c ( "cse" , "food tech" , "ece" )
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
a= c ( "manoj" , "sravan" , "harsha" )
b= c ( "vijayawada" , "ponnur" , "hyd" )
print ( cat (a,b))
|
Output:
manoj sravan harsha vijayawada ponnur hydNULL
Example 2:
R
a= c ( "manoj" , "sravan" , "harsha" )
b= c ( "vijayawada" , "ponnur" , "hyd" )
d= c (1,2,3,4,5)
e= c (1.6,2.2,3.78,4.4456,5.4)
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 Jun, 2021
Like Article
Save Article