How do I convert a list to a matrix? --8<---------------cut here---------------start------------->8--- list(c(50000, 101), c(1e+05, 46), c(150000, 31), c(2e+05, 17), c(250000, 19), c(3e+05, 11), c(350000, 12), c(4e+05, 25), c(450000, 19), c(5e+05, 16)) as.matrix(a) [,1] [1,] Numeric,2 [2,] Numeric,2 [3,] Numeric,2 [4,] Numeric,2 [5,] Numeric,2 [6,] Numeric,2 [7,] Numeric,2 [8,] Numeric,2 [9,] Numeric,2 --8<---------------cut here---------------end--------------->8--- thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ http://memri.org Rhinoceros has poor vision, but, due to his size, it's not his problem.
On Tue, Dec 4, 2012 at 8:09 PM, Sam Steingold <sds at gnu.org> wrote:> How do I convert a list to a matrix? > > --8<---------------cut here---------------start------------->8--- > list(c(50000, 101), c(1e+05, 46), c(150000, 31), c(2e+05, 17), > c(250000, 19), c(3e+05, 11), c(350000, 12), c(4e+05, 25), > c(450000, 19), c(5e+05, 16))do.call(rbind, LIST) or do.call(cbind, LIST) depending on your desired direction. The do.call syntax takes a function name and uses a list as arguments to that function, returning the result. Super useful for these situations where you collect things and something like cbind(x[[1]],x[[2]], x[[3]]...) isn't feasible or possible. MW
Try: matrix(unlist(a), ncol=2, byrow=T) --Mark Lamias ________________________________ From: Sam Steingold <sds@gnu.org> To: r-help@r-project.org Sent: Tuesday, December 4, 2012 3:09 PM Subject: [R] list to matrix? How do I convert a list to a matrix? --8<---------------cut here---------------start------------->8--- list(c(50000, 101), c(1e+05, 46), c(150000, 31), c(2e+05, 17), c(250000, 19), c(3e+05, 11), c(350000, 12), c(4e+05, 25), c(450000, 19), c(5e+05, 16)) as.matrix(a) [,1] [1,] Numeric,2 [2,] Numeric,2 [3,] Numeric,2 [4,] Numeric,2 [5,] Numeric,2 [6,] Numeric,2 [7,] Numeric,2 [8,] Numeric,2 [9,] Numeric,2 --8<---------------cut here---------------end--------------->8--- thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ http://memri.org Rhinoceros has poor vision, but, due to his size, it's not his problem. ______________________________________________ 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. [[alternative HTML version deleted]]
Hi, Try this: list1<-list(c(50000, 101), c(1e+05, 46), c(150000, 31), c(2e+05, 17), ? ? c(250000, 19), c(3e+05, 11), c(350000, 12), c(4e+05, 25), ? ? c(450000, 19), c(5e+05, 16)) res<-t(sapply(list1,function(x) x)) res #??????? [,1] [,2] ?#[1,]? 50000? 101 ?#[2,] 100000?? 46 ?#[3,] 150000?? 31 ?#[4,] 200000?? 17 ?#[5,] 250000?? 19 ?#[6,] 300000?? 11 ?#[7,] 350000?? 12 ?#[8,] 400000?? 25 ?#[9,] 450000?? 19 #[10,] 500000?? 16 ? is.matrix(res) #[1] TRUE #or res1<-sapply(list1,function(x) x) ?is.matrix(res1) #[1] TRUE A.K. ----- Original Message ----- From: Sam Steingold <sds at gnu.org> To: r-help at r-project.org Cc: Sent: Tuesday, December 4, 2012 3:09 PM Subject: [R] list to matrix? How do I convert a list to a matrix? --8<---------------cut here---------------start------------->8--- list(c(50000, 101), c(1e+05, 46), c(150000, 31), c(2e+05, 17), ? ? c(250000, 19), c(3e+05, 11), c(350000, 12), c(4e+05, 25), ? ? c(450000, 19), c(5e+05, 16)) as.matrix(a) ? ? ? [,1]? ? [1,] Numeric,2 [2,] Numeric,2 [3,] Numeric,2 [4,] Numeric,2 [5,] Numeric,2 [6,] Numeric,2 [7,] Numeric,2 [8,] Numeric,2 [9,] Numeric,2 --8<---------------cut here---------------end--------------->8--- thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ http://memri.org Rhinoceros has poor vision, but, due to his size, it's not his problem. ______________________________________________ R-help at 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.