I have some data that looks like below:
data <- structure(list(Gene1 = c(8.030835566, 7.523710852, 8.132012083,
6.351834008, 6.325281674, 6.810264692), Gene2 = c(8.755819384,
8.101372382, 8.691221404, 7.963358311, 7.003639076, 8.860927111
), Gene3 = c(6.504333248, 8.832470479, 10.7661583, 12.48262164,
6.003163216, 6.810264692), Gene4 = c(7.773754938, 6.545394663,
7.499168201, 5.587808389, 5.587808389, 5.587808389), Gene5 = c(9.782169709,
9.208945473, 10.07340718, 8.452540072, 6.810926028, 8.757832761
), Gene6 = c(6.808970669, 6.545394663, 7.206623134, 6.848704398,
5.587808389, 8.090400794), Gene7 = c(10.32869976, 10.09035118,
9.541872802, 10.57521096, 7.910751686, 10.02532033), Gene8 = c(7.274379599,
6.545394663, 7.499168201, 7.963358311, 6.003163216, 7.909825918
), Gene9 = c(8.605820177, 8.101372382, 8.293403668, 10.21655691,
6.003163216, 6.324818701), Gene10 = c(12.30601467, 12.27069432,
12.47496174, 12.23379266, 10.04845125, 12.4664811), X.1 = c(NA,
NA, NA, NA, NA, NA)), class = "data.frame", row.names = c("S1",
"S2", "S3", "S4", "S5", "S6"))
The above data is just an example. Original data has more than 1000 rows. I actually want to do correlation only with specific variables like below.
Gene1 * Gene7
Gene2 * Gene8
Gene3 * Gene10
Gene5 * Gene6
Gene4 * Gene9
I'm interested in only above correlations. I don't want correlation between Gene1 and all other Genes. I can do correlation on all variables and filter out the interested ones, but I have more than 1000 so, it is hard for me to filter.
So, is there a way to take only specific interested GeneX * GeneY
and do correlation?
I'm using Hmisc
library and rcorr
function for correlation.
Try this example:
# make combinations
x <- read.table(text = "g1 g2
Gene1 Gene7
Gene2 Gene8
Gene3 Gene10
Gene5 Gene6
Gene4 Gene9", header = TRUE)
# then get correlation
cbind(x, Corr = apply(x, 1, function(i) cor(data[, i[ 1 ]], data[, i[ 2 ]])))
# g1 g2 Corr
# 1 Gene1 Gene7 0.3064922
# 2 Gene2 Gene8 0.7185533
# 3 Gene3 Gene10 0.4521448
# 4 Gene5 Gene6 0.5747986
# 5 Gene4 Gene9 0.3009623
To get p-values, run cor.test
:
cbind(x, t(apply(x, 1, function(i){
res <- cor.test(data[, i[ 1 ]], data[, i[ 2 ]])
c(cor = res$estimate[[ "cor" ]], p = res$p.value)
})))
# g1 g2 cor p
# 1 Gene1 Gene7 0.3064922 0.5546572
# 2 Gene2 Gene8 0.7185533 0.1076713
# 3 Gene3 Gene10 0.4521448 0.3680000
# 4 Gene5 Gene6 0.5747986 0.2327570
# 5 Gene4 Gene9 0.3009623 0.5621868