Concatenate Vector of Character Strings in R - GeeksforGeeks

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.
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 functionprint(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 functionprint(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 functionprint(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”    
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 framesdeparse.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 functionprint(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”      
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 framesdeparse.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 functionprint(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"  
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 functionprint(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 datad=c(1,2,3,4,5) # create a vector with numeric datae=c(1.6,2.2,3.78,4.4456,5.4) # concatenate these four vectors using# cat functionprint(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
adnanirshad158
Picked
R Vector-Programs
R-Vectors
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R