Algorithm to calculate power set (all possible subsets) of a set in R
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 the library "sets", with the command 2^as.set(c(1,2,3,4)) , which yields the output {{}, {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}} . However, this uses a recursive algorithm, which is rather slow. Here's the algorithm I came up with. It's non-recursive, so it's much faster than some of the other solutions out there (and ~100x faster on my machine than the