R: Efficiently remove singleton dimensions from array

萝らか妹 提交于 2019-12-01 16:22:28

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/33042757/r-efficiently-remove-singleton-dimensions-from-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!