运算符 == 在 data.table 中的逻辑列中不一致 [英] Operator == inconsistent in logical columns in data.table

查看:11
本文介绍了运算符 == 在 data.table 中的逻辑列中不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下可重现的示例:

Please see the following reproducible example:

library(data.table)
set.seed(123)
DT <- data.table(A=rep(0.3,10000))
DT[, B := runif(.N) < A]
DT[B == T, .N]
# [1] 3005
DT[, summary(B)]
#    Mode   FALSE    TRUE    NA's
# logical    6995    3005       0

一切看起来都很好,两种方法的TRUE"值的计数相同.现在用一个新的替换col B.

Everything looks fine and the count of "TRUE" values is the same for the 2 methods. Now replace col B with a new one.

DT[, B := runif(.N) < A]
DT[B == T, .N]
# [1] 3331
DT[, summary(B)]
#    Mode   FALSE    TRUE    NA's
# logical    6981    3019       0 

B列中'T'的计数不同!!!它是同一列,但一种方法给出 3331 个TRUE"值,另一个给出 3019 个值.

The count of 'T' in the column B is different!!! It is the same column but one method gives 3331 "TRUE" values and the other 3019.

当 == 被绕过时

DT[B != F, .N]
# [1] 3019
DT[, summary(B)]
#    Mode   FALSE    TRUE    NA's
# logical    6981    3019       0 

这又是对的

我可以在 Windows 8.1 x64 上使用 data.table v1.94 和 1.9.5 重现它.

I can reproduce it with data.table v1.94 and 1.9.5 on Windows 8.1 x64.

这是一个没有 runif() 的更容易重现的示例.

Here's a much easier reproducible example without runif().

require(data.table) ## 1.9.4+
DT = data.table(x = 1:5)
DT[, y := x <= 2L]
#    x     y
# 1: 1  TRUE
# 2: 2  TRUE
# 3: 3 FALSE
# 4: 4 FALSE
# 5: 5 FALSE

DT[y == TRUE, .N]
# [1] 2             <~~~~~~ correct result.

DT[, y := x <= 3L]
#    x     y
# 1: 1  TRUE
# 2: 2  TRUE
# 3: 3  TRUE
# 4: 4 FALSE
# 5: 5 FALSE

DT[y == TRUE, .N]
# [1] 2             <~~~~~~ incorrect result, should be 3!

推荐答案

现已在 v1 中修复.9.5 在 GitHub 上.

Now fixed in v1.9.5 on GitHub.

:=set* 现在删除辅助键(v1.9.4 中的新功能)以便 DT[x==y] 工作在 :=set* 之后再次不需要 options(datatable.auto.index=FALSE).只有 setkey() 正确地删除了辅助键.添加了 23 项测试.感谢 user36312 的报告,#885.

:= and set* now drop secondary keys (new in v1.9.4) so that DT[x==y] works again after a := or set* without needing options(datatable.auto.index=FALSE). Only setkey() was dropping secondary keys correctly. 23 tests added. Thanks to user36312 for reporting, #885.

这篇关于运算符 == 在 data.table 中的逻辑列中不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆