快速将大型2d矩阵快速熔化为3列数据 [英] fast melt large 2d matrix to 3 column data.table

查看:37
本文介绍了快速将大型2d矩阵快速熔化为3列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大矩阵 num [1:62410,1:48010]

我想要长格式的数据表.

例如

  Var1 Var2值1:1 1 -4227.7862:2 1 -4211.9083:3 1 -4197.0344:4 1 -4183.6455:5 1 -4171.6926:6 1 -4161.634 

最小示例

  m = matrix(1:5,nrow = 1000,ncol = 1000)x = data.table(reshape2 :: melt(m)) 

理想情况下,我希望列名称分别为x,y和value.

以前,我一直在使用 data.table(melt(mymatrix)).但是根据警告, reshape2 :: melt 已过时,这可能不是最佳的速度,什么是解决此问题的最佳"data.table"方法?

以下内容无法回答我的问题:快速熔化的data.table操作正确/最快的方法来重塑data.table

其他答案是指已弃用的 reshape2

解决方案

下面是一个示例:

 #示例矩阵m =矩阵(1:12,nrow = 4)#加载数据表库(data.table) 

我们可以直接提取数据,行和列信息,并且应该很快:

  dt = data.table(行= rep(seq_len(nrow(m)),ncol(m)),col = rep(seq_len(ncol(m)),每个= nrow(m)),值= c(m)) 

结果是:

 行col值1:1 1 12:2 1 23:3 1 34:4 1 45:1 2 56:2 2 67:3 2 78:4 2 89:1 3 910:2 3 1011:3 3 1112:4 3 12 

I have a large matrix num [1:62410, 1:48010]

I want this in a long format data.table

e.g.

   Var1 Var2     value
1:    1    1 -4227.786
2:    2    1 -4211.908
3:    3    1 -4197.034
4:    4    1 -4183.645
5:    5    1 -4171.692
6:    6    1 -4161.634

minimal example

m = matrix(1:5, nrow = 1000, ncol = 1000)
x = data.table(reshape2::melt(m))

ideally I'd want the columns names x, y and value at the same time.

Previously I've been using data.table(melt(mymatrix)). But judging by the warnings that reshape2::melt is deprecated, this is probably not optimal in terms of speed, what would be the best "data.table" way of solving this?

the following do not answer my question: Fast melted data.table operations Proper/fastest way to reshape a data.table

Other answers refer to the deprecated reshape2 package

解决方案

Here's an example:

# example matrix
m = matrix(1:12, nrow = 4)

# load data table
library(data.table)

We can extract the data, row and column info directly and it should be pretty fast:

dt = data.table(
  row = rep(seq_len(nrow(m)), ncol(m)), 
  col = rep(seq_len(ncol(m)), each = nrow(m)), 
  value = c(m)
)

The result is:

    row col value
 1:   1   1     1
 2:   2   1     2
 3:   3   1     3
 4:   4   1     4
 5:   1   2     5
 6:   2   2     6
 7:   3   2     7
 8:   4   2     8
 9:   1   3     9
10:   2   3    10
11:   3   3    11
12:   4   3    12

这篇关于快速将大型2d矩阵快速熔化为3列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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