[R] enumerates all possible combinations
Weiwei Shi
helprhelp at gmail.com
Fri Jan 26 17:58:50 CET 2007
a little improvement on ken knoblauch's method by adding the variable n
so,
n <- 3
expand.grid(lapply(1:n, function(i) (c(0,1))))
HTH,
weiwei
On 1/26/07, Zhiliang Ma <zhiliang.ma at gmail.com> wrote:
> Hi all R users,
> I want to create a matrix having n columns and 2^n rows, and all its entries
> are only 0 or 1. In each row, column i is 0 means dimension i is chosen, 0
> means not. The matrix will contains all the possible combination of those n
> dimensions.
>
> Here is an example, if n = 3, the matrix will look like:
> 0 0 0
> 0 0 1
> 0 1 0
> 1 0 0
> 0 1 1
> 1 0 1
> 1 1 1
>
>
> I know I can use n "for" loops to do this, but is there a better way?
> Thanks,
> -zm
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Weiwei Shi, Ph.D
Research Scientist
GeneGO, Inc.
"Did you always know?"
"No, I did not. But I believed..."
---Matrix III
More information about the R-help
mailing list