Patrick Balada
Patrick Balada

Reputation: 1450

How to get all possible subsets of a character vector in R?

Having the following vector:

c("test1","test2","test3")

I am trying to get a list or data frame containing the following entries:

"test1" "test2" "test3"
"test1" "test2" NA
"test1" NA "test3"
"test1"  NA NA
NA  "test2" "test3"
NA  "test2" NA
NA  NA "test3"

The goal would be to get all possible subsets while the order doesn't matter, that is "text1" "text2" NA is equivalent to "text2" "text1" NA. I very much appreciate any help!

Upvotes: 6

Views: 2664

Answers (3)

zx8754
zx8754

Reputation: 56179

Using combn, and data.table::rbindlist with fill = TRUE option to make NA values.

#data
a <- c("test1","test2","test3")

#result
data.table::rbindlist(
        sapply(1:3, function(i) as.data.frame(t(combn(a, i)))), fill = TRUE)

#output
#       V1    V2    V3
# 1: test1    NA    NA
# 2: test2    NA    NA
# 3: test3    NA    NA
# 4: test1 test2    NA
# 5: test1 test3    NA
# 6: test2 test3    NA
# 7: test1 test2 test3

Upvotes: 8

Roland
Roland

Reputation: 132706

You can use combn:

res <- unlist(lapply(1:3, combn, 
                     x = c("test1","test2","test3"), simplify = FALSE), 
              recursive = FALSE)
res <- sapply(res, `length<-`, 3)
#        [,1]    [,2]    [,3]    [,4]    [,5]    [,6]    [,7]   
#[1,] "test1" "test2" "test3" "test1" "test1" "test2" "test1"
#[2,] NA      NA      NA      "test2" "test3" "test3" "test2"
#[3,] NA      NA      NA      NA      NA      NA      "test3"

Upvotes: 14

gfgm
gfgm

Reputation: 3647

There is a package sets with the relevant function.

library(sets)
a <- c("test1","test2","test3")
set_power(a)

{{}, {"test1"}, {"test2"}, {"test3"}, {"test1", "test2"}, {"test1", "test3"}, {"test2", "test3"}, {"test1", "test2", "test3"}}

This returns the set of all subsets.

Upvotes: 8

Related Questions