Some potential inconsistencies on := list of vectors

Open
#3,256 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Reproduce the listed := examples in data.table, including the grouped cases, and start by examining the [.data.table behavior shown in the warnings. Compare the resulting assignments and warnings across all examples; done means the inconsistent cases have an agreed expected behavior and corresponding verification.

Written by the indexing model from the issue text.

Description

consistency non-atomic column

In the following examples, when there are inconsistent numbers of symbols/elements between LHS and RHS, values are sometimes silently assigned, or warnings are produced. It looks like there are some potential inconsistent behavior.

library(data.table)

dt <- data.table(id = 1:10, group = rep(1:2, each = 5), x = 1:10)

dt1 <- copy(dt)
dt1[, a := list(1, 2)]
dt1
#>     id group  x a
#>  1:  1     1  1 1
#>  2:  2     1  2 2
#>  3:  3     1  3 1
#>  4:  4     1  4 2
#>  5:  5     1  5 1
#>  6:  6     2  6 2
#>  7:  7     2  7 1
#>  8:  8     2  8 2
#>  9:  9     2  9 1
#> 10: 10     2 10 2

dt2 <- copy(dt)
dt2[, c("a", "b") := list(1, 2, 3)]
#> Warning in `[.data.table`(dt2, , `:=`(c("a", "b"), list(1, 2, 3))):
#> Supplied 2 columns to be assigned a list (length 3) of values (1 unused)
dt2
#>     id group  x a b
#>  1:  1     1  1 1 2
#>  2:  2     1  2 1 2
#>  3:  3     1  3 1 2
#>  4:  4     1  4 1 2
#>  5:  5     1  5 1 2
#>  6:  6     2  6 1 2
#>  7:  7     2  7 1 2
#>  8:  8     2  8 1 2
#>  9:  9     2  9 1 2
#> 10: 10     2 10 1 2

dt3 <- copy(dt)
dt3[, c("a") := list(1, 2)]
dt3
#>     id group  x a
#>  1:  1     1  1 1
#>  2:  2     1  2 2
#>  3:  3     1  3 1
#>  4:  4     1  4 2
#>  5:  5     1  5 1
#>  6:  6     2  6 2
#>  7:  7     2  7 1
#>  8:  8     2  8 2
#>  9:  9     2  9 1
#> 10: 10     2 10 2

dt4 <- copy(dt)
dt4[, c("a", "b") := list(cummax(x), cummin(x), cumsum(x))]
#> Warning in `[.data.table`(dt4, , `:=`(c("a", "b"), list(cummax(x),
#> cummin(x), : Supplied 2 columns to be assigned a list (length 3) of values
#> (1 unused)
dt4
#>     id group  x  a b
#>  1:  1     1  1  1 1
#>  2:  2     1  2  2 1
#>  3:  3     1  3  3 1
#>  4:  4     1  4  4 1
#>  5:  5     1  5  5 1
#>  6:  6     2  6  6 1
#>  7:  7     2  7  7 1
#>  8:  8     2  8  8 1
#>  9:  9     2  9  9 1
#> 10: 10     2 10 10 1

dt5 <- copy(dt)
dt5[, c("a") := list(cummax(x), cummin(x))]
dt5
#>     id group  x               a
#>  1:  1     1  1 1,2,3,4,5,6,...
#>  2:  2     1  2 1,1,1,1,1,1,...
#>  3:  3     1  3 1,2,3,4,5,6,...
#>  4:  4     1  4 1,1,1,1,1,1,...
#>  5:  5     1  5 1,2,3,4,5,6,...
#>  6:  6     2  6 1,1,1,1,1,1,...
#>  7:  7     2  7 1,2,3,4,5,6,...
#>  8:  8     2  8 1,1,1,1,1,1,...
#>  9:  9     2  9 1,2,3,4,5,6,...
#> 10: 10     2 10 1,1,1,1,1,1,...

dt6 <- copy(dt)
dt6[, c("a") := list(1, 2), by = group]
dt6
#>     id group  x a
#>  1:  1     1  1 1
#>  2:  2     1  2 1
#>  3:  3     1  3 1
#>  4:  4     1  4 1
#>  5:  5     1  5 1
#>  6:  6     2  6 1
#>  7:  7     2  7 1
#>  8:  8     2  8 1
#>  9:  9     2  9 1
#> 10: 10     2 10 1

dt7 <- copy(dt)
dt7[, c("a") := list(cummax(x), cummin(x)), by = group]
dt7
#>     id group  x  a
#>  1:  1     1  1  1
#>  2:  2     1  2  2
#>  3:  3     1  3  3
#>  4:  4     1  4  4
#>  5:  5     1  5  5
#>  6:  6     2  6  6
#>  7:  7     2  7  7
#>  8:  8     2  8  8
#>  9:  9     2  9  9
#> 10: 10     2 10 10

I'm using the latest dev (87e7fc3).

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.