You may want something like this. Convert countTable
to a matrix
and drop down to one loop, using i
and i-1
for the loop indices. And there is no need to create an empty data frame beforehand.
> countTable <- as.matrix(countTable)
> rn <- rownames(countTable)
> dat <- do.call(rbind, lapply(2:nrow(countTable), function(i){
corCoeff <- cor(countTable[i-1,] , countTable[i,],
method = "spearman", use = "complete.obs")
data.frame(nodeA = rn[i-1], nodeB = rn[i], edge = corCoeff)
}))
> dat
# nodeA nodeB edge
# 1 ENST00000000233 ENST00000000412 0.1538968
# 2 ENST00000000412 ENST00000000442 0.6668859
# 3 ENST00000000442 ENST00000001008 0.7000000
# 4 ENST00000001008 ENST00000001567 -0.8000000
# 5 ENST00000001567 ENST00000002125 -0.5642881