I have found a correlation matrix for my data using the following code:
file_20 <- read.csv("C:/Desktop/ex.csv")
file_20
cor (file_20[,1:19], file_20[1:19], method ="spearman")
But now how can i rank my correlation matrix? Is there any suggestion
NOTE: I have 19 columns and 20 rows (including the header) and my aim is to rank the columns according to spearman rank correlation.
I tried doing
cor (rank(file_20[,1:19]), rank(file_20[1:19]), method ="spearman")
It showed output as
[1] 1
Is there any option to get all my columns of the table ranked?
Please help!
-- use the kendall
method argument for cor() instead:
df <- data.frame(x=c(1,2,3,4,5), y=c(5,4,3,2,1))
cor(df, method="kendall")
x y
x 1 -1
y -1 1
R>
From help(cor):
For cor()
, if method is "kendall"
or "spearman
", Kendall's tau
or Spearman's rho statistic
is used to estimate a rank-based measure of association. These are more robust and have been recommended if the data do not necessarily come from a bivariate normal distribution. For cov(
), a non-Pearson method is unusual but available for the sake of completeness. Note that "spearman" basically computes cor(R(x), R(y)) (or cov(.,.))
where R(u) := rank(u, na.last="keep")
. In the case of missing values, the ranks are calculated depending on the value of use, either based on complete observations, or based on pairwise completeness with reranking for each pair.