简体   繁体   English

将矩阵转换为 data.table 的最快方法

[英]Fastest way to convert a matrix to a data.table

What is the fastest way to convert a matrix like the following to a data.table?将如下矩阵转换为 data.table 的最快方法是什么?

mat <- matrix(1:9, nrow = 3)

1 2 3
4 5 6
7 8 9

I can convert this to a data.table with the code below我可以使用以下代码将其转换为 data.table

setDT(data.frame(mat))[]

But is this the fastest way?但这是最快的方法吗? Can we do this without first converting it to a data.frame?我们可以在不先将其转换为 data.frame 的情况下做到这一点吗?

Here are some examples to discuss about the speed of forming data.table , ie, as.data.table , setDT(as.data.frame(mat)) and setDT(data.frame(mat)) .这里有一些例子来讨论形成data.table的速度,即as.data.tablesetDT(as.data.frame(mat))setDT(data.frame(mat))

  • when rows are less than columns:当行小于列时:
mat <- matrix(1:5e3, nrow = 5)
microbenchmark(unit = "relative",
               as.data.table(mat),
               setDT(as.data.frame(mat))[],
               setDT(data.frame(mat))[])

Unit: relative
                        expr      min       lq     mean   median       uq      max neval
          as.data.table(mat) 1.433084 1.417747 1.340552 1.413278 1.414386 1.070289   100
 setDT(as.data.frame(mat))[] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000   100
    setDT(data.frame(mat))[] 1.287526 1.281964 1.237544 1.284735 1.258662 1.186977   100
  • when rows are more than columns:当行多于列时:
mat <- matrix(1:5e3, nrow = 5e2)
microbenchmark(unit = "relative",
               as.data.table(mat),
               setDT(as.data.frame(mat))[],
               setDT(data.frame(mat))[])

Unit: relative
                        expr      min       lq     mean   median       uq      max neval
          as.data.table(mat) 1.114003 1.041410 1.083238 1.070029 1.049262 1.254732   100
 setDT(as.data.frame(mat))[] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000   100
    setDT(data.frame(mat))[] 1.680208 1.467538 1.482018 1.511009 1.479438 1.440440   100

Remark : setDT(as.data.frame(mat))[] is the winner of speed备注setDT(as.data.frame(mat))[]是速度的赢家

You could use a magrittr pipe:您可以使用magrittr pipe:

mat <- matrix(1:9, nrow = 3) %>% data.table()

Without magrittr :没有magrittr

mat <- data.table(matrix(1:9, nrow = 3)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM