Skip to content

Instantly share code, notes, and snippets.

@hadley
Created December 31, 2011 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadley/1545121 to your computer and use it in GitHub Desktop.
Save hadley/1545121 to your computer and use it in GitHub Desktop.
take.r
# Subsetting along (single) arbitrary dimension
take <- function(x, along, indices, drop = FALSE) {
nd <- length(dim(x))
index <- as.list(rep(TRUE, nd))
index[along] <- indices
eval(as.call(c(as.name("["), as.name("x"), index, drop = drop)))
}
x <- array(seq_len(3 * 4 * 5), c(3, 4, 5))
take(x, 3, 1)
take(x, 2, 1)
take(x, 1, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment