Hello, I have a list of vectors (all the same length). How to convert the list to a matrix? Each vector should be a column. I tried this: l <- list(c(1,2,3),c(1,2,3),c(1,2,3)) mat <- matrix( unlist(l), nrow=length(l) ) But I think this is not very efficient. Is there a better solution? Thanks Markus -- Dipl.-Tech. Math. Markus Schmidberger Ludwig-Maximilians-Universit?t M?nchen IBE - Institut f?r medizinische Informationsverarbeitung, Biometrie und Epidemiologie Marchioninistr. 15, D-81377 Muenchen URL: http://ibe.web.med.uni-muenchen.de Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de
you can use do.call(), e.g., do.call(cbind, l) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting Markus Schmidberger <schmidb at ibe.med.uni-muenchen.de>:> Hello, > > I have a list of vectors (all the same length). How to convert the list > to a matrix? Each vector should be a column. > I tried this: > > l <- list(c(1,2,3),c(1,2,3),c(1,2,3)) > mat <- matrix( unlist(l), nrow=length(l) ) > > But I think this is not very efficient. Is there a better solution? > > Thanks > Markus > > -- > Dipl.-Tech. Math. Markus Schmidberger > > Ludwig-Maximilians-Universit?t M?nchen > IBE - Institut f?r medizinische Informationsverarbeitung, > Biometrie und Epidemiologie > Marchioninistr. 15, D-81377 Muenchen > URL: http://ibe.web.med.uni-muenchen.de > Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de > > ______________________________________________ > 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. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Thanks, I have the case that there is a NA in the list. This should not be a column. But na.omit(l) does not work for lists. How to remove NAs from a list? l <- list(c(1,2,3),NA,c(1,2,3)) mat <- do.call(cbind, l) Best Markus Dimitris Rizopoulos schrieb:> you can use do.call(), e.g., > > do.call(cbind, l) > > > I hope it helps. > > Best, > Dimitris > > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/(0)16/336899 > Fax: +32/(0)16/337015 > Web: http://med.kuleuven.be/biostat/ > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > Quoting Markus Schmidberger <schmidb at ibe.med.uni-muenchen.de>: > >> Hello, >> >> I have a list of vectors (all the same length). How to convert the list >> to a matrix? Each vector should be a column. >> I tried this: >> >> l <- list(c(1,2,3),c(1,2,3),c(1,2,3)) >> mat <- matrix( unlist(l), nrow=length(l) ) >> >> But I think this is not very efficient. Is there a better solution? >> >> Thanks >> Markus >> >> -- >> Dipl.-Tech. Math. Markus Schmidberger >> >> Ludwig-Maximilians-Universit?t M?nchen >> IBE - Institut f?r medizinische Informationsverarbeitung, >> Biometrie und Epidemiologie >> Marchioninistr. 15, D-81377 Muenchen >> URL: http://ibe.web.med.uni-muenchen.de >> Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de >> >> ______________________________________________ >> 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. >> >> > > > > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm >-- Dipl.-Tech. Math. Markus Schmidberger Ludwig-Maximilians-Universit?t M?nchen IBE - Institut f?r medizinische Informationsverarbeitung, Biometrie und Epidemiologie Marchioninistr. 15, D-81377 Muenchen URL: http://ibe.web.med.uni-muenchen.de Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de Tel: +49 (089) 7095 - 4599
Hi r-help-bounces at r-project.org napsal dne 02.11.2007 12:00:09:> Thanks, > > I have the case that there is a NA in the list. This should not be acolumn.> But na.omit(l) does not work for lists. How to remove NAs from a list? > > l <- list(c(1,2,3),NA,c(1,2,3)) > mat <- do.call(cbind, l)If number of NA is not big you can use mat[,which(!is.na(colSums(mat)))] to select non NA columns. Regards Petr> > Best > Markus > > > Dimitris Rizopoulos schrieb: > > you can use do.call(), e.g., > > > > do.call(cbind, l) > > > > > > I hope it helps. > > > > Best, > > Dimitris > > > > > > ---- > > Dimitris Rizopoulos > > Ph.D. Student > > Biostatistical Centre > > School of Public Health > > Catholic University of Leuven > > > > Address: Kapucijnenvoer 35, Leuven, Belgium > > Tel: +32/(0)16/336899 > > Fax: +32/(0)16/337015 > > Web: http://med.kuleuven.be/biostat/ > > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > > > > Quoting Markus Schmidberger <schmidb at ibe.med.uni-muenchen.de>: > > > >> Hello, > >> > >> I have a list of vectors (all the same length). How to convert thelist> >> to a matrix? Each vector should be a column. > >> I tried this: > >> > >> l <- list(c(1,2,3),c(1,2,3),c(1,2,3)) > >> mat <- matrix( unlist(l), nrow=length(l) ) > >> > >> But I think this is not very efficient. Is there a better solution? > >> > >> Thanks > >> Markus > >> > >> -- > >> Dipl.-Tech. Math. Markus Schmidberger > >> > >> Ludwig-Maximilians-Universit?t M?nchen > >> IBE - Institut f?r medizinische Informationsverarbeitung, > >> Biometrie und Epidemiologie > >> Marchioninistr. 15, D-81377 Muenchen > >> URL: http://ibe.web.med.uni-muenchen.de > >> Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de > >> > >> ______________________________________________ > >> 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. > >> > >> > > > > > > > > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm > > > > > -- > Dipl.-Tech. Math. Markus Schmidberger > > Ludwig-Maximilians-Universit?t M?nchen > IBE - Institut f?r medizinische Informationsverarbeitung, > Biometrie und Epidemiologie > Marchioninistr. 15, D-81377 Muenchen > URL: http://ibe.web.med.uni-muenchen.de > Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de > Tel: +49 (089) 7095 - 4599 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.