Skip to content

Commit 0047caf

Browse files
committed
handles integer vector stored as matrix in i, closes #826
1 parent 0cd7803 commit 0047caf

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

R/data.table.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,13 @@ chmatch2 <- function(x, table, nomatch=NA_integer_) {
546546
if (locked.N) lockBinding(".N", parent.frame())
547547
}
548548
if (remove.N) rm(list=".N", envir=parent.frame())
549-
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.")
549+
if (is.matrix(i)) {
550+
if (is.numeric(i) && ncol(i)==1L) { # #826 - subset DT on single integer vector stored as matrix
551+
i = as.integer(i)
552+
} else {
553+
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.")
554+
}
555+
}
550556
if (is.logical(i)) {
551557
if (notjoin) {
552558
notjoin = FALSE

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@
244244

245245
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.
246246

247+
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.
248+
249+
247250
#### NOTES
248251

249252
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).

inst/tests/tests.Rraw

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6878,6 +6878,17 @@ ans2 <- c(" s1 s2","1: A A","2: B B",
68786878
test(1553.1, capture.output(print(DT1, quote = TRUE)), ans1)
68796879
test(1553.2, capture.output(print(DT1)), ans2)
68806880

6881+
# #826 - subset DT on single integer vector stored as matrix the same way as data.frame
6882+
dt <- data.table(a=letters[1:10])
6883+
idx <- c(2:4,7L,9:10)
6884+
dim(idx) <- c(6L, 1L)
6885+
dimnames(idx) <- list(NULL, "Resample1") # as in caret::createDataPartition
6886+
test(1554.1, dt[idx], data.table(a=letters[idx]))
6887+
test(1554.2, dt[-idx], data.table(a=letters[(1:10)[-idx]]))
6888+
test(1554.3, dt[!idx], data.table(a=letters[(1:10)[-idx]]))
6889+
test(1554.4, idx, structure(c(2L, 3L, 4L, 7L, 9L, 10L), .Dim = c(6L, 1L), .Dimnames = list(NULL, "Resample1")))
6890+
6891+
68816892
##########################
68826893

68836894

man/data.table.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ data.table(..., keep.rownames=FALSE, check.names=FALSE, key=NULL)
5252
\item{x}{ A \code{data.table}.
5353

5454
}
55-
\item{i}{ Integer, logical or character vector, expression of column names, \code{list} or \code{data.table}.
55+
\item{i}{ Integer, logical or character vector, expression of column names, \code{list} or \code{data.table}, single column integer \code{matrix}.
5656

5757
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}.
5858

0 commit comments

Comments
 (0)