Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to reverse-engineer the following code

paste("a", rep(1:4, each=4), 1:4, sep="")`

which gives the following result:

[1] "a11" "a12" "a13" "a14" "a21" "a22" "a23" "a24" "a31" "a32" "a33" "a34" "a41" "a42" "a43" "a44" 

For reference, this code is from the Examples section of the LTRE{popbio} help file.

For my data, I need to repeat the following sequence 14 times: "a11 a12 a21 a22". When I try to modify the original code such that

paste("a", rep(1:2, each=14), 1:2, sep="") 

I instead get

[1] "a11" "a12" "a11" "a12" "a11" "a12" "a11" "a12" "a11" "a12" "a11" "a12" "a11" "a12" "a21" "a22" "a21" "a22" "a21" "a22" "a21" "a22" "a21" "a22" "a21" "a22" "a21" "a22"`. 

Technically these are the correct combos, but I need the sequence to be "a11, a12, a21, a22", "a11, a12, a21, a22", etc., not repeating "a11 a12" 7 times before switching to "a21 a22" 7 times. This seems like it should be simple but after trying all kinds of code modifications, I can't figure it out. Any suggestions would be appreciated.

rep(paste0("a", rep(1:2, each = 2), 1:2), 7)
#[1] "a11" "a12" "a21" "a22" "a11" "a12" "a21" "a22" "a11" "a12" "a21" "a22" "a11" "a12" "a21" "a22" "a11" "a12" "a21" "a22" "a11" "a12"
#[23] "a21" "a22" "a11" "a12" "a21" "a22"
paste0("a", rep(1:2,times = 7, each = 2), 1:2)
#[1] "a11" "a12" "a21" "a22" "a11" "a12" "a21" "a22" "a11" "a12" "a21" "a22" "a11"
#[14] "a12" "a21" "a22" "a11" "a12" "a21" "a22" "a11" "a12" "a21" "a22" "a11" "a12"
#[27] "a21" "a22"
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.