I want to create a matrix of a powerset of a vector. Similar to this question on SO.
I have this
#data
a <- c("test1","test2","test3")
#result
data.table::rbindlist(
sapply(1:3, function(i) as.data.frame(t(combn(a, i)))), fill = TRUE)
#> 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
Created on 2021-04-14 by the reprex package (v1.0.0)
I want to transform it in to this (I dont want the dots as well if that is possible):
library(tidyr)
#> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
#> when loading 'dplyr'
tribble(
~test1, ~test2, ~ test3,
"x", ".", ".",
".", "x", ".",
".", ".", "x",
"x", "x", ".",
"x", ".", "x",
".", "x", "x",
"x", "x", "x"
)
#> # A tibble: 7 x 3
#> test1 test2 test3
#> <chr> <chr> <chr>
#> 1 x . .
#> 2 . x .
#> 3 . . x
#> 4 x x .
#> 5 x . x
#> 6 . x x
#> 7 x x x
Created on 2021-04-14 by the reprex package (v1.0.0)