[R] conversion of matrix into list

Norm Matloff matloff at cs.ucdavis.edu
Sat Jun 4 03:45:28 CEST 2011


Martin Spindler wrote:

> I have a matrix X which consists of 2 columns. I would like to convert this
> matrix into a list where every entry of the list consists of a single row of
> the matrix.

Here's another way besides split():

# returns a list of the matrix m's rows (rowcol=1) or columns
mat2lst <- function(m,rowcol=1) {
   if (rowcol == 1) m <- t(m)
   dm <- as.data.frame(m)
   as.list(dm)
}

This seems to be faster than the split() approach for columns, but
slower for rows.  Apparently the transpose operation makes the
difference.  (You could try investigating via Rprof().)

Norm Matloff



More information about the R-help mailing list