Algorithm to calculate power set (all possible subsets) of a set in R

后端 未结 4 1488
故里飘歌
故里飘歌 2020-12-11 05:52

I couldn\'t find an answer to this anywhere, so here\'s my solution.

The question is: how can you calculate a power set in R?

It is possible to do this with

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 06:08

    The below should create a power set, minus the empty set element.

    powerset <- function(x) {
      sets <- lapply(1:(length(x)), function(i) combn(x, i, simplify = F))
      unlist(sets, recursive = F)
    }
    

提交回复
热议问题