Say, I have a matrix m with 2 rows and 3 columns, together with a function which takes in an argument of numeric type and return a vector. For example,
f <- function(x){
rep(x,4)
}
If I use
t <- apply(m, MARGIN = c(1,2), FUN = f)
I will get a tensor t with dim(t) == c(4,2,3). But what I want is a tensor t with dim(t) == c(2,3,4). How can I do this conveniently? Thank you.
Use
aperm, a generalized transposition:See here: How to change order of array dimensions