Skip to content
Prev 269039 / 398503 Next

More efficient option to append()?

Hi:

This seems to take a bit less code, avoids explicit loops (by using
mapply() instead, where the loops are internal) and takes about 10
seconds on my system:

m <- cbind(x = sample(1:15,2000000, replace=T),
           y = sample(1:10*1000, 2000000, replace=T))
sum(m[, 1])
# [1] 16005804
ff <- function(x, y) rep(y, x)
system.time(w <- do.call(c, mapply(ff, m[, 1], m[, 2])))
   user  system elapsed
   9.75    0.00    9.75
[1] 16005804
x    freq
1   1000 1603184
2   2000 1590599
3   3000 1596661
4   4000 1607112
5   5000 1598571
6   6000 1599195
7   7000 1600475
8   8000 1601718
9   9000 1598896
10 10000 1609393

HTH,
Dennis

PS: It would have been a good idea to keep the OP in the loop of this thread.

On Thu, Aug 18, 2011 at 12:46 AM, Timothy Bates
<timothy.c.bates at gmail.com> wrote: