This may be a simple question and I am missing something. But It is bugging me. Assume this example data.table:
library(data.table)
test <- data.table(group1 = "a", group2 = "z", value = 1)
Why doesn't this work
test[group1 %in% c("a", "b"), sum(value), group2]
Erro em `[.data.table`(test, group1 %in% c("a", "b"), sum(value), group2) :
i[2] is 0. While grouping, i=0 is allowed when it's the only value. When length(i) > 1, all i should be > 0.
But this does:
test[group1 %in% c("a", "b"), ][,sum(value), group2]
group2 V1
1: z 1
Is this really the expected behavior?
Update: This behaviour is fixed in the development version, 1.9.5 and works as expected now.
Another workaround is to use data.table built in %chin% function
test[group1 %chin% c("a", "b"), sum(value), group2]
# group2 V1
# 1: z 1
This looks like a bug to me, but you can get your expected behavior with extra parentheses around the i:
test[(group1 %in% c("a", "b")),sum(value), group2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With