[R] Pointer to covariates?
Göran Broström
gb at stat.umu.se
Fri Feb 22 11:45:18 CET 2002
On Thu, 21 Feb 2002, Anne York wrote:
> Here is another idea, but the overhead might be just as great.
>
> dat_data.frame(y=1:3,x1=c(1,0,1),x2=c(0,1,0))
> dat.unique_unique(paste(as.character(dat$x1),as.character(dat$x2)))
> dat.keys_match(paste(as.character(dat$x1),as.character(dat$x2)),dat.unique)
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:
> unix.time(sor.ay.orig <- cro.ay.orig(dat[1:100000, c(1, 2, 5)))
[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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list