Alternate, interweave or interlace two vectors

Solution 1:

Your rbind method should work well. You could also use

rpois(lambda=c(3,4),n=1e6)

because R will automatically replicate the vector of lambda values to the required length. There's not much difference in speed:

library(rbenchmark)
benchmark(rpois(1e6,c(3,4)),
     c(rbind(rpois(5e5,3),rpois(5e5,4))))


#                                        test replications elapsed relative
# 2 c(rbind(rpois(5e+05, 3), rpois(5e+05, 4)))          100  23.390 1.112168
# 1                      rpois(1e+06, c(3, 4))          100  21.031 1.000000

and elegance is in the eye of the beholder ... of course, the c(rbind(...)) method works in general for constructing alternating vectors, while the other solution is specific to rpois or other functions that replicate their arguments in that way.

Solution 2:

Some speed tests, incorporating Ben Bolker's answer:

 benchmark(
 c(rbind(rpois(lambda=3,n=5e5),rpois(lambda=4,n=5e5))),
 c(t(sapply(X=list(3,4),FUN=rpois,n=5e5))),
 sapply(X=rep.int(c(3,4),times=5e5),FUN=rpois,n=1),
 rpois(lambda=c(3,4),n=1e6),
 rpois(lambda=rep.int(c(3,4),times=5e5),n=1e6)
 )
                                                                  test
1 c(rbind(rpois(lambda = 3, n = 5e+05), rpois(lambda = 4, n = 5e+05)))
2                 c(t(sapply(X = list(3, 4), FUN = rpois, n = 5e+05)))
4                                   rpois(lambda = c(3, 4), n = 1e+06)
5           rpois(lambda = rep.int(c(3, 4), times = 5e+05), n = 1e+06)
3      sapply(X = rep.int(c(3, 4), times = 5e+05), FUN = rpois, n = 1)
  replications elapsed   relative user.self sys.self user.child sys.child
1          100    6.14   1.000000      5.93     0.15         NA        NA
2          100    7.11   1.157980      7.02     0.02         NA        NA
4          100   14.09   2.294788     13.61     0.05         NA        NA
5          100   14.24   2.319218     13.73     0.21         NA        NA
3          100  700.84 114.143322    683.51     0.50         NA        NA