让我们看一下示例矩阵并计算相关性:
some.data <- data.frame(
A1.1 = c(1,3,4,5,6),
A1.2 = c(4,5,6,2,3),
A1.3 = c(3,3,4,2,1),
A2.1 = c(3,4,5,2,4),
A2.2 = c(4,5,5,4,2),
A2.3 = c(1,1,2,2,3),
A3.1 = c(1,3,4,5,6),
A3.2 = c(1,4,3,3,4),
A3.3 = c(4,4,4,4,5)
)
cor.mat <- cor(some.data)
哪个给:
A1.1 A1.2 A1.3 A2.1 A2.2 A2.3 A3.1 A3.2 A3.3
A1.1 1.00000000 -0.4109975 -0.6155470 0.06839411 -0.5305954 0.9009862 1.00000000 0.7428336 0.6393620
A1.2 -0.41099747 1.0000000 0.8320503 0.83205029 0.6454972 -0.3779645 -0.41099747 0.0000000 -0.3535534
A1.3 -0.61554702 0.8320503 1.0000000 0.42307692 0.8951436 -0.6289709 -0.61554702 -0.3580574 -0.7844645
A2.1 0.06839411 0.8320503 0.4230769 1.00000000 0.1790287 0.1572427 0.06839411 0.3580574 0.1961161
A2.2 -0.53059545 0.6454972 0.8951436 0.17902872 1.0000000 -0.7319251 -0.53059545 -0.1666667 -0.9128709
A2.3 0.90098616 -0.3779645 -0.6289709 0.15724273 -0.7319251 1.0000000 0.90098616 0.4879500 0.8017837
A3.1 1.00000000 -0.4109975 -0.6155470 0.06839411 -0.5305954 0.9009862 1.00000000 0.7428336 0.6393620
A3.2 0.74283363 0.0000000 -0.3580574 0.35805744 -0.1666667 0.4879500 0.74283363 1.0000000 0.4564355
A3.3 0.63936201 -0.3535534 -0.7844645 0.19611614 -0.9128709 0.8017837 0.63936201 0.4564355 1.0000000
在我的原始数据中,某些列是相关的,这里用前缀(A1,A2,A3)表示。由于这些对我而言不感兴趣,因此我想将具有相同前缀的相关性设置为零,如下所示:
A1.1 A1.2 A1.3 A2.1 A2.2 A2.3 A3.1 A3.2 A3.3
A1.1 0 0 0 0.06839411 -0.5305954 0.9009862 1.00000000 0.7428336 0.6393620
A1.2 0 0 0 0.83205029 0.6454972 -0.3779645 -0.41099747 0.0000000 -0.3535534
A1.3 0 0 0 0.42307692 0.8951436 -0.6289709 -0.61554702 -0.3580574 -0.7844645
A2.1 0.06839411 0.8320503 0.4230769 0 0 0 0.06839411 0.3580574 0.1961161
A2.2 -0.53059545 0.6454972 0.8951436 0 0 0 -0.53059545 -0.1666667 -0.9128709
A2.3 0.90098616 -0.3779645 -0.6289709 0 0 0 0.90098616 0.4879500 0.8017837
A3.1 1.00000000 -0.4109975 -0.6155470 0.06839411 -0.5305954 0.9009862 0 0 0
A3.2 0.74283363 0.0000000 -0.3580574 0.35805744 -0.1666667 0.4879500 0 0 0
A3.3 0.63936201 -0.3535534 -0.7844645 0.19611614 -0.9128709 0.8017837 0 0 0
我可以使用for循环来做到这一点,但我想这样做可以比这容易得多吗?
答案 0 :(得分:3)
一种选择是将数据从宽到长整形,使其包含三列
cor.mat_long <- reshape2::melt(cor.mat)
cor.mat_long
# Var1 Var2 value
#1 A1.1 A1.1 1.00000000
#2 A1.2 A1.1 -0.41099747
#3 A1.3 A1.1 -0.61554702
#4 A2.1 A1.1 0.06839411
#5 A2.2 A1.1 -0.53059545
#6 A2.3 A1.1 0.90098616
#...
根据Var1
和Var2
的前缀创建逻辑向量,以指示这些前缀何时相同。使用此向量将cor.mat_long$value
替换为0
,其结果为TRUE
cor.mat_long$value[with(cor.mat_long, sub("\\.\\d+$", "", Var1) == sub("\\.\\d+$", "", Var2))] <- 0
最后再次调整为宽格式。
cor.mat2 <- reshape2::dcast(cor.mat_long, Var1 ~ Var2)
cor.mat2
# Var1 A1.1 A1.2 A1.3 A2.1 A2.2 A2.3 A3.1 A3.2 A3.3
#1 A1.1 0.00000000 0.0000000 0.0000000 0.06839411 -0.5305954 0.9009862 1.00000000 0.7428336 0.6393620
#2 A1.2 0.00000000 0.0000000 0.0000000 0.83205029 0.6454972 -0.3779645 -0.41099747 0.0000000 -0.3535534
#3 A1.3 0.00000000 0.0000000 0.0000000 0.42307692 0.8951436 -0.6289709 -0.61554702 -0.3580574 -0.7844645
#4 A2.1 0.06839411 0.8320503 0.4230769 0.00000000 0.0000000 0.0000000 0.06839411 0.3580574 0.1961161
#5 A2.2 -0.53059545 0.6454972 0.8951436 0.00000000 0.0000000 0.0000000 -0.53059545 -0.1666667 -0.9128709
#6 A2.3 0.90098616 -0.3779645 -0.6289709 0.00000000 0.0000000 0.0000000 0.90098616 0.4879500 0.8017837
#7 A3.1 1.00000000 -0.4109975 -0.6155470 0.06839411 -0.5305954 0.9009862 0.00000000 0.0000000 0.0000000
#8 A3.2 0.74283363 0.0000000 -0.3580574 0.35805744 -0.1666667 0.4879500 0.00000000 0.0000000 0.0000000
#9 A3.3 0.63936201 -0.3535534 -0.7844645 0.19611614 -0.9128709 0.8017837 0.00000000 0.0000000 0.0000000
如果您不希望将Var1
用作显式列,请
rownames(cor.mat2) <- cor.mat2$Var1
cor.mat2 <- cor.mat2[-1]
不知道这是否比循环容易得多。
答案 1 :(得分:2)
我们可以乘以1的块对角矩阵
library(Matrix)
as.matrix(cor.mat * !bdiag(replicate(3, matrix(1, 3, 3), simplify = FALSE)))
# A1.1 A1.2 A1.3 A2.1 A2.2 A2.3 A3.1 A3.2 A3.3
#A1.1 0.00000000 0.0000000 0.0000000 0.06839411 -0.5305954 0.9009862 1.00000000 0.7428336 0.6393620
#A1.2 0.00000000 0.0000000 0.0000000 0.83205029 0.6454972 -0.3779645 -0.41099747 0.0000000 -0.3535534
#A1.3 0.00000000 0.0000000 0.0000000 0.42307692 0.8951436 -0.6289709 -0.61554702 -0.3580574 -0.7844645
#A2.1 0.06839411 0.8320503 0.4230769 0.00000000 0.0000000 0.0000000 0.06839411 0.3580574 0.1961161
#A2.2 -0.53059545 0.6454972 0.8951436 0.00000000 0.0000000 0.0000000 -0.53059545 -0.1666667 -0.9128709
#A2.3 0.90098616 -0.3779645 -0.6289709 0.00000000 0.0000000 0.0000000 0.90098616 0.4879500 0.8017837
#A3.1 1.00000000 -0.4109975 -0.6155470 0.06839411 -0.5305954 0.9009862 0.00000000 0.0000000 0.0000000
#A3.2 0.74283363 0.0000000 -0.3580574 0.35805744 -0.1666667 0.4879500 0.00000000 0.0000000 0.0000000
#A3.3 0.63936201 -0.3535534 -0.7844645 0.19611614 -0.9128709 0.8017837 0.00000000 0.0000000 0.0000000
或者另一种选择是使用row/column
索引
replace(cor.mat, cbind(rep(1:9, each = 3),
c(sapply(list(1:3, 4:6, 7:9), rep, 3))), 0)
或使用outer
来构建逻辑矩阵并与cor.mat
nm1 <- sub("\\.\\d+$", "", colnames(cor.mat))
cor.mat * outer(nm1, nm1, `!=`)