在R中导出数组,每个"矩阵"在另一张桌子中分开

时间:2017-07-20 17:35:04

标签: arrays r csv

我想在csv中导出这个数组,我的数组的每个矩阵都在单独的表中导出

> 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

使用write.csv()时的结果是每个矩阵连接在一起的表。我想在我的csv中使用3个不同的表。

你可以帮帮我吗?

what I want

1 个答案:

答案 0 :(得分:1)

仍然没有100%清楚我,但这是你想要做的吗?

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)

产生的csv:

enter image description here