Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to interleave vector of strings with a string

I have the following vectors example:

v1 <- c("AA", "BB")
v2 <- c("AA", "BB", "CCC")

Note that the length of each vector can be varied.

What I want to do is to interleave each vector with a string:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

linker <- "xxx"

Resulting in this:

c("AA", "xxx", "BB")
c("AA","xxx",  "BB", "xxx", "CCC")

How can I achieve that?

>Solution :

You could use an rbind trick here:

v1 <- c("AA", "BB")
v2 <- c("AA", "BB", "CCC")
linker <- "xxx"

head(c(rbind(v2, linker)), -1)

[1] "AA"  "xxx" "BB"  "xxx" "CCC"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading