Skip to content

Commit 4b60d10

Browse files
authored
:=(...) with shift() - gforce follow-up #5245, #5348 (#5404)
* follow-up #5245, #5348, added test from #5403
1 parent eda3b09 commit 4b60d10

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269

270270
36. `unique.data.table()` gains `cols` to specify a subset of columns to include in the resulting `data.table`, [#5243](https://github.com/Rdatatable/data.table/issues/5243). This saves the memory overhead of subsetting unneeded columns, and provides a cleaner API for a common operation previously needing more convoluted code. Thanks to @MichaelChirico for the suggestion & implementation.
271271

272-
37. `:=` is now optimized by group, [#1414](https://github.com/Rdatatable/data.table/issues/1414). Thanks to Arun Srinivasan for suggesting, and Benjamin Schwendinger for the PR. Thanks to @clerousset, @dcaseykc, @OfekShilon, and @SeanShao98 for testing dev and filing detailed bug reports which were fixed before release and their tests added to the test suite.
272+
37. `:=` is now optimized by group, [#1414](https://github.com/Rdatatable/data.table/issues/1414). Thanks to Arun Srinivasan for suggesting, and Benjamin Schwendinger for the PR. Thanks to @clerousset, @dcaseykc, @OfekShilon, @SeanShao98, and @ben519 for testing dev and filing detailed bug reports which were fixed before release and their tests added to the test suite.
273273

274274
38. `.I` is now available in `by` for rowwise operations, [#1732](https://github.com/Rdatatable/data.table/issues/1732). Thanks to Rafael H. M. Pereira for requesting, and Benjamin Schwendinger for the PR.
275275

R/data.table.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,9 @@ replace_dot_alias = function(e) {
19121912
if (length(o__)) jrows = o__[jrows]
19131913
if (length(irows)) jrows = irows[jrows]
19141914
if (length(jvals)==1L) jvals = jvals[[1L]] # unwrap single column jvals for assign
1915+
if (.is_nrows(jsub)) { # 5403 unwrap multicolumn jvals for gfunctions that can return lists
1916+
jvals = if (length(jvals) != length(lhs)) split(unlist(jvals), rep(seq_along(jvals[[1L]]), length(jvals))) else lapply(jvals, unlist)
1917+
}
19151918
.Call(Cassign, x, jrows, lhs, newnames, jvals)
19161919
}
19171920
if (any(names_x[cols] %chin% key(x)))

inst/tests/tests.Rraw

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ test(351.2, DT[, c("a","b"):=13:15, verbose=TRUE], ans,
10901090
notOutput="revised")
10911091
test(352.1, DT[,letters[1:4]:=list(1L,NULL)], error="Supplied 4 columns to be assigned 2 items. Please see NEWS for v1.12.2")
10921092
test(352.2, DT[,letters[1:4]:=list(1L,NULL,2L,NULL)], data.table(a=c(1L,1L,1L),c=c(2L,2L,2L)))
1093+
test(352.3, DT[,letters[1:2]:=list(1L,NULL,2L,NULL)], error="Supplied 2 columns to be assigned 4 items. Please see NEWS for v1.12.2")
10931094

10941095
# Test assigning new levels into factor columns
10951096
DT = data.table(f=factor(c("a","b")),x=1:4)
@@ -18071,6 +18072,14 @@ for (opt in c(0,Inf)) {
1807118072
testnum = 2233.44
1807218073
}
1807318074
options(old)
18075+
# optimized := with gforce functions that can return lists #5403
18076+
old = options(datatable.verbose=TRUE)
18077+
DT = data.table(grp=1:2, x=1:4)
18078+
out = "Making each group and running j (GForce TRUE)"
18079+
test(2233.45, copy(DT)[, c("y", "z") := .(shift(x, type="lag", n=1), shift(x, type="lead", n=1)), by=grp], data.table(grp=1:2, x=1:4, y=c(NA, NA, 1:2), z=c(3:4, NA, NA)), output=out)
18080+
test(2233.46, copy(DT)[, l := shift(x, n=c(0, 0)), by=grp], data.table(grp=1:2, x=1:4, l=list(INT(1, 1), INT(2, 2), INT(3, 3), INT(4, 4))), output=out)
18081+
test(2233.47, copy(DT)[, c("l1", "l2") := shift(x, n=c(-1, 1)), by=grp], data.table(grp=1:2, x=1:4, l1=c(3:4,NA,NA), l2=c(NA,NA,1:2)), output=out)
18082+
options(old)
1807418083

1807518084
# support by=.I; #1732
1807618085
DT = data.table(V1=1:5, V2=3:7, V3=5:1)

0 commit comments

Comments
 (0)