dplyr::slice in data.table [英] dplyr::slice in data.table

查看:20
本文介绍了dplyr::slice in data.table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data.table中执行以下操作的惯用方法是什么?

What is the idiomatic way to do the action below in data.table?

library(dplyr)
df %>% 
  group_by(b) %>% 
  slice(1:10)

我能做到

library(data.table)
df[, .SD[1:10]
   , by = b]

但这看起来要慢得多.有没有更好的办法?

but that appears much slower. Is there a better way?

set.seed(0)
df <- rep(1:500, sample(500:1000, 500, T)) %>% 
        data.table(a = runif(length(.))
                  ,b = .)

f1 <- function(df){
  df %>% 
    group_by(b) %>% 
    slice(1:10)
}
f2 <- function(df){
  df[, .SD[1:10]
     , by = b]
}

library(microbenchmark)
microbenchmark(f1(df), f2(df))
#Unit: milliseconds
#   expr      min       lq      mean   median        uq      max neval
# f1(df) 17.67435 19.50381  22.06026 20.50166  21.42668  78.3318   100
# f2(df) 69.69554 79.43387 119.67845 88.25585 106.38661 581.3067   100

========== 带有建议方法的基准 ==========

========== Benchmarks with suggested methods ==========

set.seed(0)
df <- rep(1:500, sample(500:1000, 500, T)) %>% 
        data.table(a = runif(length(.))
                  ,b = .)

use.slice <- function(df){
  df %>% 
    group_by(b) %>% 
    slice(1:10)
}
IndexSD <- function(df){
  df[, .SD[1:10]
     , by = b]
}
Index.I <- function(df) {
  df[df[, .I[seq_len(10)], by = b]$V1]
}
use.head <- function(df){
  df[, head(.SD, 10)
     , by = b]
}

library(microbenchmark)
microbenchmark(use.slice(df)
              , IndexSD(df)
              , Index.I(df)
              , use.head(df)
              , unit = "relative"
              , times = 100L)

#Unit: relative
#          expr       min        lq      mean    median        uq       max neval
# use.slice(df)  9.804549 10.269234  9.167413  8.900060  8.782862  6.520270   100
#   IndexSD(df) 38.881793 42.548555 39.044095 38.636523 39.942621 18.981748   100
#   Index.I(df)  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000   100
#  use.head(df)  3.666898  4.033038  3.728299  3.408249  3.545258  3.951565   100

推荐答案

我们可以使用.I来提取行索引,应该会更快

We can use .I to extract the row index and should be faster

out <- df[df[, .I[seq_len(10)], by = b]$V1]
dim(out)
#[1] 5000    2

检查是否有 NAs(如 OP 所述)

Checking if there are NAs (as the OP commented)

any(out[, Reduce(`|`, lapply(.SD, is.na))])
#[1] FALSE


dim(df)
#[1] 374337      2

基准

f3 <- function(df) {
  df[df[, .I[seq_len(10)], by = b]$V1]
 }

microbenchmark(f1(df), f2(df), f3(df), unit = "relative", times = 10L)
#Unit: relative
#   expr       min        lq      mean    median        uq      max neval cld
# f1(df)  5.727822  5.480741  4.945486  5.672206  4.317531  5.10003    10  b 
# f2(df) 24.572633 23.774534 17.842622 23.070634 16.099822 11.58287    10   c
# f3(df)  1.000000  1.000000  1.000000  1.000000  1.000000  1.00000    10 a  

这篇关于dplyr::slice in data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆