Reputation: 89
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.
Upvotes: 9
Views: 5009
Reputation: 94192
Suggestions:
Get your supervisor to work with R
Use the ncdf
package to write a NetCDF file, which can be multi-dimensional (its often used for space-time data which is 4D)
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.
Upvotes: 10
Reputation: 8691
I think you want 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()
If console output size is a problem, just use options(max.print=...)
to change it temporarily.
Upvotes: 0