r - 使用列表列时,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
解决方案
推荐阅读
- excel - 打开另存为对话框并在取消时退出
- python - 如果在矩阵上运行 SVD,如何使用该 SVD 的结果转换新行?
- r - 在 r 中将名称默认控制台从“<”更改为 R<
- asp.net-core - 如何检查用户在第二个浏览器选项卡中是否经过身份验证?
- javascript - 有没有更好的方法在 JavaScript 中编写冗余过滤器语句?
- java - Hibernate OGM Mongodb 投影
- amazon-web-services - 如何使用 AWS CLI 列出所有角色的标签
- javascript - Flask SocketIo 应用程序在 Heroku 上部署时不工作
- python - 每次我刷新值时都会在先前值和当前值之间切换
- c++ - 在不关闭池的情况下提升线程池加入任务