如何更改数组尺寸的顺序 [英] 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屋!