Re: [R] Extract data from Array to Table

2015-02-12 Thread Sven E. Templer
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
 Disease1Disease2  -0.94Gene2
 Disease1Disease2   0.78Gene4
 Disease3Disease2   0.5  Gene5
 ...

 and
  Disease1   Disease2  Disease3
 Disease1510
 Disease21 53
 Disease30  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.

Re: [R] Extract data from Array to Table

2015-02-12 Thread Sven E. Templer
see inline

On 12 February 2015 at 09:10, Sven E. Templer sven.temp...@gmail.com wrote:
 Make use of the plyr and reshape2 package (both on CRAN):


I forgot:

library(reshape2)

 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
 Disease1Disease2  -0.94Gene2
 Disease1Disease2   0.78Gene4
 Disease3Disease2   0.5  Gene5
 ...

 and
  Disease1   Disease2  Disease3
 Disease1510
 Disease21 53
 Disease30  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.

[R] Extract data from Array to Table

2015-02-11 Thread Karim Mezhoud
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
Disease1Disease2  -0.94Gene2
Disease1Disease2   0.78Gene4
Disease3Disease2   0.5  Gene5
...

and
 Disease1   Disease2  Disease3
Disease1510
Disease21 53
Disease30  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.