Saving a multidimensional matrix to a file

I have a 6D matrix (it can eventually increase) that I would like to save as a txt, csv or xcel file. I want to do this because the output is too large for the console window, and I just want to be able to quickly view the data before analyzing it. In addition, my supervisor does not work with R, so if I want him to look at the data I need to export it from R.

I want to see the data, and the headers tell me which part of the matrix I enter. ie

1, 5, 6, 5

Alternatively, the data can be rearranged into a table, provided that each row of the data set indicates in which part of the matrix it was located. column to the size of the matrix.

So far I have not tried write.matrix, write.csvand write.big.matrix.

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

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

Thank you very much for your help.

+4
source share
2 answers

Suggestions:

  • Ask your supervisor to work with R

  • Use a package ncdfto write a NetCDF file, which can be multidimensional (it is often used for 4D time-time data)

  • Use the package reshape2to convert the array to a data format i,j,k,l,m,n,value(where i - n are your dimension indices and value is value A[i,j,k,l,m,n]). Here is a three-dimensional example.

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

a is now a three-dimensional 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

Then this is single line:

> 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.

+7

, sink()

sink("file.txt")
array(data=1:1000,dim=c(2,5,2,5,2,5)) # your array here
## back to the console
sink(type = "message")
sink()

, options(max.print=...), .

0

Source: https://habr.com/ru/post/1524655/


All Articles