[BioC] Converting list to a matrix

Day, Roger S day01 at pitt.edu
Mon Apr 18 23:15:58 CEST 2011


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



More information about the Bioconductor mailing list