I'm trying to generate a dataframe or matrix of all possible unique combinations of lengths 1:n
for a character vector of length n
. I've looked at combn, and expand_grid
(see: here), and putting together approaches from here, and here, have come up with a solution:
library(plyr)
char_vec <- c('a','b','c')
store <- list()
for (i in 1:length(char_vec)){
store[[i]] <- as.data.frame(t(as.data.frame(combn(char_vec, i, simplify = FALSE))))
}
df <- do.call("rbind.fill", store)
>df
V1 V2 V3
1 a <NA> <NA>
2 b <NA> <NA>
3 c <NA> <NA>
4 a b <NA>
5 a c <NA>
6 b c <NA>
7 a b c
>
This is my desired output; however, it is very slow with longer inputs, and strikes me as a clunky approach. Does anyone have any recommendations for speeding up and/or simplifying my solution?