Thanks David for the thoughts. The challenge I have with this approach is that the criteria I have is defined by a series of tests--which I do not think I could substitute in in place of the logical indexing.
In the combinations code I was hoping there is a step where, each new combination is added to the current list of combinations. If this were the case, I could put my series of tests in the code right there and then store the combination if appropriate. However, evalutating the code--which uses recursion--I am not sure if this approach will work. The combinations code is listed below. Is there a simple place(s) where I could insert my tests, operating on the current combination? function (n, r, v = 1:n, set = TRUE, repeats.allowed = FALSE) { if (mode(n) != "numeric" || length(n) != 1 || n < 1 || (n%%1) != 0) stop("bad value of n") if (mode(r) != "numeric" || length(r) != 1 || r < 1 || (r%%1) != 0) stop("bad value of r") if (!is.atomic(v) || length(v) < n) stop("v is either non-atomic or too short") if ((r > n) & repeats.allowed == FALSE) stop("r > n and repeats.allowed=FALSE") if (set) { v <- unique(sort(v)) if (length(v) < n) stop("too few different elements") } v0 <- vector(mode(v), 0) if (repeats.allowed) sub <- function(n, r, v) { if (r == 0) v0 else if (r == 1) matrix(v, n, 1) else if (n == 1) matrix(v, 1, r) else rbind(cbind(v[1], Recall(n, r - 1, v)), Recall(n - 1, r, v[-1])) } else sub <- function(n, r, v) { if (r == 0) v0 else if (r == 1) matrix(v, n, 1) else if (r == n) matrix(v, 1, n) else rbind(cbind(v[1], Recall(n - 1, r - 1, v[-1])), Recall(n - 1, r, v[-1])) } sub(n, r, v[1:n]) } <environment: namespace:gtools> ************************************************************************************************************************************************************** > I am working with the combinations function (available in the gtools > package). However, rather than store all of the possible combinations I > would like to check each combination to see if it meets a certain criteria. > If it does, I will then store it. > > I have looked at the combinations code but am unsure where in the algorithm I > would be able to operate on each combination. Logical indexing: > combinations(3,2,letters[1:3])[,1]=="a" [1] TRUE TRUE FALSE > combinations(3,2,letters[1:3])[ combinations(3,2,letters[1:3])[,1]=="a", ] [,1] [,2] [1,] "a" "b" [2,] "a" "c" --David [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.