There are multiple ways to concatenate or merge elements of the vector into a string in R either by separating with space or any delimiter. Use the paste() function to merge elements of a vector into a string. It provides two optional parameters to change the behavior of the merge result.
Also, I will use stri_paste()
function from stringi
package to merge character elements from vector into a single string.
1. Quick Examples Merge Vector into String
The following are quick examples of how to merge elements of the vector into the string.
# Quick examples
# Using paste() function
v <- c('A','B','C','D')
print(v)
print(paste(v,collapse=' '))
print(paste(v))
# With 2 vectors
v <- c('A','B','C','D')
v2 <- c('X','Y','Z')
print(paste(v,v2,collapse='-'))
print(paste(v,v2,sep='|'))
print(paste(v,v2,sep='|',collapse='-'))
print(paste(v,v2))
# cbin()
v <- c('A','B','C')
v2 <- c('X','Y','Z')
print(cbind(v,v2))
# rbind()
v <- c('A','B','C')
v2 <- c('X','Y','Z')
print(rbind(v,v2))
# do.call()
do.call(paste, c(as.list(v), sep = " "))
2 Syntax of paste()
Following is the syntax of the paste() function.
# Syntax
paste (…, sep = " ", collapse = NULL)
Parameters
...
– one or more input objects to merge.sep
– character to separate the values. In order to usesep
param, you should use multiple vectors as input.collapse
–
3. Usage paste() to Merge Vector Elements into a string
The paste()
function in R merges the string elements from the vector to a string. To separate the string elements from a single vector use the collapse param with the delimiter. And to separate the elements from multiple vectors use sep
param. Let’s create a character vector by using combined function c(). The following example merges all values from a vector into a single string with the values separated by space.
#Using paste()
v <- c('A','B','C','D')
print(paste(v,collapse=' '))
Yields below output. By not using collapse it returns the original vectors without any changes.
# Output
[1] "A B C D"
4. Merge Two Vectors into a String
When you use two or more (multiple) R vectors as arguments to paste()
function it merges the string elements from all vectors column-wise by space separator and each position of the vector by collapse character. It merges the first position of two vectors, the second position of two vectors, and so on.
# Other examples
v <- c('A','B','C','D')
v2 <- c('X','Y','Z')
print(paste(v,v2,collapse='-'))
Yields below output.
# Output
[1] "A X-B Y-C Z-D X"
5. Merge Vector by using sep Param
If you wanted to merge elements from the vector by position with a separator use sep param.
# Using sep Param
v <- c('A','B','C','D')
v2 <- c('X','Y','Z')
print(paste(v,v2,sep='-'))
print(paste(v,v2,sep='-',collapse='-'))
Yields below output.
# Output
> print(paste(v,v2,sep='|'))
[1] "A|X" "B|Y" "C|Z" "D|X"
> print(paste(v,v2,sep='|',collapse='-'))
[1] "A|X-B|Y-C|Z-D|X"
> print(paste(v,v2))
[1] "A X" "B Y" "C Z" "D X"
6. Using do.call()
Alternatively, you can also use do.call()
function to merge vector elements into a string. Use sep
to separate the string in vector by a delimiter.
# Using do.call()
do.call(paste, c(as.list(v), sep = " "))
7. stri_paste() to merge elements
Use stri_paste()
function from stringi
package to merge vector elements into strings in R. This function also takes collapse
param to separate the strings by character. In order to use this package first you to install r package and load it by using library()
function.
# Load stringi
library('stringi')
stri_paste(v, collapse='')
3. Using cbind()
To merge string vector column-wise use cbind()
function.
#cbin()
v <- c('A','B','C')
v2 <- c('X','Y','Z')
print(cbind(v,v2))
Yields below output.
# Output
v v2
[1,] "A" "X"
[2,] "B" "Y"
[3,] "C" "Z"
4. Using rbind()
Use rbind()
function to concatenate string vectors by rows
#rbind()
v <- c('A','B','C')
v2 <- c('X','Y','Z')
print(rbind(v,v2))
Yields below output.
# Output
[,1] [,2] [,3]
v "A" "B" "C"
v2 "X" "Y" "Z"
Conclusion
In this article, you have learned how to merge string elements from the vector into a string in R by using the paste(), cbind(), and rbind() functions. Also learned how to use stri_paste() from stringi
package. You can also find this example at Github R Programming Examples Project.
Related Articles
- Explain Character Vector in R?
- How to Get Vector Length in R?
- Add or Append Element to Vector in R?
- How to Remove NA from Vector?
- How to Create a Vector in R?
- How to Create a DataFrame From Vectors?
- How to Create Empty Vector in R?
- Create Character Vector in R?
- How to Convert Vector to List in R?
- How to Convert List to Vector in R?
- How to Concatenate Vector in R?
- How to Subset Vector in R?
- How to Sort Vector in R?
- How to Convert List to String?
- How to Remove Values from R Vector?
- How to Remove Duplicates from Vector in R?
- Remove character from string in r
- How to concatenate the strings in R?
- Remove all white space characters in R