[R] Sub sets
R. Michael Weylandt
michael.weylandt at gmail.com
Mon Nov 21 17:42:36 CET 2011
I'd appreciate it if you'd keep on list for the archives. That said, I
think this function does what you were hoping for.
Michael
powerset <- function(n, items = NULL){
if(!is.null(items)) {
if(n != length(items)) warning("Resetting n in preference to
length(items)")
n = length(items)
}
smat <- do.call(expand.grid, rep(list(c(0,1)), n))
if(!is.null(items))
smat <- smat * matrix(items, ncol = n, nrow = 2^n, byrow = T)
smat
}
On Sat, Nov 19, 2011 at 8:49 PM, Gyanendra Pokharel
<gyanendra.pokharel at gmail.com> wrote:
> Hi Michael,
> I have trouble to find the subsets of a given set. For example x <- c(2, 3,
> 4, 5), there will be 16 subsets of this set
> in the matrix form
> 0 0 0 0
> 1 0 0 0
> 0 1 0 0
> 0 0 1 0
> 0 0 0 1
> 1 1 0 0
> 0 1 1 0
> 0 0 1 1
> 0 1 0 1
> 1 0 1 0
> 1 0 1 1
> 1 1 1 0
> 0 1 1 1
> 1 0 1 1
> 1 1 0 1
> 1 1 1 1
> times transpose(x) , but how could I produce in R?
> Thanks in advance
More information about the R-help
mailing list