1
votes

I used the following code to make a table of Spearman Rho correlations for a CSV file with 2+ columns in R:

> myDataset <- read.csv(file.choose())
> attach(myDataset)
> spearmanRhoTestData <- cor(myDataset, use="complete.obs",method="spearman")

However, in my table (spearmanRhoTestData), the correlation between any two variables will show up twice (like the following):

    Var1 Var2 Var3 Var4
Var1  1   0.5  0.7 0.9
Var2  0.5  1   0.3 0.6  
Var3  0.7  0.3  1  0.2
Var4  0.9  0.6  0.2 1

Is there any way I could write code in R to get rid of the correlation values (ex: between var1 and var2) from showing up twice in the entire table??

1
Could you provide a sample of your original data? ! Use dput(head(my_data,10)) for instance. - NelsonGon
Not sure exactly how you want your output to look like, but you can try if this is OK for you: spearmanRhoTestData[upper.tri(spearmanRhoTestData)] = NA; spearmanRhoTestData .This will replace all the upper triangle part of the matrix with NAs. - AntoniosK
Thanks! spearmanRhoTestData[upper.tri(spearmanRhoTestData)] = "" worked for me - Audrey
Keep in mind that the = "" will update your values from numeric to character, in order to match the "" value. You might face some difficulties if you want to further process your data. If it's just for viewing / presentation purposes it's OK. :) - AntoniosK

1 Answers

0
votes

The simplest approach assuming that you want to keep a correlation matrix format is

# set upper triangle values to NA
spearmanRhoTestData[upper.tri(spearmanRhoTestData)] = NA

# visualise updated matrix
spearmanRhoTestData

Here's an alternative approach, using the corrr package, which will give you a reshaped correlation dataframe, without duplicates:

library(corrr)

# get correaltion matrix
tbl = correlate(mtcars)

# set upper triangle values to NA
tbl[upper.tri(tbl)] = NA

# reshape and omit NAs
stretch(tbl, na.rm = T)

# # A tibble: 55 x 3
#     x     y     r
#  <chr> <chr>  <dbl>
# 1 mpg   cyl   -0.852
# 2 mpg   disp  -0.848
# 3 mpg   hp    -0.776
# 4 mpg   drat   0.681
# 5 mpg   wt    -0.868
# 6 mpg   qsec   0.419
# 7 mpg   vs     0.664
# 8 mpg   am     0.600
# 9 mpg   gear   0.480
# 10 mpg   carb  -0.551
# # ... with 45 more rows