R: Effectively remove singleton sizes from an array

I am looking for a quick way to remove redundant measurements from an array in R, similar to a command squeeze()in MATLAB. Now I combine the team melt()and cast()out of the package reshape2, but should be less complicated way to do the same.

Here 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
+4
source share
1 answer

It looks like you are looking for drop()one that "removes [s] the dimensions of the array that have only one level."

drop(array3d)
#       [,1] [,2]
# [1,]    0    0
# [2,]    0    0
+7
source

Source: https://habr.com/ru/post/1610978/


All Articles