将相关表显示为降序列表

时间:2021-10-22 20:16:27

When running cor() on a times series with a lot of variables, I get a table back that has a row and column for each variable, showing the correlation between them.

当在一个包含很多变量的times系列上运行cor()时,我得到一个表,其中每个变量都有行和列,显示它们之间的相关性。

How can I view this table as a list from most correlated to least correlated (eliminating all NA results and results that map back to themselves (i.e. the correlation of A to A)). I would also like to count inverse (negative) results as absolute values, but still show them as negative.

如何将该表视为一个从最相关到最不相关的列表(消除所有映射回自身的NA结果和结果(即a与a的相关性)。我也想把逆(负)结果算作绝对值,但仍然把它们表示为负值。

So the desired output would be something like:

所以期望的输出是这样的:

A,B,0.98
A,C,0.9
C,R,-0.8
T,Z,0.5

3 个解决方案

#1


13  

Here's one of many ways I could think to do this. I used the reshape package because the melt() syntax was easy for me to remember, but the melt() command could pretty easily be done with base R commands:

这是我能想到的方法之一。我使用重塑包是因为melt()语法对我来说很容易记住,但是melt()命令可以很容易地使用基本的R命令完成:

require(reshape)
## set up dummy data
a <- rnorm(100)
b <- a + (rnorm(100, 0, 2))
c <- a + b + (rnorm(100)/10)
df <- data.frame(a, b, c)
c <- cor(df)
## c is the correlations matrix

## keep only the lower triangle by 
## filling upper with NA
c[upper.tri(c, diag=TRUE)] <- NA

m <- melt(c)

## sort by descending absolute correlation
m <- m[order(- abs(m$value)), ]

## omit the NA values
dfOut <- na.omit(m)

## if you really want a list and not a data.frame
listOut <- split(dfOut, 1:nrow(dfOut))

#2


10  

Using base R (where cors is the correlation matrix):

使用基数R (cors为相关矩阵):

up <- upper.tri(cors)
out <- data.frame(which(up, arr.ind=TRUE), cor=cors[up])
out <- out[!is.na(out$cor),]
out[order(abs(out$cor), decreasing=TRUE),]

#3


2  

Replace ... with your correlation call.

取代……和你的联系电话。

library(reshape)
x <- subset(melt(cor(...)), value != 1 | value != NA)
x <- x[with(x, order(-abs(x$value))),]

If you're getting a lot of NA in your correlations, perhaps try using the use="complete.obs" argument in your correlation call.

如果你在你的相关性中得到了很多NA,也许可以尝试使用use=“complete”。在相关调用中的obs参数。

#1


13  

Here's one of many ways I could think to do this. I used the reshape package because the melt() syntax was easy for me to remember, but the melt() command could pretty easily be done with base R commands:

这是我能想到的方法之一。我使用重塑包是因为melt()语法对我来说很容易记住,但是melt()命令可以很容易地使用基本的R命令完成:

require(reshape)
## set up dummy data
a <- rnorm(100)
b <- a + (rnorm(100, 0, 2))
c <- a + b + (rnorm(100)/10)
df <- data.frame(a, b, c)
c <- cor(df)
## c is the correlations matrix

## keep only the lower triangle by 
## filling upper with NA
c[upper.tri(c, diag=TRUE)] <- NA

m <- melt(c)

## sort by descending absolute correlation
m <- m[order(- abs(m$value)), ]

## omit the NA values
dfOut <- na.omit(m)

## if you really want a list and not a data.frame
listOut <- split(dfOut, 1:nrow(dfOut))

#2


10  

Using base R (where cors is the correlation matrix):

使用基数R (cors为相关矩阵):

up <- upper.tri(cors)
out <- data.frame(which(up, arr.ind=TRUE), cor=cors[up])
out <- out[!is.na(out$cor),]
out[order(abs(out$cor), decreasing=TRUE),]

#3


2  

Replace ... with your correlation call.

取代……和你的联系电话。

library(reshape)
x <- subset(melt(cor(...)), value != 1 | value != NA)
x <- x[with(x, order(-abs(x$value))),]

If you're getting a lot of NA in your correlations, perhaps try using the use="complete.obs" argument in your correlation call.

如果你在你的相关性中得到了很多NA,也许可以尝试使用use=“complete”。在相关调用中的obs参数。