Dear R, Is there an efficient way to make a list that each element is from the corresponding column of a matrix. For example, if I have a matrix "a"> a <- matrix(1:10, 5, 2) > a[,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10 I would like to have a list "b" like this> b <- list(a[, 1], a[, 2]) > b[[1]] [1] 1 2 3 4 5 [[2]] [1] 6 7 8 9 10 Thanks in advance! Feng -- Feng Li Department of Statistics Stockholm University 106 91 Stockholm, Sweden http://feng.li/ [[alternative HTML version deleted]]
Try this: c(unname(as.data.frame(a))) On Tue, Jan 18, 2011 at 10:53 AM, Feng Li <m@feng.li> wrote:> Dear R, > > Is there an efficient way to make a list that each element is from the > corresponding column of a matrix. For example, if I have a matrix "a" > > > a <- matrix(1:10, 5, 2) > > a > [,1] [,2] > [1,] 1 6 > [2,] 2 7 > [3,] 3 8 > [4,] 4 9 > [5,] 5 10 > > I would like to have a list "b" like this > > > b <- list(a[, 1], a[, 2]) > > b > [[1]] > [1] 1 2 3 4 5 > > [[2]] > [1] 6 7 8 9 10 > > > Thanks in advance! > > > Feng > > -- > Feng Li > Department of Statistics > Stockholm University > 106 91 Stockholm, Sweden > http://feng.li/ > > [[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]]
hi feng, a possible solution is b1<-apply(a,2,list) and possibly lapply(b1,unlist) if you want exactly the output equal to "list(a[, 1], a[, 2])" best, vito Il 18/01/2011 13.53, Feng Li ha scritto:> Dear R, > > Is there an efficient way to make a list that each element is from the > corresponding column of a matrix. For example, if I have a matrix "a" > >> a<- matrix(1:10, 5, 2) >> a > [,1] [,2] > [1,] 1 6 > [2,] 2 7 > [3,] 3 8 > [4,] 4 9 > [5,] 5 10 > > I would like to have a list "b" like this > >> b<- list(a[, 1], a[, 2]) >> b > [[1]] > [1] 1 2 3 4 5 > > [[2]] > [1] 6 7 8 9 10 > > > Thanks in advance! > > > Feng >-- ===================================Vito M.R. Muggeo Dip.to Sc Statist e Matem `Vianelli' Universit? di Palermo viale delle Scienze, edificio 13 90128 Palermo - ITALY tel: 091 23895240 fax: 091 485726/485612 http://dssm.unipa.it/vmuggeo