Search code examples
rsortingcorrelationranking

Create table showing the sorted absolute correlation of various variables with another series


I want the output show the name of each X variable, its signed correlation to another series, and its absolute correlation, sorted by descending absolute correlation.

Using the following codes I was able to calculate the correlation between a series (object res1) and the X variables (located within the data2 dataframe).

cor(data2, res1, method = c("pearson"))

The above code generated the output below that shows vertically in the console.

         [,1]
x1 0.45683210
x2 0.62858863
x3 0.08457911
x4 0.41022052

Next, using the following code I was able to rank those correlations by their absolute value using the sort() function.

abs(cor(data2, res1, method = c("pearson")))
abs1<-abs(cor(data2, res1, method = c("pearson")))
sort(abs1, decreasing = TRUE)

And, I got the following output.

[1] 0.62858863 0.45683210 0.41022052 0.08457911 

I want to generate an output that looks like a table or a dataframe.

In the first column you would have the labels of the X variable.
In the second column you would have their absolute correlation.
In the third column you would have the actual correlation.

And, this vertical tabular list would be ranked in descending order. I think I have all the info I need. I just need the codes to generate the output as specified.


Solution

  • Answer by @Jon Spring is perfect. Here is the same code in base R

    res1 <- c(0, 5, 2, 7, 1)
    data2 <- data.frame(x1 = 1:5,           # uncorrelated
                        x2 = 14:10,             # uncorrelated and wrong direction
                        x3 = c(0, 5, 1, 6, 0),  # very similar
                        x4 = c(0, 0, 2, 7, 1))  # somewhat similar
    
    correlation = cor(data2, res1, method = "pearson")
    names = rownames(correlation)
    abs_cor = abs(correlation)
    data = data.frame(X_var = names,abs_cor = abs_cor,cor = correlation)
    data[order(data$abs_cor,decreasing = TRUE),]