Dear all, suppose I have following matrix:> mat <- matrix(rnorm(25), 5) > > mat[,1] [,2] [,3] [,4] [,5] [1,] 0.97056228 -1.3278509 -0.73511792 2.1650629 -0.4411997 [2,] 0.58613700 -0.2559899 -1.18334248 -1.4990907 1.8138846 [3,] -1.03333313 2.0227887 0.89622681 0.6483973 -1.5523283 [4,] 0.38968833 0.2490004 -0.02301061 -0.2705150 -0.9237268 [5,] 0.03306289 -0.4022751 -0.44404905 -1.6810542 -0.1016683 Now from "mat" I would like to create a "list" object where i-th column of "mat" will represent i-th element of that list object. I am looking for some way to avoid for loop. It will be really helpful if somebody points me on that. Thanks, [[alternative HTML version deleted]]
On Sep 3, 2010, at 7:48 AM, Ron Michael wrote:> Dear all, suppose I have following matrix: > >> mat <- matrix(rnorm(25), 5) >> >> mat > [,1] [,2] [,3] [,4] [,5] > [1,] 0.97056228 -1.3278509 -0.73511792 2.1650629 -0.4411997 > [2,] 0.58613700 -0.2559899 -1.18334248 -1.4990907 1.8138846 > [3,] -1.03333313 2.0227887 0.89622681 0.6483973 -1.5523283 > [4,] 0.38968833 0.2490004 -0.02301061 -0.2705150 -0.9237268 > [5,] 0.03306289 -0.4022751 -0.44404905 -1.6810542 -0.1016683 > > Now from "mat" I would like to create a "list" object where i-th column of "mat" will represent i-th element of that list object. > > I am looking for some way to avoid for loop. It will be really helpful if somebody points me on that. > > Thanks,Try this: set.seed(1) mat <- matrix(rnorm(25), 5)> mat[,1] [,2] [,3] [,4] [,5] [1,] -0.6264538 -0.8204684 1.5117812 -0.04493361 0.91897737 [2,] 0.1836433 0.4874291 0.3898432 -0.01619026 0.78213630 [3,] -0.8356286 0.7383247 -0.6212406 0.94383621 0.07456498 [4,] 1.5952808 0.5757814 -2.2146999 0.82122120 -1.98935170 [5,] 0.3295078 -0.3053884 1.1249309 0.59390132 0.61982575 # Coerce 'mat' to a data frame, then to a 'normal' list> as.list(as.data.frame(mat))$V1 [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 $V2 [1] -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884 $V3 [1] 1.5117812 0.3898432 -0.6212406 -2.2146999 1.1249309 $V4 [1] -0.04493361 -0.01619026 0.94383621 0.82122120 0.59390132 $V5 [1] 0.91897737 0.78213630 0.07456498 -1.98935170 0.61982575 You can argue that the coercion to a list is redundant, since a data frame is a list, but it may depend upon what you then want to do with the data. HTH, Marc Schwartz
Try this: unclass(as.data.frame(mat)) On Fri, Sep 3, 2010 at 9:48 AM, Ron Michael <ron_michael70@yahoo.com> wrote:> Dear all, suppose I have following matrix: > > > mat <- matrix(rnorm(25), 5) > > > > mat > [,1] [,2] [,3] [,4] [,5] > [1,] 0.97056228 -1.3278509 -0.73511792 2.1650629 -0.4411997 > [2,] 0.58613700 -0.2559899 -1.18334248 -1.4990907 1.8138846 > [3,] -1.03333313 2.0227887 0.89622681 0.6483973 -1.5523283 > [4,] 0.38968833 0.2490004 -0.02301061 -0.2705150 -0.9237268 > [5,] 0.03306289 -0.4022751 -0.44404905 -1.6810542 -0.1016683 > > Now from "mat" I would like to create a "list" object where i-th column of > "mat" will represent i-th element of that list object. > > I am looking for some way to avoid for loop. It will be really helpful if > somebody points me on that. > > Thanks, > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]