Concatenate strings in pairs?
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
c(outer(c("C","S"), 1:5, paste0))
2 Answers
2
paste0(c("C","S"), rep(1:5, each=2))
This did it. I did not realize rep had an each parameter. Thanks!
– Shawn
Sep 6 '18 at 3:49
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
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
c(outer(c("C","S"), 1:5, paste0))
– r2evans
Sep 6 '18 at 3:46