Skip to content

Commit 87b375f

Browse files
committed
corrected
1 parent ea5458b commit 87b375f

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
108108
18. `fwrite` now allows `dec` to be the same as `sep` for edge cases where only one will be written, e.g. 0-row or 1-column tables. [#7227](https://github.com/Rdatatable/data.table/issues/7227). Thanks @MichaelChirico for the report and @venom1204 for the fix.
109109
110-
22. Using `by=` or `keyby=` with a simple numeric or character vector in `j` (e.g. `DT[, 1:2, by=grp]`) used to silently ignore the grouping argument. This now issues a warning to alert the user that grouping is not applied in this syntax and guides them to use the `.SD` idiom instead. [#5397](https://github.com/Rdatatable/data.table/issues/5397). Thanks to @mcol for the report and @venom1204 for the fix.
110+
19. Using `by=` or `keyby=` with a simple numeric or character vector in `j` (e.g. `DT[, 1:2, by=grp]`) used to silently ignore the grouping argument. This now issues a warning to alert the user that grouping is not applied in this syntax and guides them to use the `.SD` idiom instead. [#5397](https://github.com/Rdatatable/data.table/issues/5397). Thanks to @mcol for the report and @venom1204 for the fix.
111111
112112
```r
113113
DT = data.table(a=1:4, grp=c(1,1,2,2))

R/data.table.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,11 @@ replace_dot_alias = function(e) {
739739
if (!length(j) && !notj) return( null.data.table() )
740740
if (is.factor(j)) j = as.character(j) # fix for FR: #358
741741
if (is.character(j)) {
742-
if (!missingby && (missing(with) || isTRUE(with))) {
743-
warning(
744-
"`by` or `keyby` is ignored when `j` is a character vector used for column selection. ",
745-
"Perhaps you intended to use `.SD`? For example: DT[, .SD[, ", deparse(jsub), "], by = ...]"
746-
)
742+
if (!missingby) {
743+
warning(
744+
"`by` or `keyby` is ignored when `j` is a character vector used for column selection. ",
745+
"Perhaps you intended to use `.SD`? For example: DT[, .SD[, ", deparse(jsub), "], by = ...]"
746+
)
747747
}
748748
if (notj) {
749749
if (anyNA(idx <- chmatch(j, names_x)))
@@ -768,9 +768,11 @@ replace_dot_alias = function(e) {
768768
# else the NA in ansvals are for join inherited scope (test 1973), and NA could be in irows from join and data in i should be returned (test 1977)
769769
# in both cases leave to the R-level subsetting of i and x together further below
770770
} else if (is.numeric(j)) {
771-
if (!missingby && (missing(with) || isTRUE(with))) {
772-
warning(
773-
"`by` or `keyby` is ignored when `j` is a numeric vector used for column selection. ", "Perhaps you intended to use `.SD`? For example: DT[, .SD[, ", deparse(jsub), "], by = ...]")
771+
if (!missingby) {
772+
warning(
773+
"`by` or `keyby` is ignored when `j` is a numeric vector used for column selection. ",
774+
"Perhaps you intended to use `.SD`? For example: DT[, .SD[, ", deparse(jsub), "], by = ...]"
775+
)
774776
}
775777
j = as.integer(j)
776778
if (any(w <- (j>ncol(x)))) stopf("Item %d of j is %d which is outside the column number range [1,ncol=%d]", idx <- which.first(w), j[idx], ncol(x))

inst/tests/tests.Rraw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21624,7 +21624,7 @@ local({
2162421624
DT = data.table(a=1:4, b=5:8, g=c(1,1,2,2))
2162521625
test(2339.1, DT[, 1:2, by=g], DT[, 1:2], warning="`by` or `keyby` is ignored")
2162621626
test(2339.2, DT[, 2:1, keyby=g], DT[, 2:1], warning="`by` or `keyby` is ignored")
21627-
test(2339.3, DT[, c("b", "a"), by=g, with=FALSE], DT[, c("b", "a")])
21627+
test(2339.3, DT[, c("b", "a"), by=g, with=FALSE], DT[, c("b", "a")], warning="`by` or `keyby` is ignored")
2162821628
expected_sd = data.table(g=c(1,1,2,2), a=1:4, b=5:8)
2162921629
test(2339.4, DT[, .SD[, 1:2], by=g], expected_sd)
2163021630
expected_single_int = data.table(g=c(1,2), V1=c(1,1))

man/data.table.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ data.table(\dots, keep.rownames=FALSE, check.names=FALSE, key=NULL, stringsAsFac
9797
9898
See \href{../doc/datatable-intro.html}{\code{vignette("datatable-intro")}} and \code{example(data.table)}.}
9999
100-
\item{by}{ Column names are seen as if they are variables (as in \code{j} when \code{with=TRUE}). \emph{Note that `by` and `keyby` are ignored when `j` is a character or numeric vector used for selecting columns (i.e., when the internal `with=FALSE` is triggered).} The \code{data.table} is then grouped by the \code{by} and \code{j} is evaluated within each group. The order of the rows within each group is preserved, as is the order of the groups. \code{by} accepts:
100+
\item{by}{ Column names are seen as if they are variables (as in \code{j} when \code{with=TRUE}). \emph{Note that \code{by} and \code{keyby} are ignored when \code{j} is a character or numeric vector used for selecting columns (i.e., when the internal \code{with=FALSE} is triggered).} The \code{data.table} is then grouped by the \code{by} and \code{j} is evaluated within each group. ...}
101101
102102
\itemize{
103103
\item A single unquoted column name: e.g., \code{DT[, .(sa=sum(a)), by=x]}

0 commit comments

Comments
 (0)