R:有效地从数组中删除单例尺寸 [英] R: Efficiently remove singleton dimensions from array
本文介绍了R:有效地从数组中删除单例尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种从R中的数组中删除冗余维的快速方法,类似于MATLAB中的squeeze()
命令.
现在,我结合了reshape2
软件包中的melt()
和cast()
命令,但是应该有一种不太复杂的方法.
I am looking for a fast way to remove redundant dimensions from an array in R, similar to the squeeze()
command in MATLAB.
Right now I combine the melt()
and the cast()
commands from the reshape2
package, but there should be a less intricate way of doing the same.
到目前为止,这是我的操作方式:
This is how I do it so far:
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()
,它删除仅具有一个级别的数组的维数".
It sounds like you're looking for drop()
, which "delete[s] the dimensions of an array which have only one level".
drop(array3d)
# [,1] [,2]
# [1,] 0 0
# [2,] 0 0
这篇关于R:有效地从数组中删除单例尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文