r语言 - 在数据表中获取不同行的最有效方法是什么?



我有一个有很多行的数据表。

我正在考虑采用不同的选项来获取一组独特的行,包括

dt <- dt %>% unique(.)
dt <- dt %>% distinct()

最有效的方法是什么?我担心效率,因为它是一个20GB的文件。

unique可能是最高效的,因为有一个data.table实现。

示例数据(250米行,2列(。

library("data.table")
# Setting the number of threads to something reasonable for the benchmark.
# You don't need to normally set this. 
setDTthreads(6)
DT <- data.table(
obj=sample(LETTERS[1:10], 2.5e8, replace=TRUE),
val=sample(seq_len(10), 2.5e8, replace=TRUE)
)
> print(object.size(DT), units="Gb")
2.8 Gb

基准。

bench::mark(distinct=distinct(DT), unique=unique(DT), iterations=5)
# A tibble: 2 x 13
expression   min median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time
<bch:expr> <bch> <bch:>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm>
1 distinct   5.28s   5.4s     0.185    2.93GB    0.123     3     2     16.24s
2 unique     1.91s  1.97s     0.504  953.69MB    0         5     0      9.93s
# … with 4 more variables: result <list>, memory <list>, time <list>, gc <list>

最新更新