从具有重复元素的向量生成所有唯一组合
Posted
技术标签:
【中文标题】从具有重复元素的向量生成所有唯一组合【英文标题】:Generate all unique combinations from a vector with repeating elements 【发布时间】:2019-08-05 10:25:26 【问题描述】:之前有人问过这个问题,但仅针对具有非重复元素的向量。我无法找到一个简单的解决方案来从具有重复元素的向量中获取所有组合。为了说明,我在下面列出了一个示例。
x <- c('red', 'blue', 'green', 'red', 'green', 'red')
向量 x 有 3 个重复元素代表“红色”,2 个重复元素代表“绿色”。所有独特组合的预期结果都是这样的。
# unique combinations with one element
'red'
'blue'
'green'
# unique combination with two elements
'red', 'blue' # same as 'blue','red'
'red', 'green'
'red', 'red'
'blue', 'green'
'green', 'green'
# unique combination with three elements
'red', 'blue', 'green'
'red', 'red', 'blue'
'red', 'red', 'green'
'red', 'red', 'red' # This is valid because there are three 'red's
'green', 'green', 'red'
'green', 'green', 'blue'
# more unique combinations with four, five, and six elements
【问题讨论】:
【参考方案1】:使用combn()
和lapply()
应该可以解决问题。
x <- c('red', 'blue', 'green', 'red', 'green', 'red')
lapply(1:3, function(y) combn(x, y))
# [[1]]
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] "red" "blue" "green" "red" "green" "red"
# [[2]]
# [,1] [,2] [,3] [,4] [,5] [,6] ...
# [1,] "red" "red" "red" "red" "red" "blue" ...
# [2,] "blue" "green" "red" "green" "red" "green" ...
# [[3]]
# [,1] [,2] [,3] [,4] [,5] [,6] ...
# [1,] "red" "red" "red" "red" "red" "red" ...
# [2,] "blue" "blue" "blue" "blue" "green" "green" ...
# [3,] "green" "red" "green" "red" "red" "green" ...
所有独特的组合
lapply(cc, function(y)
y[,!duplicated(apply(y, 2, paste, collapse="."))]
)
[[1]]
[1] "red" "blue" "green"
[[2]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "red" "red" "red" "blue" "blue" "green" "green"
[2,] "blue" "green" "red" "green" "red" "red" "green"
[[3]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] ...
[1,] "red" "red" "red" "red" "red" "red" "blue" ...
[2,] "blue" "blue" "green" "green" "red" "red" "green" ...
[3,] "green" "red" "red" "green" "green" "red" "red" ...
虽然严格来说这些并不是所有独特的组合,因为它们中的一些是彼此的排列。
适当的独特组合
lapply(cc, function(y)
y[,!duplicated(apply(y, 2, function(z) paste(sort(z), collapse=".")))]
)
# [[1]]
# [1] "red" "blue" "green"
# [[2]]
# [,1] [,2] [,3] [,4] [,5]
# [1,] "red" "red" "red" "blue" "green"
# [2,] "blue" "green" "red" "green" "green"
# [[3]]
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] "red" "red" "red" "red" "red" "blue"
# [2,] "blue" "blue" "green" "green" "red" "green"
# [3,] "green" "red" "red" "green" "red" "green"
【讨论】:
或lapply(1:3, function(k) matrix(x[combn(i, k)], nrow=k))
。 OP还想要1和2的组合。然后申请unique
。
@RuiBarradas:啊,我明白了。看来我毕竟没有做索引技巧。
此解决方案生成所有可能的组合,但不是所有唯一的组合。
@Jian:我现在明白了。【参考方案2】:
library(DescTools)
x <- c('red', 'blue', 'green', 'red', 'green', 'red')
allSets <- lapply(1:length(x), function(i)
unique(t(apply(CombSet(x,i,repl = F),1,sort)))
)
#[1]]
#[,1] [,2] [,3] [,4] [,5] [,6]
#[1,] "red" "blue" "green" "red" "green" "red"
##[[2]]
#[,1] [,2]
#[1,] "blue" "red"
#[2,] "green" "red"
#[3,] "red" "red"
#[4,] "blue" "green"
#[5,] "green" "green"
#[[3]]
#[,1] [,2] [,3]
#[1,] "blue" "green" "red"
#[2,] "blue" "red" "red"
#[3,] "green" "red" "red"
#[4,] "green" "green" "red"
#[5,] "red" "red" "red"
#[6,] "blue" "green" "green"
#[[4]]
#[,1] [,2] [,3] [,4]
#[1,] "blue" "green" "red" "red"
#[2,] "blue" "green" "green" "red"
#[3,] "blue" "red" "red" "red"
#[4,] "green" "green" "red" "red"
#[5,] "green" "red" "red" "red"
#[[5]]
#[,1] [,2] [,3] [,4] [,5]
#[1,] "blue" "green" "green" "red" "red"
#[2,] "blue" "green" "red" "red" "red"
#[3,] "green" "green" "red" "red" "red"
#[[6]]
#[,1] [,2] [,3] [,4] [,5] [,6]
#[1,] "blue" "green" "green" "red" "red" "red"
【讨论】:
OP 在问题的末尾说“有四个、五个和六个元素的更多独特组合”。我想他想要所有六个。我理解错了这个问题吗? 其实我觉得你是对的。我的错。没看到最后一句话。 :) 谢谢,除了一个组件 [[1]] 的情况外,此解决方案有效。 我可以将其保存为数据框还是将它们中的每一个连接到一列中?【参考方案3】:library(arrangements)
combinations(c("red", "blue", "green"), k = 2, freq = c(3, 1, 2))
# [,1] [,2]
# [1,] "red" "red"
# [2,] "red" "blue"
# [3,] "red" "green"
# [4,] "blue" "green"
# [5,] "green" "green"
combinations(c("red", "blue", "green"), k = 3, freq = c(3, 1, 2))
# [,1] [,2] [,3]
# [1,] "red" "red" "red"
# [2,] "red" "red" "blue"
# [3,] "red" "red" "green"
# [4,] "red" "blue" "green"
# [5,] "red" "green" "green"
# [6,] "blue" "green" "green"
如果您不想手动输入频率:
x <- c('red', 'blue', 'green', 'red', 'green', 'red')
tx <- table(x)
combinations(names(tx), k = 2, freq = tx)
# [,1] [,2]
# [1,] "blue" "green"
# [2,] "blue" "red"
# [3,] "green" "green"
# [4,] "green" "red"
# [5,] "red" "red"
或者使用RcppAlgos
:
library(RcppAlgos)
comboGeneral(names(tx), m=2, freqs = tx)
# [,1] [,2]
# [1,] "blue" "green"
# [2,] "blue" "red"
# [3,] "green" "green"
# [4,] "green" "red"
# [5,] "red" "red"
【讨论】:
谢谢,如果在 comboGeneral 中以 1:6 迭代 m ,这将有效。感谢您的回答。以上是关于从具有重复元素的向量生成所有唯一组合的主要内容,如果未能解决你的问题,请参考以下文章