Jose Victor Zambrana
Jose Victor Zambrana

Reputation: 519

export array in R with every "matrix" separate in a different table

I want to export this array in csv where every matrix of my array is exported in separate tables

> a <- aperm(array(1:96, dim= c(4,8,3)), c(2,1,3))
, , 1

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16
[5,]   17   18   19   20
[6,]   21   22   23   24
[7,]   25   26   27   28
[8,]   29   30   31   32

, , 2

     [,1] [,2] [,3] [,4]
[1,]   33   34   35   36
[2,]   37   38   39   40
[3,]   41   42   43   44
[4,]   45   46   47   48
[5,]   49   50   51   52
[6,]   53   54   55   56
[7,]   57   58   59   60
[8,]   61   62   63   64

, , 3

     [,1] [,2] [,3] [,4]
[1,]   65   66   67   68
[2,]   69   70   71   72
[3,]   73   74   75   76
[4,]   77   78   79   80
[5,]   81   82   83   84
[6,]   85   86   87   88
[7,]   89   90   91   92
[8,]   93   94   95   96

The result I have when using write.csv() is a table with every matrix joint together. I want 3 different tables in my csv.

May you help me?

what I want

Upvotes: 1

Views: 189

Answers (1)

Florian
Florian

Reputation: 25375

Still not 100% clear to me, but is this what you are trying to do?

a = aperm(array(1:96, dim= c(4,8,3)), c(2,1,3))

b = a[,,1]

for( i in 2:dim(a)[3])
{
  # uncomment the line below if you want no white space between tables.
  b = rbind(b, rep("",ncol(b)))
  b = rbind(b,a[,,i])
}

write.csv(b,file="test.csv",row.names = F)

Resulting csv:

enter image description here

Upvotes: 1

Related Questions