diff --git a/R/data.table.R b/R/data.table.R index 87cd5f9b9..383a3e567 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -546,7 +546,13 @@ chmatch2 <- function(x, table, nomatch=NA_integer_) { if (locked.N) lockBinding(".N", parent.frame()) } if (remove.N) rm(list=".N", envir=parent.frame()) - if (is.matrix(i)) stop("i is invalid type (matrix). Perhaps in future a 2 column matrix could return a list of elements of DT (in the spirit of A[B] in FAQ 2.14). Please let datatable-help know if you'd like this, or add your comments to FR #1611.") + if (is.matrix(i)) { + if (is.numeric(i) && ncol(i)==1L) { # #826 - subset DT on single integer vector stored as matrix + i = as.integer(i) + } else { + stop("i is invalid type (matrix). Perhaps in future a 2 column matrix could return a list of elements of DT (in the spirit of A[B] in FAQ 2.14). Please let datatable-help know if you'd like this, or add your comments to FR #657.") + } + } if (is.logical(i)) { if (notjoin) { notjoin = FALSE diff --git a/README.md b/README.md index 03059bbbd..aa415b84a 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,9 @@ 65. `print.data.table` now accepts a `quote` argument defaulting to `FALSE` (_a la_ `print.data.frame` in `base`). This option surrounds all printed elements with quotes and, e.g., makes whitespace more evident. Closes [#1177](https://github.com/Rdatatable/data.table/issues/1177); thanks to @MichaelChirico for the PR. + 66. `[.data.table` now accepts single column integer matrix in `i` argument the same way as data.frame. Closes [#826](https://github.com/Rdatatable/data.table/issues/826). Thanks to @jangorecki. + + #### NOTES 1. Clearer explanation of what `duplicated()` does (borrowed from base). Thanks to @matthieugomez for pointing out. Closes [#872](https://github.com/Rdatatable/data.table/issues/872). diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 8b576e7b7..e19359e95 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -6878,6 +6878,17 @@ ans2 <- c(" s1 s2","1: A A","2: B B", test(1553.1, capture.output(print(DT1, quote = TRUE)), ans1) test(1553.2, capture.output(print(DT1)), ans2) +# #826 - subset DT on single integer vector stored as matrix the same way as data.frame +dt <- data.table(a=letters[1:10]) +idx <- c(2:4,7L,9:10) +dim(idx) <- c(6L, 1L) +dimnames(idx) <- list(NULL, "Resample1") # as in caret::createDataPartition +test(1554.1, dt[idx], data.table(a=letters[idx])) +test(1554.2, dt[-idx], data.table(a=letters[(1:10)[-idx]])) +test(1554.3, dt[!idx], data.table(a=letters[(1:10)[-idx]])) +test(1554.4, idx, structure(c(2L, 3L, 4L, 7L, 9L, 10L), .Dim = c(6L, 1L), .Dimnames = list(NULL, "Resample1"))) + + ########################## diff --git a/man/data.table.Rd b/man/data.table.Rd index 42b45aef0..975372776 100644 --- a/man/data.table.Rd +++ b/man/data.table.Rd @@ -52,7 +52,7 @@ data.table(..., keep.rownames=FALSE, check.names=FALSE, key=NULL) \item{x}{ A \code{data.table}. } - \item{i}{ Integer, logical or character vector, expression of column names, \code{list} or \code{data.table}. + \item{i}{ Integer, logical or character vector, expression of column names, \code{list} or \code{data.table}, single column integer \code{matrix}. integer and logical vectors work the same way they do in \code{\link{[.data.frame}}. Other than \code{NA}s in logical \code{i} are treated as \code{FALSE} and a single \code{NA} logical is not recycled to match the number of rows, as it is in \code{[.data.frame}.