[R] Working with combinations
David Winsemius
dwinsemius at comcast.net
Wed Mar 3 22:30:22 CET 2010
On Mar 3, 2010, at 12:35 PM, Herm Walsh wrote:
> 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)
#---new---
> else if (combo_true)
Recall(n - 1, r, v[-1])
#------------
> else rbind(cbind(v[1], Recall(n, r - 1, v)), Recall(n -
> 1, r, v[-1]))
# this would be the point at which one would decide whether to insert
a new combination-row.
# You could insert an else if clause above it triggered by your
combination criteria
> }
> 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]))
*Or here.... or both
> }
> 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
>
>
More information about the R-help
mailing list