如何更改数组维度的顺序 [英] How to change order of array dimensions
问题描述
如何重新排序 n 维数组的维度.例如,如果我有一个销售数据的三维数组,其中第一维表示日期,第二维是商店,第三维是部门.如何转换数组,使第一个维度是 Store,第二个维度是 Department,第三个维度是 Date.这只是一个例子.我希望有一个通用的解决方案.
How do I reorder the dimensions of an n dimensional array. For example, if I have a three dimensional array of sales data, where the first dimension represents the Date, the second dimension is the Store, and the third dimension is Department. How do I transform the array so that the first dimension is Store, the second is Department, and the third is Date. This is just an example. I am hoping for a general solution.
推荐答案
执行此操作的函数是 aperm
,来自基础包.它是转置 t()
函数对多维数组的推广.对于您的示例,您可以按如下方式调用它:
The function for doing that is aperm
, from the base package. It is a generalization of the transpose t()
function to multidimensional arrays. For your example, you would call it as follows:
new.data <- aperm(old.data, c(2,3,1))
这篇关于如何更改数组维度的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!