combn(n, k, ...) and all its re-inventions
Martin Maechler <maechler at stat.math.ethz.ch> writes:
tweaks, I have now a version of combn() that is faster than all
the other implementations {only slightly faster than
combinations()}, and I plan to add this to R's standard package
'utils'.
Hopefully, the reinventing can be stopped by this, once people
can rely on a relatively fast implementation of the
functionality.
One might also consider to include a version of the ``one by
one'' combination generators {as mentioned above} which is
needed for larger k.
Opinions ?
While you're in there... combn() has a nice feature that you can apply a function to each combination, which can be quite useful (and fun) if you're demonstrating permutation tests:
x <- combn(20,10,function(i)mean(sleep$extra[i])) x <- round(x,2) # this is needed, or you get artifacts plot(sort(unique(x)),table(x),type="h") plot(sort(x),type="l") sum(x <= mean(sleep$extra[1:10])) / length(x)
[1] 0.04072398 However, combn() does its work in sapply() style: First create a list, then simplify. As this quickly becomes a rather *long* list (e.g., a slightly larger case with choose(29, 14)==77558760 combinations kills R for me on a 2GB 64 bit machine), it might be desirable to have an option, "assume.scalar" or so, to specify that the function always returns a single scalar.
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907