首页 > 解决方案 > 使用列表列时,data.table 在 `i` 中有 `!` 的意外行为

问题描述

请参见以下示例:

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 包(v0.3.0)于 2019-07-11 创建

这是一个错误还是在data.table!中具有特殊作用?i

如何操作 data.table 中的 data.frame 相关

标签: rdata.table

解决方案


推荐阅读