Merge Vector Elements into String in R

  • Post author:

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 use sep 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

References

r merge vector string

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Merge Vector Elements into String in R