将多维矩阵保存到文件 [英] Saving a multi-dimensional matrix to a file

查看:120
本文介绍了将多维矩阵保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个6D矩阵(最终可能会变大),我想另存为 txt csv xcel 文件.我之所以要这样做,是因为输出对于控制台窗口来说太大了,我只希望能够在分析数​​据之前快速查看数据.另外,我的主管不使用R,因此,如果我希望他查看数据,则需要能够将其导出到R中.

我想要的是能够看到数据,标题告诉我我在矩阵的哪一部分.即

、、、 1、5、6、5

可选地,如果数据集的每一行指定其位于矩阵的哪一部分中,则可以将数据重新布置到表中.矩阵每个维度的一列.

到目前为止,我尝试write.matrixwrite.csvwrite.big.matrix均未成功.

我为write.big.matrix收到的错误消息是:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘write.big.matrix’ for signature ‘"array", "character"’

非常感谢您的帮助.

解决方案

建议:

  1. 让您的主管与R合作

  2. 使用ncdf包编写一个NetCDF文件,该文件可以是多维的(它通常用于4D时空数据)

  3. 使用reshape2包将数组转换为i,j,k,l,m,n,value格式的数据帧(其中i到n是您的维度索引,值是A[i,j,k,l,m,n]的值.这是一个3维示例:

    a = array(sample(24),c(2,3,4))

a现在是3d数组:

> a
, , 1

     [,1] [,2] [,3]
[1,]   23   21   20
[2,]   22    7   14

, , 2

     [,1] [,2] [,3]
[1,]   11    9    5
[2,]   12    6   17

, , 3

     [,1] [,2] [,3]
[1,]   16    3   24
[2,]    1   10    4

, , 4

     [,1] [,2] [,3]
[1,]    2   19    8
[2,]   18   15   13

然后是一线:

> require(reshape2)
> melt(a)
   Var1 Var2 Var3 value
1     1    1    1    23
2     2    1    1    22
3     1    2    1    21
4     2    2    1     7
5     1    3    1    20
6     2    3    1    14
7     1    1    2    11
...
17    1    3    3    24
18    2    3    3     4
19    1    1    4     2
20    2    1    4    18
21    1    2    4    19
22    2    2    4    15
23    1    3    4     8
24    2    3    4    13

要获取一些东西,您可以write.table作为CSV文件供喜欢Excel的主管使用.

I have a 6D matrix (it may eventually get larger) that I would like to save as a txt, csv or xcel file. I want to do this because the output is too big for the console window and I just want to be able to look over the data quickly before analysing it. Also, my supervisor doesn't work with R so if I want him to look at the data I need to be able to export it out of R.

What I want is to be able to see the data, with headings telling me which part of the matrix I'm in. i.e.

, , 1, 5, 6, 5

Alternatively the data could be rearranged into a table provided that each row of the data set specifies which part of the matrix it was in. e.g. a column per dimension of the matrix.

So far I've tried write.matrix, write.csv, and write.big.matrix with no success.

The error message I get for write.big.matrix is:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘write.big.matrix’ for signature ‘"array", "character"’

Many thanks for your help.

解决方案

Suggestions:

  1. Get your supervisor to work with R

  2. Use the ncdf package to write a NetCDF file, which can be multi-dimensional (its often used for space-time data which is 4D)

  3. Use the reshape2 package to convert the array into a i,j,k,l,m,n,value formatted data frame (where i to n are your dimension indexes, and value is the value of A[i,j,k,l,m,n]. Here's a 3-dimensional example:

    a=array(sample(24),c(2,3,4))

a is now a 3d array:

> a
, , 1

     [,1] [,2] [,3]
[1,]   23   21   20
[2,]   22    7   14

, , 2

     [,1] [,2] [,3]
[1,]   11    9    5
[2,]   12    6   17

, , 3

     [,1] [,2] [,3]
[1,]   16    3   24
[2,]    1   10    4

, , 4

     [,1] [,2] [,3]
[1,]    2   19    8
[2,]   18   15   13

Its then a one-liner:

> require(reshape2)
> melt(a)
   Var1 Var2 Var3 value
1     1    1    1    23
2     2    1    1    22
3     1    2    1    21
4     2    2    1     7
5     1    3    1    20
6     2    3    1    14
7     1    1    2    11
...
17    1    3    3    24
18    2    3    3     4
19    1    1    4     2
20    2    1    4    18
21    1    2    4    19
22    2    2    4    15
23    1    3    4     8
24    2    3    4    13

to get something you can write.table as a CSV file for your Excel-loving supervisor.

这篇关于将多维矩阵保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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