Converting list to a matrix
5
0
Entering edit mode
pankaj borah ▴ 120
@pankaj-borah-3804
Last seen 10.3 years ago
I have a list ( L) of 63 objects and each object has 30800 variables. >str (L) List of 63  $ objec1  : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ...  $ object2  : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... .............  $ object63  : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... I want to convert the list L to a matrix M So it should be --- >str(M) num [1:30380, 1:63]  attr(*, "dimnames")=List of 2 How do I do that ? Regards, Pankaj Barah Department of Biology, Norwegian University of Science & Technology (NTNU) Realfagbygget, N-7491 Trondheim, Norway [[alternative HTML version deleted]]
convert convert • 2.3k views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 4 days ago
United States
Hi Pankaj, On 4/12/2011 8:54 AM, pankaj borah wrote: > I have a list ( L) of 63 objects and each object has 30800 variables. This has nothing to do with Bioconductor, so you should ask on R-help. Best, Jim > >> str (L) > > List of 63 > $ objec1 : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ... > $ object2 : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... > ............. > $ object63 : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... > > > > > I want to convert the list L to a matrix M > > So it should be --- >> str(M) > > num [1:30380, 1:63] > attr(*, "dimnames")=List of 2 > > > How do I do that ? > > Regards, > > Pankaj Barah > > Department of Biology, > Norwegian University of Science& Technology (NTNU) > Realfagbygget, N-7491 Trondheim, Norway > > [[alternative HTML version deleted]] > > > > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor -- James W. MacDonald, M.S. Biostatistician Douglas Lab University of Michigan Department of Human Genetics 5912 Buhl 1241 E. Catherine St. Ann Arbor MI 48109-5618 734-615-7826 ********************************************************** Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues
ADD COMMENT
0
Entering edit mode
@sean-davis-490
Last seen 4 months ago
United States
On Tue, Apr 12, 2011 at 8:54 AM, pankaj borah <pankajborah2k3 at="" yahoo.co.in=""> wrote: > I have a list ( L) of 63 objects and each object has 30800 variables. > >>str (L) > > List of 63 > ?$ objec1? : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ... > ?$ object2? : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... > ............. > ?$ object63? : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... > > > > > I want to convert ?the list L to a matrix M > > So it should be --- >>str(M) > > num [1:30380, 1:63] > ?attr(*, "dimnames")=List of 2 > > > How do I do that ? Try: M = do.call('cbind',L) Sean
ADD COMMENT
0
Entering edit mode
Thanks Sean.. On Tue, Apr 12, 2011 at 3:10 PM, Sean Davis <sdavis2@mail.nih.gov> wrote: > On Tue, Apr 12, 2011 at 8:54 AM, pankaj borah > <pankajborah2k3@yahoo.co.in> wrote: > > I have a list ( L) of 63 objects and each object has 30800 variables. > > > >>str (L) > > > > List of 63 > > $ objec1 : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ... > > $ object2 : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... > > ............. > > $ object63 : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... > > > > > > > > > > I want to convert the list L to a matrix M > > > > So it should be --- > >>str(M) > > > > num [1:30380, 1:63] > > attr(*, "dimnames")=List of 2 > > > > > > How do I do that ? > > Try: > > M = do.call('cbind',L) > > Sean > [[alternative HTML version deleted]]
ADD REPLY
0
Entering edit mode
@carlos-j-gil-bellosta-1569
Last seen 10.3 years ago
Hello, Try this: a <- list( a = 1:4, b = 2:5 ) do.call( rbind, a ) Mind that not all your list entries seem to have 30800 variables. Some seem to be shorter. Best regards, Carlos J. Gil Bellosta http://www.datanalytics.com 2011/4/12 pankaj borah <pankajborah2k3 at="" yahoo.co.in="">: > I have a list ( L) of 63 objects and each object has 30800 variables. > >>str (L) > > List of 63 > ?$ objec1? : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ... > ?$ object2? : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... > ............. > ?$ object63? : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... > > > > > I want to convert ?the list L to a matrix M > > So it should be --- >>str(M) > > num [1:30380, 1:63] > ?attr(*, "dimnames")=List of 2 > > > How do I do that ? > > Regards, > > Pankaj Barah > > Department of Biology, > Norwegian University of Science & Technology (NTNU) > Realfagbygget, N-7491 Trondheim, Norway > > ? ? ? ?[[alternative HTML version deleted]] > > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor >
ADD COMMENT
0
Entering edit mode
Thanks Carlos, It worked. I had to transpose it. t(do.call( rbind, a )) Thanks a lot, Pankaj On Tue, Apr 12, 2011 at 3:09 PM, Carlos J. Gil Bellosta < cgb@datanalytics.com> wrote: > Hello, > > Try this: > > a <- list( a = 1:4, b = 2:5 ) > do.call( rbind, a ) > > Mind that not all your list entries seem to have 30800 variables. Some > seem to be shorter. > > Best regards, > > Carlos J. Gil Bellosta > http://www.datanalytics.com > > > 2011/4/12 pankaj borah <pankajborah2k3@yahoo.co.in>: > > I have a list ( L) of 63 objects and each object has 30800 variables. > > > >>str (L) > > > > List of 63 > > $ objec1 : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ... > > $ object2 : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... > > ............. > > $ object63 : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... > > > > > > > > > > I want to convert the list L to a matrix M > > > > So it should be --- > >>str(M) > > > > num [1:30380, 1:63] > > attr(*, "dimnames")=List of 2 > > > > > > How do I do that ? > > > > Regards, > > > > Pankaj Barah > > > > Department of Biology, > > Norwegian University of Science & Technology (NTNU) > > Realfagbygget, N-7491 Trondheim, Norway > > > > [[alternative HTML version deleted]] > > > > > > _______________________________________________ > > Bioconductor mailing list > > Bioconductor@r-project.org > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > > > [[alternative HTML version deleted]]
ADD REPLY
0
Entering edit mode
@carlos-j-gil-bellosta-1569
Last seen 10.3 years ago
If you want it transpose, use cbind instead of rbind. Best regards, Carlos J. Gil Bellosta http://www.datanalytics.com 2011/4/12 Pankaj Barah <pankajborah at="" gmail.com="">: > Thanks Carlos, > > It worked. I had to transpose it. t(do.call( rbind, a )) > > Thanks a lot, > > Pankaj > > On Tue, Apr 12, 2011 at 3:09 PM, Carlos J. Gil Bellosta > <cgb at="" datanalytics.com=""> wrote: >> >> Hello, >> >> Try this: >> >> a <- list( a = 1:4, b = 2:5 ) >> do.call( rbind, a ) >> >> Mind that not all your list entries seem to have 30800 variables. Some >> seem to be shorter. >> >> Best regards, >> >> Carlos J. Gil Bellosta >> http://www.datanalytics.com >> >> >> 2011/4/12 pankaj borah <pankajborah2k3 at="" yahoo.co.in="">: >> > I have a list ( L) of 63 objects and each object has 30800 variables. >> > >> >>str (L) >> > >> > List of 63 >> > ?$ objec1? : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ... >> > ?$ object2? : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... >> > ............. >> > ?$ object63? : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... >> > >> > >> > >> > >> > I want to convert ?the list L to a matrix M >> > >> > So it should be --- >> >>str(M) >> > >> > num [1:30380, 1:63] >> > ?attr(*, "dimnames")=List of 2 >> > >> > >> > How do I do that ? >> > >> > Regards, >> > >> > Pankaj Barah >> > >> > Department of Biology, >> > Norwegian University of Science & Technology (NTNU) >> > Realfagbygget, N-7491 Trondheim, Norway >> > >> > ? ? ? ?[[alternative HTML version deleted]] >> > >> > >> > _______________________________________________ >> > Bioconductor mailing list >> > Bioconductor at r-project.org >> > https://stat.ethz.ch/mailman/listinfo/bioconductor >> > Search the archives: >> > http://news.gmane.org/gmane.science.biology.informatics.conductor >> > > > > > >
ADD COMMENT
0
Entering edit mode
@arunava-chakravartty-3910
Last seen 10.3 years ago
try >do.call(rbind,L) On Tue, Apr 12, 2011 at 7:54 AM, pankaj borah <pankajborah2k3@yahoo.co.in>wrote: > I have a list ( L) of 63 objects and each object has 30800 variables. > > >str (L) > > List of 63 > $ objec1 : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ... > $ object2 : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... > ............. > $ object63 : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... > > > > > I want to convert the list L to a matrix M > > So it should be --- > >str(M) > > num [1:30380, 1:63] > attr(*, "dimnames")=List of 2 > > > How do I do that ? > > Regards, > > Pankaj Barah > > Department of Biology, > Norwegian University of Science & Technology (NTNU) > Realfagbygget, N-7491 Trondheim, Norway > > [[alternative HTML version deleted]] > > > _______________________________________________ > Bioconductor mailing list > Bioconductor@r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > -- Arunava Chakravartty 1431 Johnson Drive, # 1011 Buffalo Grove, IL 60089 [[alternative HTML version deleted]]
ADD COMMENT
0
Entering edit mode
More generally, here's a utility that will bind arrays after checking compatibility. It's passed some testing; may not be completely bullet-proof. Vectors are bound in columns, not rows. pancake = function(arrays) { ### Purpose: given a list of arrays, all of the same shape, construct an array of one greater dimension by piling the list elements together. Set the dimnames appropriately. if(wereVectors <- all(sapply(arrays, is.vector))) arrays <- lapply(arrays, matrix) if(length(common.dim <- unique(lapply(arrays, dim))) != 1L) stop("Elements should be arrays of the same shape.") if(length(unique(lapply(arrays, mode))) != 1L) stop("Elements should be arrays of the same mode.") theDim <- c(common.dim[[1]], length(arrays)) ### dim of new array. answer <- array(unlist(arrays), dim=theDim) ### Create dimnames for answer. eachNames <- dimnames(arrays[[1]]) ### Uses only the 1st array. Assumes all components have the same dimnames. ### <- TODO: maybe check assumption, warn or maybe throw error. if(is.null(eachNames)) eachNames <- lapply(dim(arrays[[1]]), seq) extraDimName <- names(arrays) if(is.null(extraDimName)) extraDimName <- seq(length(arrays)) answerNames <- c(eachNames, list(extraDimName)) dimnames(answer) <- answerNames if(wereVectors) answer <- answer[ , 1, , drop=T] return(answer) } Roger Day University of Pittsburgh Departments of Biomedical Informatics and Biostatistics University of Pittsburgh Cancer Institute University of Pittsburgh Molecular Medicine Institute ----------------------------------------------------- Room 310, Suite 301 Cancer Pavilion (CNPAV) 5150 Centre Ave. Pittsburgh, PA 15232 e-mail: day01 at pitt.edu cell phone 412-609-3918 assistant: Lucy Cafeo: (412) 623-2952 ----------------------------------------------------- On Apr 14, 2011, at 12:11 PM, Arunava Chakravartty wrote: > try >> do.call(rbind,L) > > On Tue, Apr 12, 2011 at 7:54 AM, pankaj borah <pankajborah2k3 at="" yahoo.co.in="">wrote: > >> I have a list ( L) of 63 objects and each object has 30800 variables. >> >>> str (L) >> >> List of 63 >> $ objec1 : num [1:30300] 0.927 0.813 0.397 0.703 0.651 ... >> $ object2 : num [1:30300] 0.636 0.447 0.738 0.648 0.62 ... >> ............. >> $ object63 : num [1:30380] 0.1123 0.2119 0.4078 0.0383 0.0641 .... >> >> >> >> >> I want to convert the list L to a matrix M >> >> So it should be --- >>> str(M) >> >> num [1:30380, 1:63] >> attr(*, "dimnames")=List of 2 >> >> >> How do I do that ? >> >> Regards, >> >> Pankaj Barah >> >> Department of Biology, >> Norwegian University of Science & Technology (NTNU) >> Realfagbygget, N-7491 Trondheim, Norway >> >> [[alternative HTML version deleted]] >> >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at r-project.org >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor >> > > > > -- > Arunava Chakravartty > 1431 Johnson Drive, # 1011 > Buffalo Grove, IL 60089 > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
ADD REPLY

Login before adding your answer.

Traffic: 398 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6