R:有效地从数组中删除单例维度
R: Efficiently remove singleton dimensions from array
我正在寻找一种从 R 中的数组中删除冗余维度的快速方法,类似于 MATLAB 中的 squeeze()
命令。
现在我结合了 melt()
和 reshape2
包中的 cast()
命令,但应该有一个不那么复杂的方法来做同样的事情。
到目前为止我是这样做的:
require(reshape2)
array3d <- array(rep(0,4),dim=c(1,2,2)) # create a 2*2 matrix within a 3-d array
acast(melt(array3d),Var2~Var3) # recover the matrix
听起来您正在寻找 drop()
,"delete[s] the dimensions of an array which have only one level"。
drop(array3d)
# [,1] [,2]
# [1,] 0 0
# [2,] 0 0
我正在寻找一种从 R 中的数组中删除冗余维度的快速方法,类似于 MATLAB 中的 squeeze()
命令。
现在我结合了 melt()
和 reshape2
包中的 cast()
命令,但应该有一个不那么复杂的方法来做同样的事情。
到目前为止我是这样做的:
require(reshape2)
array3d <- array(rep(0,4),dim=c(1,2,2)) # create a 2*2 matrix within a 3-d array
acast(melt(array3d),Var2~Var3) # recover the matrix
听起来您正在寻找 drop()
,"delete[s] the dimensions of an array which have only one level"。
drop(array3d)
# [,1] [,2]
# [1,] 0 0
# [2,] 0 0