Make use of the plyr and reshape2 package (both on CRAN): library(plyr) d<-adply(ArrayDiseaseCor, 1:2) # adply calls function identity by default d<-melt(d) d<-subset(d,value>.5) head(d)
You will have to rename columns, or adjust arguments in melt/adply. Note: use set.seed before sampling for reproducible code! Best, S. On 11 February 2015 at 17:11, Karim Mezhoud <kmezh...@gmail.com> wrote: > Dear All, > I am facing the task to extract data from array to table. here an example > of array. > > ##Get A list of Matrices with unequal rows > > Disease <- NULL > Diseases <- NULL > ListMatByGene <- NULL > for(i in 1:3){ > > Disease[[i]] <-matrix(sample(-30:30,25+(5* > i)),5+i) > rownames(Disease[[i]]) <- paste0("Sample",1:(5+i)) > colnames(Disease[[i]]) <- paste0("Gene",1:5) > > D <- paste0("Disease",i) > Diseases[[D]] <- Disease[[i]] > } > > > ## get the same Column from all matrices > getColumn <- function(x, colNum, len = nrow(x)){ > y <- x[,colNum] > length(y) <- len > y > } > > ## get Matrices by the same columns of the list of matrices > getMatrices <- function(colNums, dataList = x){ > # the number of rows required > n <- max(sapply(dataList, nrow)) > lapply(colNums, function(x, dat, n) { # iterate along requested columns > do.call(cbind, lapply(dat, getColumn,x, len=n)) # iterate along > input data list > }, dataList, n) > } > > ## Rotate the list of matrices by 90° > G <- paste0("Gene",1:5) > ListMatByGene[G] <- getMatrices(c(1:ncol(Diseases[[1]])),dataList=Diseases) > > ## get Disease correlation by gene > DiseaseCorrelation <- lapply(ListMatByGene,function(x) cor(x,use="na", > method="spearman")) > > ##convert the list of Matrices to array > ArrayDiseaseCor <- array(unlist(DiseaseCorrelation), dim = > c(nrow(DiseaseCorrelation[[1]]), ncol(DiseaseCorrelation[[1]]), > length(DiseaseCorrelation))) > dimnames(ArrayDiseaseCor) <- list(names(Diseases), names(Diseases), > colnames(Diseases[[1]])) > > ## Select only correlation bigger than 0.5 from the array > FilterDiseaseCor <- apply(ArrayDiseaseCor,MARGIN=c(1,2) ,function(x) > x[abs(x)>0.5]) > > ## Final result > FilterDiseaseCor > > Disease1 Disease2 Disease3 > Disease1 Numeric,5 Numeric,2 -0.9428571 > Disease2 Numeric,2 Numeric,5 Numeric,2 > Disease3 -0.9428571 Numeric,2 Numeric,5 > > > Question is: > How can get a table as: > > D1 D2 Cor Gene > Disease1 Disease2 -0.94 Gene2 > Disease1 Disease2 0.78 Gene4 > Disease3 Disease2 0.5 Gene5 > ... > > and > Disease1 Disease2 Disease3 > Disease1 5 1 0 > Disease2 1 5 3 > Disease3 0 3 5 > > Or in general, How can I extract data from Array to Table? > > Thanks > Karim > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.