所以我有一个相当大的数组列表。简化版本如下所示:
> array_list <- list ( 'a1' = matrix(c(1,2,3,4),c(2,2)),
'a2' = matrix(c(5,6,7,8),c(2,2)))
我可以使用 abind
使其成为 3 维数组:
> array3 <- abind(array_list, along=0)
我可以给出维度坐标名称:
> array3 <- abind(array_list, along=0,
new.names=list(c('one','two'), c('a','b'), c('u','v')))
> str(array3)
num [1:2, 1:2, 1:2] 1 5 2 6 3 7 4 8
- attr(*, "dimnames")=List of 3
..$ : chr [1:2] "one" "two"
..$ : chr [1:2] "a" "b"
..$ : chr [1:2] "u" "v"
但我不关心坐标。我真正想做的是给维度本身命名(就像列表一样)。也就是说,像这样:
array3 <- abind(array_list, along=0, dim.names=c('array', 'x', 'y')
R> str(array3)
num [1:2, 1:2, 1:2] 1 5 2 6 3 7 4 8
- attr(*, "dimnames")=List of 3
..$ array: chr [1:2] "a1" "a2"
..$ x : NULL
..$ y : NULL
(注意 $ ... :
之间的位)
这可能吗?如果是这样,怎么办?
最佳答案
自 dimnames()
返回一个列表,可以使用names(dimnames(x)) <- ...
更改维度的名称。
例如,更改内置数据集的名称Titanic
:
str(Titanic)
table [1:4, 1:2, 1:2, 1:2] 0 0 35 0 0 0 17 0 118 154 ...
- attr(*, "dimnames")=List of 4
..$ Class : chr [1:4] "1st" "2nd" "3rd" "Crew"
..$ Sex : chr [1:2] "Male" "Female"
..$ Age : chr [1:2] "Child" "Adult"
..$ Survived: chr [1:2] "No" "Yes"
现在更改 dimnames
的名称:
names(dimnames(Titanic)) <- paste0("Dim_", 1:4)
结果:
str(Titanic)
table [1:4, 1:2, 1:2, 1:2] 0 0 35 0 0 0 17 0 118 154 ...
- attr(*, "dimnames")=List of 4
..$ Dim_1: chr [1:4] "1st" "2nd" "3rd" "Crew"
..$ Dim_2: chr [1:2] "Male" "Female"
..$ Dim_3: chr [1:2] "Child" "Adult"
..$ Dim_4: chr [1:2] "No" "Yes"
关于arrays - 将 R 数组列表与暗淡名称绑定(bind)在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11185524/