data.table:如何在聚合/汇总后删除分组变量

时间:2015-08-05 22:43:05

标签: r group-by data.table

我有一个data.frame,如下所示。我想在count上对element_id进行总结,然后删除element_id。我想有效地做到这一点,但我只能通过总结并分配给变量,然后使用[,element_id:=NULL]删除列来实现这一点。

   element_id count
1           1     1
2           1     1
3           1     3
4           1     2
5           2     1
6           2     1
7           2     1
8           2     3
9           2     1
10          2     2

这是所需的输出:

   short poly
1:     3    1
2:     5    1

这是我的代码:

library(data.table)

long_dat <- structure(list(
              element_id = c(1L,1L,1L,1L,2L,2L,2L,2L,2L,2L),
              count = c(1,1,3,2,1,1,1,3,1,2)),
              .Names = c("element_id","count"), row.names = c(NA,-10L), class = "data.frame")

data.table::setDT(long_dat)
data.table::setkey(long_dat, "element_id")
long_dat[, list(short = sum(count < 3), poly = sum(count > 2)), by=element_id]

# almost there
   element_id short poly
1:          1     3    1
2:          2     5    1

# an attempt to aggregate and drop at once but the aggregation does not occur
long_dat[, list(short = sum(count < 3), poly = sum(count > 2)), by=element_id][, element_id:=NULL]

# works but I assume the reassignment is inefficient
long_dat <- long_dat[, list(short = sum(count < 3), poly = sum(count > 2)), by=element_id]
long_dat[, element_id:=NULL]

0 个答案:

没有答案