[R] as.list.matrix
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Oct 29 08:08:18 CEST 2004
On Fri, 29 Oct 2004, Peter Alspach wrote:
>
> Kjetil
>
> Isn't a data.frame as special type of list, and thus one could use
> as.data.frame?
Yes, or
split(mat, col(mat))
works. So simple it hardly needs an as.list method.
> Peter Alspach
>
>
> >>> Kjetil Brinchmann Halvorsen <kjetil at acelerate.com> 29/10/04
> 14:16:03 >>>
> 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?
Daily?? You created the matrix in the first place: why not create a data
frame instead - na.omit was written for data frames? And surely do.call
is overkill for a 2-column matrix, and there seems a surplus c() in there.
m <- na.omit(cbind(VRS$Tmed, resid(VRS.mod1,type="response"))
scatter.smooth(m[,1], m[, 2], xlab="", ylab="")
is much easier to follow.
But do you need this? The strange thing is that loess.smooth seems to
have been written expecting missing values, but does not work with them.
It is easy to fix.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list