我们说我有6个数字:
a <- c(1,2,3,4,5,6)
我想列出这6个数字的所有可能的3位数组合,包括重复。
期望的结果如下所示:
1 1 1
1 2 3
1 2 4
...
我不想包含具有相同3个数字但顺序不同的元素:
例如
1 2 3
3 2 1
应该排除其中一个
答案 0 :(得分:5)
来自combinations
的{{1}}功能可以执行此操作:
gtools
library(gtools) combinations(n=6, r=3, v=a, repeats.allowed=TRUE)
答案 1 :(得分:2)
SELECT page
FROM Page page
INNER JOIN page.permissions permission
INNER JOIN permission.user user WITH IDENTITY(user) = :userId
返回组合的data.frame,从您提供的每个集合中选择一个。如果expand.grid
与1 2 3
不同,请对其进行子集化以获得所需的行。
3 2 1
答案 2 :(得分:2)
下面是一个允许您在输出上指定约束的通用函数。例如,我有很多情况需要,因为我需要给定集合的所有n元组,使得产品小于给定的界限。在我编写此函数之前,我被迫使用combinations
并搜索符合我条件的那些行。这花费了大量的时间和大量的记忆。
Combo <- function(n,r,v=1:n,li=10^8,fun1="prod",fun2="<",repeats.allowed=FALSE) {
## where fun1 is a general function such as "prod", "sum", "sd", etc.
## and fun2 is a comparison operator such as "<", "<=", ">", "==", etc.
myfun <- match.fun(FUN = fun1)
operator1 <- match.fun(FUN = fun2)
operator2 <- match.fun(FUN = fun2)
myv <- sort(v)
if (fun2 %in% c(">",">=")) {
myv <- rev(myv)
TheLim <- min(v)
} else {
TheLim <- max(v)
if (fun2 == "==") {
operator1 <- match.fun(FUN = "<=")
}
}
if (!repeats.allowed) {
m <- matrix(numeric(0),combinat::nCm(n,r),r)
v1 <- myv; n1 <- length(v); t <- TRUE; count <- 0L
while (t) {
t <- operator1(myfun(v1[1:r]),li)
while (t && length(v1)>=r) {
t_1 <- operator2(myfun(v1[1:r]),li)
if (t_1) {count <- count+1L; m[count,] <- v1[1:r]}
v1 <- v1[-r]
t <- operator1(myfun(v1[1:r],na.rm=TRUE),li)
}
if (t) {
s <- 1:length(v1)
mymax <- myv[n1-(r-s)]
t1 <- which(!v1==mymax)
if (length(t1)>0) {
e <- max(t1)
v1[e] <- myv[which(myv==v1[e])+1L]
v1 <- c(v1[1:e],myv[(which(myv==v1[e])+1L):n1])
} else {
return(m[!is.na(m[,1]),])
}
} else {
r1 <- r-1L
while (r1>=1L && !t) {
v1[r1] <- myv[which(myv==v1[r1])+1L]
if (v1[r1]==TheLim) {r1 <- r1-1L; next}
v1 <- c(v1[1:r1],myv[(which(myv==v1[r1])+1L):n1])
t <- operator1(myfun(v1[1:r],na.rm=TRUE),li) && length(v1)>=r
r1 <- r1-1L
}
if (!t) {return(m[!is.na(m[,1]),])}
}
}
} else {
MySet <- 1:n
for (i in 1:(r-1L)) {MySet <- sapply(1:n, function(x) sum(MySet[1:x]))}
m <- matrix(numeric(0),nrow=MySet[n],ncol=r)
v1 <- c(rep(myv[1], r),myv[2:n]); n1 <- length(v); t <- TRUE; count <- 0L
while (t) {
t <- operator1(myfun(v1[1:r]),li)
while (t && length(v1)>=r) {
t_1 <- operator2(myfun(v1[1:r]),li)
if (t_1) {count <- count+1L; m[count,] <- v1[1:r]}
v1 <- v1[-r]
t <- operator1(myfun(v1[1:r],na.rm=TRUE),li)
}
if (t) {
s <- 1:length(v1)
t1 <- which(!v1==TheLim)
if (length(t1)>0) {
e <- max(t1)
v1[e] <- myv[which(myv==v1[e])+1L]
tSize <- r - length(myv[1:e])
if (!v1[e]==TheLim) {
v1 <- c(v1[1:e],rep(v1[e],tSize),myv[(which(myv==v1[e])+1L):n1])
} else {
v1 <- c(v1[1:e],rep(v1[e],tSize))
}
} else {
return(m[!is.na(m[,1]),])
}
} else {
r1 <- r-1L
while (r1>=1L && !t) {
if (v1[r1]==TheLim) {r1 <- r1-1L; next}
v1[r1] <- myv[which(myv==v1[r1])+1L]
tSize <- r - length(myv[1:r1])
v1 <- c(v1[1:r1],rep(v1[r1],tSize),myv[(which(myv==v1[r1])+1L):n1])
t <- operator1(myfun(v1[1:r],na.rm=TRUE),li) && length(v1)>=r
r1 <- r1-1L
}
if (!t) {return(m[!is.na(m[,1]),])}
}
}
}
}
以下是一些例子:
## return all 3-tuple combinations of 1 through 6 such
## that the PRODUCT is less than 10
> Combo(n=6, r=3, v=1:6, li=10, fun1="prod", fun2="<", repeats.allowed=TRUE)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 2
. . .
[10,] 1 3 3
[11,] 2 2 2
## return all 3-tuple combinations of 1 through 6 such
## that the SUM is less than 10
> Combo(n=6, r=3, v=1:6, li=10, fun1="sum", fun2="<", repeats.allowed=TRUE)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 2
[3,] 1 1 3
. . .
[20,] 2 3 3
[21,] 2 3 4
[22,] 3 3 3
以下是一些涉及素数的很酷的例子:
> library(numbers)
> myps <- Primes(1000)
> system.time(t1 <- Combo(n=length(myps), r=3, v=myps, li=10^5, fun1="prod", fun2="<", repeats.allowed=TRUE))
user system elapsed
0.18 0.00 0.18
> nrow(t1)
[1] 13465
> set.seed(42)
> t1[sample(nrow(t1),5),]
[,1] [,2] [,3]
[1,] 13 31 197
[2,] 17 19 167
[3,] 2 131 227
[4,] 11 11 751
[5,] 5 31 151
> object.size(t1)
323360 bytes
> system.time(t2 <- combinations(n=length(myps), r=3, v=myps, repeats.allowed=TRUE))
user system elapsed
3.63 0.00 3.68
> nrow(t2)
[1] 804440
> system.time(t3 <- t2[which(sapply(1:nrow(t2), function(x) prod(t2[x,]) < 10^5)),])
user system elapsed
1.55 0.00 1.54
> nrow(t3)
[1] 13465
> object.size(t2)
19306760 bytes
正如您所看到的,Combo
功能更快,并且一步完成,而combinations/sapply
二重奏很慢(超过5秒)和两个笨重的步骤。 Combo
函数还返回一个几乎小60倍的对象。
这是另一个很酷的例子。假设您要查找前168个素数的所有3元组(即素数<1000),使得标准差小于50.没问题(使用与上面相同的设置):
> system.time(t1 <- Combo(n=length(myps), r=3, v=myps, li=50, fun1="sd", fun2="<", repeats.allowed=TRUE))
user system elapsed
1.49 0.00 1.48
> system.time(t3 <- t2[which(sapply(1:nrow(t2), function(x) sd(t2[x,]) < 50)),])
user system elapsed
19.89 0.00 19.89
> nrow(t1)
[1] 22906
> nrow(t3)
[1] 22906
> all(t3==t1)
[1] TRUE
应该注意,所有功能组合都不起作用。例如,如果您允许fun1="sd"
和fun2=">"
,则上述代码将返回0个匹配项。干杯!
答案 3 :(得分:1)
一种简单的方法是使用三个for循环。这会导致你想要的结果吗?
for (x in 1:6) {
for (y in x:6) {
for (z in y:6) {
print(paste(x,y,z))
}
}
}