在data.table中按组计数快速排在前N位

时间:2018-07-06 20:15:05

标签: r count data.table greatest-n-per-group

我想知道按组的外观计数来frank个子组的首选方式。

例如,我有属于细分市场且具有邮政编码的客户。我想知道每个细分市场中最常见的3个邮政编码。

library(data.table)
set.seed(123)
n <- 1e6
df <- data.table( cust_id = 1:n,
                  cust_segment = sample(LETTERS, size=n, replace=T),
                  cust_postal = sample(as.character(5e4:7e4),size=n, replace=T)
                 )

此链条(在下面的dcast()内)产生所需的输出,但需要两次通过,第一次通过按组-子组进行计数,第二次按组对计数进行排名。< / p>

dcast(
  df[,.(.N),
     by = .(cust_segment, cust_postal)
     ][,.(cust_postal,
          postal_rank = frankv(x=N, order=-1, ties.method = 'first')
     ), keyby=cust_segment
     ][postal_rank<=3],
  cust_segment ~ paste0('postcode_rank_',postal_rank), value.var = 'cust_postal' 
)
# desired output:
# cust_segment postcode_rank_1 postcode_rank_2 postcode_rank_3
#            A           51274           64588           59212
#            B           63590           69477           50380
#            C           60619           66249           53494 ...etc...

这是最好的吗?还是有单程方法?

1 个答案:

答案 0 :(得分:1)

从评论中摘录弗兰克的答案:

使用forder代替frankv并使用keyby,因为这比仅使用by的速度快

df[, .N, 
   keyby = .(cust_segment, cust_postal)
   ][order(-N), r := rowid(cust_segment)
     ][r <= 3, dcast(.SD, cust_segment ~ r, value.var ="cust_postal")]

    cust_segment     1     2     3
 1:            A 51274 53440 55754
 2:            B 63590 69477 50380
 3:            C 60619 66249 52122
 4:            D 68107 50824 59305
 5:            E 51832 65249 52366
 6:            F 51401 55410 65046

微基准测试时间:

library(microbenchmark)

microbenchmark(C8H10N4O2 = dcast(
                                df[,.(.N),
                                   by = .(cust_segment, cust_postal)
                                   ][,.(cust_postal,
                                        postal_rank = frankv(x=N, order=-1, ties.method = 'first')
                                   ), keyby=cust_segment
                                   ][postal_rank<=3],
                                cust_segment ~ paste0('postcode_rank_',postal_rank), value.var = 'cust_postal' 
                              ),
              frank = df[, .N, 
                         keyby = .(cust_segment, cust_postal)
                         ][order(-N), r := rowid(cust_segment)
                           ][r <= 3, dcast(.SD, cust_segment ~ r, value.var ="cust_postal")])
Unit: milliseconds
     expr      min       lq     mean   median       uq      max neval
C8H10N4O2 136.3318 140.8096 156.2095 145.6099 170.4862 205.8457   100
    frank 102.2789 110.0140 118.2148 112.6940 119.2105 192.2464   100

弗兰克的答案快了25%。