[R] programming question

Adrian Dusa dusa.adrian at gmail.com
Mon Sep 17 14:16:32 CEST 2007


On Monday 17 September 2007, you wrote:
> R 2.6.0 has Reduce;
>
> myvec <- c(2, 8, 24, 26, 51, 57, 58, 78, 219)
> Reduce(function(myvec, p) setdiff(myvec, findSubsets2(p)), myvec, myvec)

Thanks Gabor, at first I jumped off my chair but... for many input variables 
it takes <ages> to reduce the vector. My crude solution is way faster than 
Reduce()
For 9 input variables:

`findSubsets2` <- 
function(element) {
    require(QCA)
    base3row <- getRow(rep(3,9), element, zerobased=TRUE)
    increment <- function(x, y) {
        a <- x
        for (i in 1:2) {
            a <- as.vector(outer(y, a, "+"))
            x <- c(x, a)
            }
        return(x)
        }
    indices <- which(base3row == 0)
    mbase <- c(6561, 2187, 729, 243, 81, 27, 9, 3, 1)
    for (i in indices) {
        element <- increment(element, mbase[i])
        }
    return(element[-1])
    }


`myreduce` <- function(myvec) {
    position <- 1
    while(position < length(myvec)) {
        falsevector <- findSubsets2(myvec[position])
        myvec <- setdiff(myvec, falsevector)
        position <- position + 1
        }
    return(myvec
    }


The timings for the tests:
set.seed(1)
myvec1 <- myvec2 <- sort(sample(3^9, 3^8)) - 1

> system.time(myvec1 <- myreduce(myvec1))
   user  system elapsed
  0.200   0.004   0.204

> system.time(myvec2 <- Reduce(function(myvec2, p) setdiff(myvec2, 
findSubsets2(p)), myvec2, myvec2))
   user  system elapsed
 12.093   0.000  12.095

With 14 input variables my function takes 24 seconds to complete, and I'd like 
to process 20 such input variables (their complexity grow exponentially)...

Thanks again,
Adrian



-- 
Adrian Dusa
Romanian Social Data Archive
1, Schitu Magureanu Bd
050025 Bucharest sector 5
Romania
Tel./Fax: +40 21 3126618 \
          +40 21 3120210 / int.101



More information about the R-help mailing list