I know that combn() can give me all the unique combinations across a vector. However, it gives me a matrix output, and I want it as a vector. Wrapping the output in as.vector() makes every value individual, losing the purpose of running combn() in the first place. Imagine my dataset was c("a", "b", "c"), how can I use combn() (or some other function), to get a vector where my output would be:
my_data <- c("a", "b", "c")
#natural output with combn()
#output with combn()
combn(my_data, 2, simplify = TRUE)
#output with as.vector() wrapped
as.vector(combn(my_data, 2, simplify = TRUE))
#desired output
answer <- c("ab", "ac", "bc") #I don't care about order
You can paste
each column of the result together using apply
my_data <- c("a", "b", "c")
apply(combn(my_data, 2), 2, paste, collapse = "")
#> [1] "ab" "ac" "bc"
Created on 2023-01-25 with reprex v2.0.2