[R] Getting all possible combinations
peter dalgaard
pdalgd at gmail.com
Thu Aug 24 15:20:01 CEST 2017
> On 24 Aug 2017, at 11:58 , peter dalgaard <pdalgd at gmail.com> wrote:
>
> M <- as.matrix(do.call(expand.grid, rep(list(0:1),5)))
> M
> # This is just 0:31 encoded as (little-endian) binary
> apply(M, 1, function(i) sum((2^(0:4))[i]))
>
> # now, use rows of M for logical indexing into "A"-"E"
> mode(M) <- "logical"
Whoops. That apply() line should either come _after_ conversion to logical, or read
apply(M, 1, function(i) sum(2^(0:4) * i))
As in:
> M <- as.matrix(do.call(expand.grid, rep(list(0:1),5)))
> apply(M, 1, function(i) sum(2^(0:4) * i))
[1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[26] 25 26 27 28 29 30 31
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list