Concat values of two arrays

Viewed 40

Having two equal sized arrays with string values,

e.g.
c('a', 'b', 'c')
c('d', 'e', 'f')

I want to merge their values and make one array like this:
('ad','be','cf')

2 Answers

We can use paste0 which is Vectorized and does an elementwise concat

paste0(v1, v2)
#[1] "ad" "be" "cf"

Or another option is str_c

library(stringr)
str_c(v1, v1)

data

v1 <- c('a', 'b', 'c')
v2 <- c('d', 'e', 'f')

We could use %s+% operator from stringi package:

v1 %s+% v2

or

stri_join from stringi package:

library(stringi)
stri_join(v1,v2,sep="")

Output:

[1] "ad" "be" "cf"

data:

v1 <- c('a', 'b', 'c')
v2 <- c('d', 'e', 'f')
Related