![]() |
VOOZH | about |
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.
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:
Output:
[1] "manoj--vijayawada" "sravan--ponnur" "harsha--hyd"
Example 2:
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:
Output:
[1] "manoj vijayawada java iit cse" "sravan ponnur .net srm-ap food tech"
[3] "harsha hyd python vignan ece"
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:
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"
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:
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"
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:
Output:
manoj sravan harsha vijayawada ponnur hydNULL
Example 2:
Output:
manoj sravan harsha vijayawada ponnur hyd 1 2 3 4 5 1.6 2.2 3.78 4.4456 5.4NULL