3D数组值赋值会破坏数组的结构

时间:2015-05-11 01:51:40

标签: r matrix

以下是如何重现我的问题。我想创建一个3D数组

> dim(x)
[1] 31 31
> x
    1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1  NA  0  2  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  1  0  0  0  0  0  0  0  0
2   0 NA  1  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0
3   2  1 NA  0  0  0  0  0  0  1  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0
4   0  0  0 NA  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0
5   0  0  0  0 NA  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
6   0  0  0  0  0 NA  1  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  1  0  0  0  0  0
7   0  0  0  0  0  1 NA  0  0  1  0  1  0  0  0  0  1  0  0  0  1  0  0  0  0  0  0  0  0  0  0
8   0  0  0  0  0  0  0 NA  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
9   0  0  0  0  0  0  0  0 NA  0  2  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
10  0  1  1  0  0  0  1  0  0 NA  0  0  0  0  0  0  1  1  0  0  0  0  0  0  0  0  0  0  0  1  0
11  0  0  0  0  0  0  0  0  2  0 NA  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0
12  0  0  0  0  0  0  1  1  0  0  0 NA  0  0  0  1  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0
13  0  0  0  0  0  0  0  0  0  0  0  0 NA  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0
14  0  0  0  0  0  0  0  0  0  0  0  0  0 NA  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
15  0  0  1  1  1  0  0  0  0  0  0  0  0  1 NA  1  0  0  0  0  0  0  0  0  0  0  1  0  0  0  1
16  0  0  0  0  0  0  0  0  0  0  0  1  0  0  1 NA  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0
17  0  0  0  0  0  0  1  0  0  1  0  0  0  0  0  0 NA  0  0  0  0  0  0  1  0  0  0  0  0  0  0
18  0  0  0  0  0  0  0  0  0  1  0  0  1  0  0  0  0 NA  0  1  1  0  0  0  0  0  0  0  0  0  0
19  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0 NA  0  0  0  0  0  0  0  0  0  0  0  0
20  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0 NA  0  0  0  0  0  0  0  0  0  0  0
21  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  1  0  0 NA  0  0  0  0  0  0  0  0  0  0
22  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 NA  1  0  0  0  0  0  0  0  0
23  1  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  1 NA  0  0  0  0  0  0  0  0
24  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0 NA  0  0  1  0  0  0  0
25  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 NA  0  0  0  0  0  0
26  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 NA  0  0  0  0  0
27  0  0  0  0  0  0  0  0  0  0  0  1  0  0  1  0  0  0  0  0  0  0  0  1  0  0 NA  0  1  0  0
28  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0 NA  0  0  0
29  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0 NA  0  0
30  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 NA  0
31  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 NA

这是尺寸为31×31的x

> g[1,,] = x

当我尝试将x分配给第一个'部分时使用

> dim(g)
NULL
> head(g)
[[1]]
[1] NA  0  2  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  1  0  0  0  0  0  0  0  0

[[2]]
[1] 0

[[3]]
[1] 0

[[4]]
[1]  0 NA  1  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0

[[5]]
[1] 0

[[6]]
[1] 0

g的数组结构完全改变了,现在变成了:

mysqli_query($db, "DELETE FROM registered WHERE id=$id");

这与我的预期完全不同,我只是想把一个矩阵放到g [1 ,,]而dim(g)应该仍然是3乘31乘31,我错了吗?我哪里做错了?

1 个答案:

答案 0 :(得分:0)

感谢Pascal在下面的评论,我已经修改了我的答案,尽管我已经将维度更改为31x31x3,以便更容易理解。问题是在存储到数组之前从data.frame对象转换数据的方式。我认为首先转换为矩阵你应该得到你想要的东西:

<t>
   <record>
      <identifier>hdl.handle.net/11134/200003:93939393</identifier>
      <source>200003:93939393/TN</source>
   </record>
   <record>
      <identifier>hdl.handle.net/11134/50003:93939393</identifier>
      <source>50003:93939393/TN</source>
   </record>
   <record>
      <identifier>hdl.handle.net/11134/94949:93999393</identifier>
      <source>94949:93999393/TN</source>
   </record>
</t>