Skip to content
Prev 17334 / 398513 Next

Pointer to covariates?

On Thu, 21 Feb 2002, Anne York wrote:

            
This is very good! I made this function of it:

cro.ay.orig <- function(dat){
  covar <- unique(dat[, -1])
  dat.keys <-
    match(paste(dat$x1, dat$x2, sep = ""),
          paste(covar$x1, covar$x2, sep = ""))

  return(y = dat[, 1],
         covar = covar,
         keys = dat.keys)
}

and this is fast; with 'dat' containing 100000 observations, I get:
[1] 1.00 0.02 1.08 0.00 0.00
However, this function needs to be generalized, so I wrote:
cro.ay <- function(dat, response = 1){
  covar <- unique(dat[, -response, drop = FALSE])
  dat.keys <-
    match(apply(dat[, -response, drop = FALSE], 1, paste, collapse = ""),
          apply(covar, 1, paste, collapse = ""))
  return(y = dat[, response],
         covar = covar,
         keys = dat.keys)
}

but this was much slower (but acceptable) on the same data:

[1] 11.63  0.32 12.34  0.00  0.00

It is apparently the pasting row by row of the data frame,

 apply(covar, 1, paste, collapse = "")

that takes the time. Is there a better way of doing this?

Thanks for all the help!

G?ran

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._