Shawn
Shawn

Reputation: 3369

Concatenate strings in pairs?

I am trying to concatenate strings in pairs.

Here is what I am doing:

paste(c("C","S"), seq(1,5), sep="")

The result is:

C1, S2, C3, S4, C5

The result I want is:

C1, S1, C2, S2, C3, S3, C4, S4, C5, S5

Upvotes: 0

Views: 136

Answers (2)

Marcos Pastor
Marcos Pastor

Reputation: 61

If the order is not important, you can try this:

paste(sort(paste(c("C","S"),rep(seq(1,5),2), sep = "")), collapse = ", ")

And you will get this result:

C1, C2, C3, C4, C5, S1, S2, S3, S4, S5

Upvotes: 1

DanY
DanY

Reputation: 6073

paste0(c("C","S"), rep(1:5, each=2))

Upvotes: 3

Related Questions