MeeshCompBio
MeeshCompBio

Reputation: 109

R cor across rows and columns

Hello I currently have a matrix that has genes as row.name and Tissue Type as colnames. If I:

> cor(matrix)
> str(DataCor2)
 num [1:39453, 1:19] 0.502 7.974 33.462 0 14.543 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:39453] "AC205376.4_FG003" "GRMZM2G024563" ...
  ..$ : chr [1:19] "V18_Meotic_TasselR1" "V18_Meotic_TasselR2" ...

I get a correlation between tissue types. What I want to do is get a correlation of a subsets of genes across each tissue type for a heat map, I tried:

cor(v, t(v))

But I get an error saying incompatible dimensions. Here is part of the data and I want to see a correlation of expression levels between genes across tissue types. The heat map would look similar to the matrix with genes on the Y axis and tissue on the X axis. Hopefully it makes more sense.

enter image description here

Upvotes: 0

Views: 896

Answers (1)

akrun
akrun

Reputation: 886938

Suppose you want to subset the genes based on the prefix and do the cor on each subset

 res <- lapply(split(1:nrow(mat1), gsub("_.*", "", rownames(mat1))),
                               function(i) cor(mat1[i,]))

data

set.seed(29)
mat1 <- matrix(sample(0:80, 15*3, replace=TRUE), ncol=3,
   dimnames=list(paste(rep(c('V18', 'V19'), c(8,7)), LETTERS[1:15],
   sep="_"), paste0("AC", 1:3))) 

Upvotes: 2

Related Questions