有时,我们想调换数组的维度,比如:将data变量的行维度(male, female)变为第三个维度;列维度(young, middle, old)变为行维度;第三个维度(A-D)变为列维度。在R中,可使用aperm()函数来实现这个功能:
> data
, , A
young middle old
male 1 3 5
female 2 4 6
, , B
young middle old
male 7 9 11
female 8 10 12
, , C
young middle old
male 13 15 17
female 14 16 18
, , D
young middle old
male 19 21 23
female 20 22 24
> new.data <- aperm(data, c(2,3,1)) ##调换数组的维度,将data的行维度变为第三个维度;列维度变为行维度;第三个维度变为列维度
> new.data
, , male
A B C D
young 1 7 13 19
middle 3 9 15 21
old 5 11 17 23
, , female
A B C D
young 2 8 14 20
middle 4 10 16 22
old 6 12 18 24
暂无数据