Calculate the Spearman coefficient

I would like to know if I'm doing the Spearman coefficient right for the database below. I ask this because the values were only 1 and -1, so I don't know if it's quite right.

df1<-structure(list(M1 = c(0.0819922360772441, 1, 0.997906804996597, 
0.996947395338312, 0.996544592731174, 0.996544592731174, 0.996504698345956, 
0.996466418305031, 0.996466418305031, 0.996466418305031, 0.996466418305031, 
0.996073887361272, 0.996013593033973, 0.996156596023304, 0.996156596023304, 
0.996156596023304, 0.996143129198409, 0.996072907981415, 0.996072907981415, 
0.99597332693421, 0.99597332693421, 0.99597332693421, 0.99597332693421, 
0.99597332693421, 0.99597332693421, 0.99597332693421, 0.995962096896553, 
0.995943900534613, 0.995943900534613), M2 = c(0.687752686793705, 
0.184328244142717, 0.227707262626215, 0.269521283427417, 0.30115892512968, 
0.30115892512968, 0.30531040016779, 0.309552856382155, 0.309552856382155, 
0.309552856382155, 0.309552856382155, 0.384301713846187, 0.41089425611557, 
0.360067875075062, 0.360067875075062, 0.360067875075062, 0.363442987553713, 
0.384652173426076, 0.384652173426076, 0.440130213617075, 0.440130213617075, 
0.440130213617075, 0.440130213617075, 0.440130213617075, 0.440130213617075, 
0.440130213617075, 0.452925925081707, 0.507338518968011, 0.507338518968011
), M3 = c(0.832550756796408, 0, 0.0623422939077367, 0.0994025225880894, 
0.119244082570837, 0.119244082570837, 0.12148059799612, 0.123691601200231, 
0.123691601200231, 0.123691601200231, 0.123691601200231, 0.153356141263302, 
0.160910166815577, 0.145325231545442, 0.145325231545442, 0.145325231545442, 
0.146519242848501, 0.153463626026167, 0.153463626026167, 0.168008878164586, 
0.168008878164586, 0.168008878164586, 0.168008878164586, 0.168008878164586, 
0.168008878164586, 0.168008878164586, 0.170786494229362, 0.180835139451362, 
0.180835139451362), M4 = c(0, 1, 0.860063548075109, 0.769005783475762, 
0.717332364472959, 0.717332364472959, 0.711366211548447, 0.705438648123933, 
0.705438648123933, 0.705438648123933, 0.705438648123933, 0.622884341715579, 
0.600890222717323, 0.645815756326619, 0.645815756326619, 0.645815756326619, 
0.64243496465979, 0.622574324613684, 0.622574324613684, 0.57982897648021, 
0.57982897648021, 0.57982897648021, 0.57982897648021, 0.57982897648021, 
0.57982897648021, 0.57982897648021, 0.57148060433324, 0.540751796949386, 
0.540751796949386)), row.names = c(NA, 29L), class = "data.frame")

rho<-cor(df1, method = "spearman")

> rho
   M1 M2 M3 M4
M1  1 -1 -1  1
M2 -1  1  1 -1
M3 -1  1  1 -1
M4  1 -1 -1  1

I doubt we could judge if your approach is right for your data/task without knowing about your data/task... we lack context.
There's nothing obviously wrong with your code.

Yes, it is correct. If you plot the various columns against each other, you will see that the relationships are monotonic; when one column goes up, the other column either always goes up or always goes down.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.