使用r中的贝尔数字获取所有可能的分区
我想找到所有可能在R中相似的方式可能相似的方式。组合总数被计算为贝尔的数字。例如,有15种组织4个类别的方法。
我正在尝试编写一些R代码,以将这15种方式显示为二进制向量。
第一个代码的第一个块显示有四个类,每个类都可以将A,B或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"
将每个类互相比较,我们可以得到该系统的结构表示:
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
您可以看到B和A相同,C和A是相同的,因此通过扣除C和B是相同的,并且所有其他比较都不同。
我们知道这是四个类别的15种解决方案之一 - 我想以这种二进制格式知道其他14个解决方案。
我首先尝试使用Expand.Grid,但这并不是类别的条件结构(仅显示每个单元格的所有可能性,即1或0)
# not correct
poss = rep(list(0:1), length(comparisons))
possibilities = expand.grid(poss)
我觉得有一个简单的解决方案,但我目前对此不了解。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用R软件包
分区
获取一组类别的所有可能分区。由
You can use the R package
partitions
to get all possible partitions of a set of categories.Created on 2022-05-16 by the reprex package (v2.0.0)