filter rows in data.table with `by`

有些话、适合烂在心里 提交于 2020-11-26 16:53:49

问题


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

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.

Expected behavior (by dplyr)

As you can see dplyr works as expected, and brings back only values with 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[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

回答1:


You could use [ as in

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



回答2:


We need to use if

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


来源:https://stackoverflow.com/questions/34393053/filter-rows-in-data-table-with-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!