Skip to content

Instantly share code, notes, and snippets.

@hadley
Created September 12, 2014 21:25
Show Gist options
  • Save hadley/78529c4965c87d542999 to your computer and use it in GitHub Desktop.
Save hadley/78529c4965c87d542999 to your computer and use it in GitHub Desktop.
`[.my_df` <- function (x, i, j) {
if (missing(i) && missing(j)) return(x)
# First, subset columns
if (!missing(j)) {
x <- .subset(x, j)
}
# Next, subset rows
if (!missing(i)) {
x <- lapply(x, `[`, i)
}
# TODO: handle 0 column case
structure(x,
class = "data.frame",
row.names = .set_row_names(length(x[[1]]))
)
}
class(mtcars) <- c("my_df", "data.frame")
mtcars[]
mtcars[,]
mtcars[1:5, ]
mtcars[, 1:5]
mtcars[, c("mpg", "disp", "hp")]
mtcars[mtcars$mpg < 20, ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment