nopeva
nopeva

Reputation: 1605

sort list of matrices by number of columns in R

I have a list of matrices generated using expand.grid().

myarray=matrix(rexp(200),10,3)
list.of.matrices <- apply(expand.grid(rep(list(c(FALSE, TRUE)), ncol(myarray))),
                              1, function(j)myarray[, j, drop = FALSE])

When I used the command

myarray.dim=sapply(myarray, ncol)

I've noticed that the list is not generated in the order that I need. I would like to get them ordered starting from 1 column to 3 if possible.

Upvotes: 1

Views: 150

Answers (1)

Roland
Roland

Reputation: 132969

Like this?

lapply(order(sapply(list.of.matrices,ncol)),function(i) list.of.matrices[[i]])

Your list has a zero column entry.

Upvotes: 2

Related Questions