Home > Mobile >  Create an alternating series of vector in R
Create an alternating series of vector in R

Time:10-22

I have a vector that looks like this:

tt <- c("chr1:110363793:G:C", "chr1:110363823:A:G", "chr1:110363849:A:G")

How do I create a vector with POS and NEG characters appended alternatively to get this below?

"chr1:110363793:G:C_POS", "chr1:110363823:A:G_NEG", "chr1:110363849:A:G_POS", "chr1:110363793:G:C_NEG", "chr1:110363823:A:G_POS", "chr1:110363849:A:G_NEG"

CodePudding user response:

Just make use of the recyling of vector with rep and paste

paste0(rep(tt, 2), c("_POS", "_NEG"))

-output

[1] "chr1:110363793:G:C_POS" "chr1:110363823:A:G_NEG" "chr1:110363849:A:G_POS" "chr1:110363793:G:C_NEG" "chr1:110363823:A:G_POS"
[6] "chr1:110363849:A:G_NEG"
  •  Tags:  
  • r
  • Related