Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some potential inconsistencies on := list of vectors #3256

Open
renkun-ken opened this issue Jan 4, 2019 · 0 comments
Open

Some potential inconsistencies on := list of vectors #3256

renkun-ken opened this issue Jan 4, 2019 · 0 comments
Labels
consistency non-atomic column e.g. list columns, S4 vector columns

Comments

@renkun-ken
Copy link
Member

renkun-ken commented Jan 4, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
consistency non-atomic column e.g. list columns, S4 vector columns
Projects
None yet
Development

No branches or pull requests

2 participants