Re: [R] enumerate subsets

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sun, 20 Jul 2008 23:13:23 +0200

lamack lamack wrote:
> Dear all, is there a R function that enumerate a partition of a vector of size n? (of course for n not very large).
> I would like enumerate all the (2 power n)-1 sub-sets. (2 power n)-1 since (2 power n) includes de empty subset.
>
> Best Regards.
>
> ps. It is not a homework. I never posted homework in this list.
>
>
The easiest way is probably to generate the numbers 1:(2^n-1), convert them to binary, and use the bits to indicate in/out status.

E.g.,

n <- 4
x <- 1:(2^n-1)
sapply(1:n, function(i) {r <- as.logical(x%%2) ; x <<- x%/%2 ; r})  

-- 
   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

______________________________________________
R-help_at_r-project.org 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.
Received on Sun 20 Jul 2008 - 21:16:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 20 Jul 2008 - 21:32:45 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive