使用“时,R data.table j子句中需要括号;带“假”的;?
使用逻辑索引向量选择列时,会根据索引向量中的实际值发生不同的行为。对于带有列“a”和“b”的data.table x 返回Null data.table,而将(否定的)索引向量括在括号中使用“时,R data.table j子句中需要括号;带“假”的;?,r,data.table,R,Data.table,使用逻辑索引向量选择列时,会根据索引向量中的实际值发生不同的行为。对于带有列“a”和“b”的data.table x 返回Null data.table,而将(否定的)索引向量括在括号中 x[, (!c(FALSE, FALSE)), with = FALSE] 按预期返回列“a”和“b” 如果索引向量中至少有一个元素为true,则不需要括号。比如说 x[, !c(FALSE, TRUE), with = FALSE] 按预期返回列“a”。另请参见下面测试脚本的完整控制台输出 这是一个bug
x[, (!c(FALSE, FALSE)), with = FALSE]
按预期返回列“a”和“b”
如果索引向量中至少有一个元素为true,则不需要括号。比如说
x[, !c(FALSE, TRUE), with = FALSE]
按预期返回列“a”。另请参见下面测试脚本的完整控制台输出
这是一个bug还是预期的行为?如果是后者,有人能解释一下原因吗
require(data.table)
x <- data.table(a = c(1,2), b = c(10,20))
x
# a b
# 1: 1 10
# 2: 2 20
# doesn't work, returns Null data.table
x[, !c(FALSE, FALSE), with = FALSE]
# Null data.table (0 rows and 0 cols)
# works with paratheses, returns both columns
x[, (!c(FALSE, FALSE)), with = FALSE]
# a b
# 1: 1 10
# 2: 2 20
# works, returns first column
x[, !c(FALSE, TRUE), with = FALSE]
# a
# 1: 1
# 2: 2
# works, returns second column
x[, !c(TRUE, FALSE), with = FALSE]
# b
# 1: 10
# 2: 20
# works, returns both columns
x[, c(TRUE, TRUE), with = FALSE]
# a b
# 1: 1 10
# 2: 2 20
require(data.table)
x请使用sessionInfo()
指示您正在使用的data.table
的哪个版本。table_1.9.6 rj_2.0.4-2也可以在1.9.7中看到这一点。对我来说,更现实的例子是x[,!sappy(x,is.character),with=FALSE]
同意。我构造这个最小的示例是因为我最初认为它可能与j子句中的函数调用有关,但事实并非如此。@Frank,是否需要在执行条件的地方传递列x[,b=!sapply(x,is.character),with=FALSE]
。
require(data.table)
x <- data.table(a = c(1,2), b = c(10,20))
x
# a b
# 1: 1 10
# 2: 2 20
# doesn't work, returns Null data.table
x[, !c(FALSE, FALSE), with = FALSE]
# Null data.table (0 rows and 0 cols)
# works with paratheses, returns both columns
x[, (!c(FALSE, FALSE)), with = FALSE]
# a b
# 1: 1 10
# 2: 2 20
# works, returns first column
x[, !c(FALSE, TRUE), with = FALSE]
# a
# 1: 1
# 2: 2
# works, returns second column
x[, !c(TRUE, FALSE), with = FALSE]
# b
# 1: 10
# 2: 20
# works, returns both columns
x[, c(TRUE, TRUE), with = FALSE]
# a b
# 1: 1 10
# 2: 2 20