Below, is a general function that allows you to specify constraints on the output. For example, I have had many situations to arise whereby I needed all n-tuples of a given set such that there product was less than a given bound. Before I wrote this function, I was forced to use combinations
and search for those rows that met my condition. This took a lot of time and a lot of memory.
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]),])}
}
}
}
}
Below are some examples:
## 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
Here are some cool example involving prime numbers:
> 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
As you can see, the Combo
function is much faster and performed in one step, whereas the combinations/sapply
duo is slow (more than 5 seconds) and two clunky steps. The Combo
function also returns an object that is almost 60 times smaller.
Here is another cool example. Let's say you want to find all 3-tuples of the first 168 primes (i.e. primes < 1000) such that the standard deviation is less than 50. No problem (with the same setup as above):
> 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
It should be noted that all function combinations don't work. For example if you let fun1="sd"
and fun2=">"
, the above code will return 0 matches. Cheers!