r - 如何从多维数组到维护组的一维
问题描述
我有一个 4 维数组:位置(3)x 物种(3)x 季节(6)x 深度(2)。像这个矩阵一样 12 次。
Season = 1, depth = 1
[A] [B] [C]
[a] 12 52 55
[b] 13 14 235
[c] 13 76 355
我想将所有内容合并到一个大矩阵中,例如:
Season = 1, depth = 1
[A] [B] [C]
[a11] 12 52 55
[b11] 13 14 235
[c11] 13 76 355
[a12] 12 52 55
[b12] 13 14 235
[c12] 13 76 355
[a21] 12 52 55
[b21] 13 14 235
[c21] 13 76 355
...
等等。第一个数字表示一个额外的维度,第二个表示另一个维度。是否有意义?任何想法?
非常感谢!!:)
解决方案
这将数组转置aperm
,然后生成一个矩阵。
location = 3
species = 3
Season = 6
Depth = 2
set.seed(1)
myArr <- array(sample(1000, location * species * Season * Depth), dim = c(location, species, Season, Depth))
myArrPerm <- aperm(myArr, perm = c(1,3,4,2))
matrix(myArrPerm, ncol = species)
推荐阅读
- serialization - 将 PostGIS 中的数据序列化为 JSON
- r - gganimate 根据时间为多条路径设置动画
- android - RecyclerView 适配器不适用于黄油刀
- mysql - 创建过程mysql CRUD时出错
- vim - Vim 在插入模式退出时禁用 make
- java - Java 客户端 SSL 重新协商
- javascript - 将 promise 与 resolve & reject 转换为异步函数的正确方法(puppeteer)
- python - PLY 可以处理输入而不将其全部加载到内存中吗?
- python - 氢包原子编辑器——奇怪的显示
- iot - Beaglebone Black:如何以编程方式打开/关闭引脚?