R找到所有可能的唯一组合
R find all possible unique combinations
我正在尝试在 R 中找到所有可能的唯一组合。似乎已经问了很多类似的问题,但我找不到相同的问题。
我的问题是从向量 x 中找到 m 个元素的组合,但 m 可能大于 x。比如从letters[1:2]中挑选3个元素,希望可以return:
combn(letters[1:2],3)
[,1] [,2] [,3] [,4]
[1,] "a" "a" "a" "b"
[2,] "a" "a" "b" "b"
[3,] "a" "b" "b" "b"
而是组合函数 n < m 中的错误。类似的函数有gtools:permutations, expand.grid.
如果之前有人问过同样的问题,但我没有听清,再次表示歉意。谢谢
combn1=function(x,m){
n=ifelse(length(x)==1,ifelse(is.numeric(x),x,1),length(x))
if(n>=m) return(combn(x,m))
a=do.call(expand.grid, rep(list(x),m))
b=t(unique(t(apply(a,1,sort))))
`dimnames<-`(b,NULL)
}
combn1(letters[1],3)
[,1]
[1,] "a"
[2,] "a"
[3,] "a"
> combn1(letters[1:2],3)
[,1] [,2] [,3] [,4]
[1,] "a" "a" "a" "b"
[2,] "a" "a" "b" "b"
[3,] "a" "b" "b" "b"
> combn1(letters[1:3],3)
[,1]
[1,] "a"
[2,] "b"
[3,] "c"
有一些专门为此构建的软件包。基本前提是我们需要重复长度为 m
的组合,其中 m
可能大于输入向量。我们从经典开始 gtools
:
library(gtools)
combinations(2, 3, letters[1:2], repeats.allowed = TRUE)
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "b"
[3,] "a" "b" "b"
[4,] "b" "b" "b"
然后是 arrangements
替代 iterpc
(@Gregor 在上面的评论中链接的包):
library(arrangements)
arrangements::combinations(2, 3, letters[1:2], replace = TRUE)
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "b"
[3,] "a" "b" "b"
[4,] "b" "b" "b"
最后是 RcppAlgos
,这是我创作的:
library(RcppAlgos)
comboGeneral(letters[1:2], 3, TRUE)
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "b"
[3,] "a" "b" "b"
[4,] "b" "b" "b"
combn
是一个很棒的函数,作为 R
的基础包之一提供,但它的缺点之一是它不允许重复(这是这里所需要的) .我写了一个非常全面的概述来解决与这个问题完全相同的问题,可以在这里找到:A Walk Through a Slice of Combinatorics in R.
这可能有效,它列出了所有不重复的唯一组合
> v1 = letters[1:4]
> v1
[1] "a" "b" "c" "d"
> do.call("c",lapply(seq_along(v1),function(i) utils::combn(v1,i,FUN=list)))
[[1]]
[1] "a"
[[2]]
[1] "b"
[[3]]
[1] "c"
[[4]]
[1] "d"
[[5]]
[1] "a" "b"
[[6]]
[1] "a" "c"
[[7]]
[1] "a" "d"
[[8]]
[1] "b" "c"
[[9]]
[1] "b" "d"
[[10]]
[1] "c" "d"
[[11]]
[1] "a" "b" "c"
[[12]]
[1] "a" "b" "d"
[[13]]
[1] "a" "c" "d"
[[14]]
[1] "b" "c" "d"
[[15]]
[1] "a" "b" "c" "d"
我正在尝试在 R 中找到所有可能的唯一组合。似乎已经问了很多类似的问题,但我找不到相同的问题。
我的问题是从向量 x 中找到 m 个元素的组合,但 m 可能大于 x。比如从letters[1:2]中挑选3个元素,希望可以return:
combn(letters[1:2],3)
[,1] [,2] [,3] [,4]
[1,] "a" "a" "a" "b"
[2,] "a" "a" "b" "b"
[3,] "a" "b" "b" "b"
而是组合函数 n < m 中的错误。类似的函数有gtools:permutations, expand.grid.
如果之前有人问过同样的问题,但我没有听清,再次表示歉意。谢谢
combn1=function(x,m){
n=ifelse(length(x)==1,ifelse(is.numeric(x),x,1),length(x))
if(n>=m) return(combn(x,m))
a=do.call(expand.grid, rep(list(x),m))
b=t(unique(t(apply(a,1,sort))))
`dimnames<-`(b,NULL)
}
combn1(letters[1],3)
[,1]
[1,] "a"
[2,] "a"
[3,] "a"
> combn1(letters[1:2],3)
[,1] [,2] [,3] [,4]
[1,] "a" "a" "a" "b"
[2,] "a" "a" "b" "b"
[3,] "a" "b" "b" "b"
> combn1(letters[1:3],3)
[,1]
[1,] "a"
[2,] "b"
[3,] "c"
有一些专门为此构建的软件包。基本前提是我们需要重复长度为 m
的组合,其中 m
可能大于输入向量。我们从经典开始 gtools
:
library(gtools)
combinations(2, 3, letters[1:2], repeats.allowed = TRUE)
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "b"
[3,] "a" "b" "b"
[4,] "b" "b" "b"
然后是 arrangements
替代 iterpc
(@Gregor 在上面的评论中链接的包):
library(arrangements)
arrangements::combinations(2, 3, letters[1:2], replace = TRUE)
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "b"
[3,] "a" "b" "b"
[4,] "b" "b" "b"
最后是 RcppAlgos
,这是我创作的:
library(RcppAlgos)
comboGeneral(letters[1:2], 3, TRUE)
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "b"
[3,] "a" "b" "b"
[4,] "b" "b" "b"
combn
是一个很棒的函数,作为 R
的基础包之一提供,但它的缺点之一是它不允许重复(这是这里所需要的) .我写了一个非常全面的概述来解决与这个问题完全相同的问题,可以在这里找到:A Walk Through a Slice of Combinatorics in R.
这可能有效,它列出了所有不重复的唯一组合
> v1 = letters[1:4]
> v1
[1] "a" "b" "c" "d"
> do.call("c",lapply(seq_along(v1),function(i) utils::combn(v1,i,FUN=list)))
[[1]]
[1] "a"
[[2]]
[1] "b"
[[3]]
[1] "c"
[[4]]
[1] "d"
[[5]]
[1] "a" "b"
[[6]]
[1] "a" "c"
[[7]]
[1] "a" "d"
[[8]]
[1] "b" "c"
[[9]]
[1] "b" "d"
[[10]]
[1] "c" "d"
[[11]]
[1] "a" "b" "c"
[[12]]
[1] "a" "b" "d"
[[13]]
[1] "a" "c" "d"
[[14]]
[1] "b" "c" "d"
[[15]]
[1] "a" "b" "c" "d"