Reputation: 77
I want to calculate correlation matrix P which each P[i,j] is correlation coefficient of row i and col j in matrix Data. for example
Data <- matrix(rnorm(500),50,10)
P <- matrix(0,50,50)
for (i in 1:50)
for(j in 1:50)
P[i,j] <- cor(Data[i,],Data[j,])
But how can I use apply or something like this command to calculate such correlations.
Upvotes: 1
Views: 1513
Reputation: 47642
You can just use cor()
on a data frame or matrix to obtain a correlation matrix of correlations between all pairs of columns:
cor(t(Data))
From your question and code it is not clear if you want correlations for all pairs of rows or correlations between rows and columns, but since the matrix is not square I assumed the first.
Upvotes: 6