[R] as.list.matrix

Kjetil Brinchmann Halvorsen kjetil at acelerate.com
Fri Oct 29 03:16:03 CEST 2004


I found the need of converting a matrix into a list of its columns
(for use with do.call), and was surprised there was no method
as.list.matrix, it could easily be a part of as.list default

I wrote

my.as.list.matrix <- function(mat) {
       if(!is.matrix(mat))stop("Argument must be a matrix")
       n <- NCOL(mat)
       res <- vector(mode="list", length=n)
       for (i in seq(along=res))
         res[[i]] <- mat[,i]
       res
}

but still think there must be some in-built function doing this!

By the way, my use is

 with(VRS,do.call("scatter.smooth",
    c(my.as.list.matrix(na.omit(cbind(Tmed, 
resid(VRS.mod1,type="response")))),
        xlab="", ylab="")))

there shoud be an easier way for such a daily task?

Kjetil

-- 

Kjetil Halvorsen.

Peace is the most effective weapon of mass construction.
               --  Mahdi Elmandjra




More information about the R-help mailing list