1

Very simple R question:

What simplest operation should I use with cols1 and cols2 to obtain cols3 below? (using loop is not the answer)

>   cols1 <-  paste0(letters[1:3]);cols1
[1] "a" "b" "c"
>   cols2 <- paste0(cols1, ".new"); cols2
[1] "a.new" "b.new" "c.new"
>   cols3 =  c("a", "a.new" , "b" ,"b.new" , "c", "c.new"); cols3
[1] "a"     "a.new" "b"     "b.new" "c"     "c.new"
Maël
  • 45,206
  • 3
  • 29
  • 67
IVIM
  • 2,167
  • 1
  • 15
  • 41

1 Answers1

3

With c + rbind

c(rbind(cols1, cols2))
#[1] "a"     "a.new" "b"     "b.new" "c"     "c.new"

FYI, this is sometimes called "interleaving", or "interlacing". There is also the built-in vctrs::vec_interleave:

vctrs::vec_interleave(cols1, cols2)
#[1] "a"     "a.new" "b"     "b.new" "c"     "c.new"
Maël
  • 45,206
  • 3
  • 29
  • 67