使用`by`过滤data.table中的行

时间:2022-12-20 19:40:04

I would like to filter group which following criteria. The DT brings unexpected results.

我想按照以下标准过滤组。 DT带来意想不到的结果。

Input data

library(data.table)
library(dplyr)

dt <- data.table(
    logic = c(TRUE, TRUE, FALSE, TRUE, TRUE, TRUE),
    group = c("A" , "A",  "A"  , "B" , "B" , "B")
)

I would like to filter group, where logic field values are all TRUE.

我想过滤组,逻辑字段值全部为TRUE。

Expected behavior (by dplyr)

As you can see dplyr works as expected, and brings back only values with group = B

正如您所见,dplyr按预期工作,并仅返回group = B的值

dt %>% 
  group_by(group) %>% 
  filter(all(logic))
# Source: local data table [3 x 2]
# Groups: group

#   logic group
# 1  TRUE     B
# 2  TRUE     B
# 3  TRUE     B

Unexpected behavior by data.table

DT doesn't really filter rows, either bringing all table or nothing.

DT并不真正过滤行,无论是带来所有表还是什么都没有。

dt[all(logic), group, by = group]
# Empty data.table (0 rows) of 2 cols: group,group

dt[all(.SD$logic), group,by = group]
#    group group
# 1:     A     A
# 2:     B     B

2 个解决方案

#1


5  

You could use [ as in

你可以使用[as in

dt[, .SD[all(logic)], by = group]
#   group logic
#1:     B  TRUE
#2:     B  TRUE
#3:     B  TRUE

#2


6  

We need to use if

我们需要使用if

dt[, if(all(logic)) .SD, by = group]
#    group logic
#1:     B  TRUE
#2:     B  TRUE
#3:     B  TRUE

#1


5  

You could use [ as in

你可以使用[as in

dt[, .SD[all(logic)], by = group]
#   group logic
#1:     B  TRUE
#2:     B  TRUE
#3:     B  TRUE

#2


6  

We need to use if

我们需要使用if

dt[, if(all(logic)) .SD, by = group]
#    group logic
#1:     B  TRUE
#2:     B  TRUE
#3:     B  TRUE