Interleave lists in R
While investigating a similar question, I came across this beautiful solution by Gabor Grothendieck (i.e. @GGrothendieck?) for certain cases:
c(rbind(a,b))
This works equally well when a
and b
are both lists, or when a
and b
are both vectors. It's not a precise solution to OP's question, because when a
and b
have different lengths, it will recycle the elements of the shorter sequence, printing a warning. However, since this solution is simple and elegant, and provides an answer to a very similar question--a question of some people (like me) who find their way to this page as a result--it seemed worth adding as an answer.
Here's one way:
idx <- order(c(seq_along(a), seq_along(b)))
unlist(c(a,b))[idx]
# [1] "a.1" "b.1" "a.2" "b.2" "a.3" "b.3" "b.4"
As @James points out, since you need a list back, you should do:
(c(a,b))[idx]
interleave(a, b)
# unlist(interleave(a, b))
# [1] "a.1" "b.1" "a.2" "b.2" "a.3" "b.3" "b.4"
interleave <- function(a, b) {
shorter <- if (length(a) < length(b)) a else b
longer <- if (length(a) >= length(b)) a else b
slen <- length(shorter)
llen <- length(longer)
index.short <- (1:slen) + llen
names(index.short) <- (1:slen)
lindex <- (1:llen) + slen
names(lindex) <- 1:llen
sindex <- 1:slen
names(sindex) <- 1:slen
index <- c(sindex, lindex)
index <- index[order(names(index))]
return(c(a, b)[index])
}