使用列表列时,data.table在i中的`!具有意外行为!

时间:2019-07-11 13:58:21

标签: r data.table

请参见以下示例:

library(data.table)
DT <- data.table(x = 1:3, y = list(character(), "bar", "baz"))
DT[]
#>    x   y
#> 1: 1    
#> 2: 2 bar
#> 3: 3 baz
!lengths(DT$y)
#> [1]  TRUE FALSE FALSE

# this is wrong:
DT[!lengths(y), ]
#>    x   y
#> 1: 2 bar
#> 2: 3 baz

# but this is right (adding brackets):
DT[(!lengths(y)), ]
#>    x y
#> 1: 1

DT2 <- copy(DT)
# this is wrong
DT[!lengths(y), y:= list(list("foo"))][]
#>    x   y
#> 1: 1    
#> 2: 2 foo
#> 3: 3 foo

# but this is right (adding brackets):
DT2[(!lengths(y)), y:= list(list("foo"))][]
#>    x   y
#> 1: 1 foo
#> 2: 2 bar
#> 3: 3 baz

reprex package(v0.3.0)于2019-07-11创建

这是错误还是! data.table 中的i中起特殊作用?

How to manipulate data.frame in the data.table

相关的

0 个答案:

没有答案