报告 cor() 输出的唯一组合
report unique combinations of cor() output
我想报告(融化的)相关矩阵中的所有唯一值。
如果我这样做:
melt(cor(x,method="pearson",use="complete.obs"))
我会得到:
VarA VarA 1
VarA VarB 0.001
VarA VarC -0.002
VarB VarB 1
VarB VarA 0.001
VarB VarC 0.003
VarC VarC 1
VarC VarA -0.002
VarC VarB 0.003
然而,有些行实际上报告的是同一件事,即 VarA VarB = VarB VarA
,所以我真正想要的是:
VarA VarA 1
VarA VarB 0.001
VarA VarC -0.002
VarB VarB 1
VarB VarC 0.003
VarC VarC 1
甚至更好的奖励是删除与自身相关的变量,这样我只会得到:
VarA VarB 0.001
VarA VarC -0.002
VarB VarC 0.003
你可以在矩阵上工作,这样更容易:
res <- cor(iris[,-5])
res[lower.tri(res)] <- NA #assuming there are no actual NAs already
# which seems likely with complete.obs
#use lower.tri(res, diag = TRUE) to remove the diagonal too
na.omit(reshape2::melt(res))
# Var1 Var2 value
#1 Sepal.Length Sepal.Length 1.0000000
#5 Sepal.Length Sepal.Width -0.1175698
#6 Sepal.Width Sepal.Width 1.0000000
#9 Sepal.Length Petal.Length 0.8717538
#10 Sepal.Width Petal.Length -0.4284401
#11 Petal.Length Petal.Length 1.0000000
#13 Sepal.Length Petal.Width 0.8179411
#14 Sepal.Width Petal.Width -0.3661259
#15 Petal.Length Petal.Width 0.9628654
#16 Petal.Width Petal.Width 1.0000000
您可以采用两步法:
#starting from:
x <- melt(cor(x,method="pearson",use="complete.obs"))
#subset first the variable 3 when it is equal to 1
x <- subset(x, V3 != 1)
#remove duplicate entries in that same variable
x[duplicated(x$V3),]
V1 V2 V3
5 VarB VarA 0.001
8 VarC VarA -0.002
9 VarC VarB 0.003
我想报告(融化的)相关矩阵中的所有唯一值。
如果我这样做:
melt(cor(x,method="pearson",use="complete.obs"))
我会得到:
VarA VarA 1
VarA VarB 0.001
VarA VarC -0.002
VarB VarB 1
VarB VarA 0.001
VarB VarC 0.003
VarC VarC 1
VarC VarA -0.002
VarC VarB 0.003
然而,有些行实际上报告的是同一件事,即 VarA VarB = VarB VarA
,所以我真正想要的是:
VarA VarA 1
VarA VarB 0.001
VarA VarC -0.002
VarB VarB 1
VarB VarC 0.003
VarC VarC 1
甚至更好的奖励是删除与自身相关的变量,这样我只会得到:
VarA VarB 0.001
VarA VarC -0.002
VarB VarC 0.003
你可以在矩阵上工作,这样更容易:
res <- cor(iris[,-5])
res[lower.tri(res)] <- NA #assuming there are no actual NAs already
# which seems likely with complete.obs
#use lower.tri(res, diag = TRUE) to remove the diagonal too
na.omit(reshape2::melt(res))
# Var1 Var2 value
#1 Sepal.Length Sepal.Length 1.0000000
#5 Sepal.Length Sepal.Width -0.1175698
#6 Sepal.Width Sepal.Width 1.0000000
#9 Sepal.Length Petal.Length 0.8717538
#10 Sepal.Width Petal.Length -0.4284401
#11 Petal.Length Petal.Length 1.0000000
#13 Sepal.Length Petal.Width 0.8179411
#14 Sepal.Width Petal.Width -0.3661259
#15 Petal.Length Petal.Width 0.9628654
#16 Petal.Width Petal.Width 1.0000000
您可以采用两步法:
#starting from:
x <- melt(cor(x,method="pearson",use="complete.obs"))
#subset first the variable 3 when it is equal to 1
x <- subset(x, V3 != 1)
#remove duplicate entries in that same variable
x[duplicated(x$V3),]
V1 V2 V3
5 VarB VarA 0.001
8 VarC VarA -0.002
9 VarC VarB 0.003