Skip to content

Loading…

Make `[[.indedex_array faster` #27

Closed
hadley opened this Issue · 0 comments

1 participant

@hadley
Owner

Compare splitter_a to split:

df <- data.frame(x = sample(100, 1e3, rep = T))
system.time(x <- as.list(splitter_a(df, 1)))
system.time(y <- split(df, 1:nrow(df)))
all.equal(x, y)

Should be able to speed up by specialising for common cases

"[[.indexed_array" <- function(x, i) {

  if (ncol(x$index) == 2) {
    x1 <- x$index[[1]]
    x2 <- x$index[[2]]
    if (x$subs[1] == "[") {
      return(x$env$data[x1[i], x1[2], drop = x$drop])
    }
    if (x$subs[2] == "[[") {
      return(x$env$data[[x1[i], x1[2], drop = x$drop]])
    }
  }

  indices <- x$index[i, ,drop=TRUE]
  call <- paste("x$env$data", x$subs[1], paste(indices, collapse = ","), ",
    drop = ", x$drop, x$subs[2], sep = "")
  eval(parse(text = call))
}
@hadley hadley added a commit that closed this issue
@hadley Faster array indexing.
Closes #27
b34e78b
@hadley hadley closed this in b34e78b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.