使用both和/ =函数的意外行为

时间:2016-07-01 09:59:59

标签: r data.table

我猜data.table(1.9.6)包的unique-function中存在一个错误:

小例子:

test <- data.table(a = c("1", "1", "2", "2", "3", "4", "4", "4"), 
                   b = letters[1:8], 
                   d = c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))

   a b     d
1: 1 a  TRUE
2: 1 b  TRUE
3: 2 c FALSE
4: 2 d FALSE
5: 3 e  TRUE
6: 4 f FALSE
7: 4 g FALSE
8: 4 h FALSE

test[d == TRUE, `:=` (b = "M")]
test <- unique(test, by = c("a", "b"))

   a b     d
1: 1 M  TRUE
2: 2 c FALSE
3: 2 d FALSE
4: 3 M  TRUE
5: 4 f FALSE
6: 4 g FALSE
7: 4 h FALSE

此时一切都很完美,但现在我只想选择列d为真的行:

test[d == TRUE]
   a b    d
1: 1 M TRUE

但结果是错误的。

2 个答案:

答案 0 :(得分:5)

该错误刚刚在开发存储库中修复。

library(data.table)
test <- data.table(a = c("1", "1", "2", "2", "3", "4", "4", "4"), 
                   b = letters[1:8], 
                   d = c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
test[d == TRUE, `:=` (b = "M")]
test <- unique(test, by = c("a", "b"))
test[d == TRUE]
#   a b    d
#1: 1 M TRUE
#2: 3 M TRUE

开发版本data.table已经在drat repo中发布,可以easily installed通过:

install.packages("data.table", repos="https://Rdatatable.github.io/data.table", type="source")

感谢报道!

答案 1 :(得分:0)

如果不解决错误,它可以使用普通的data.frame语法:

test[test$d, ]

test[test$d == TRUE, ]