I want to find all the possible ways a number of categories can be similar to each other in R. The total number of combinations is calculated as Bell's number. E.g. there are 15 ways to organise 4 categories.
I am trying to write some R code that shows those 15 ways as binary vectors.
This first block of code shows there are four classes, each of which can take a the value of a, b, or c.
set.seed(123)
n_classes = 4
classes = LETTERS[1:n_classes]
state = sample(letters[1:3], size = n_classes, replace = TRUE)
# "c" "b" "b" "b"
Comparing each class to each other we can get a structural representation of this system:
comparisons = outer(state, state, "==") %>%
.[lower.tri(.)]
names(comparisons) = outer(classes, classes, paste0) %>%
.[lower.tri(.)]
# > comparisons
# BA CA DA CB DB DC
# TRUE TRUE FALSE TRUE FALSE FALSE
You can see B and A are the same, and C and A are the same, so by deduction C and B are the same and all other comparisons are different.
We know this is one of 15 solutions for four categories - I want to know the other 14 in this binary format.
I first tried using expand.grid, but this doesnt account for the conditional structure of the categories (it just shows all the possibilities for each cell being 1 or 0)
# not correct
poss = rep(list(0:1), length(comparisons))
possibilities = expand.grid(poss)
I feel like there is a simple solution, but I am currently blind to it.
Thanks in advance.