How to get column index matrix of an array in R? -
imagine have simple 4x3x2 array in r.
> x <- array(1:24,c(4,3,2), dimnames=list(c('a','b','c','d'),c('x','y','z'),1:2)) > x , , 1 x y z 1 5 9 b 2 6 10 c 3 7 11 d 4 8 12 , , 2 x y z 13 17 21 b 14 18 22 c 15 19 23 d 16 20 24
what i'd like, simple function on array gives me name of index of each element arbitrary dimension. in case, dimension 2.
the function behave this:
> arraydims(x,2) #where 2 dimension want names for. , , 1 [,1] [,2] [,3] [1,] "x" "y" "z" [2,] "x" "y" "z" [3,] "x" "y" "z" [4,] "x" "y" "z" , , 2 [,1] [,2] [,3] [1,] "x" "y" "z" [2,] "x" "y" "z" [3,] "x" "y" "z" [4,] "x" "y" "z"
the function
colmtx <- function(x, n) { return( array( rep(dimnames(x)[[n]], each=prod(dim(x)[0:(n-1)])), dim=dim(x) ) ) }
Comments
Post a Comment