Reputation: 1840
Lets take a look at an example matrix and caclulate the correlations:
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)
Which gives:
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
In my original data some columns are dependent, here indicated by the prefixes (A1, A2, A3). As these are not of interest to me I want to set the correlations with the same prefix to zero like so:
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
I could do this using a for loop but I suppose this could be done much easier than that?
Upvotes: 0
Views: 857
Reputation: 887148
We could multiply with a block diagonal matrix of 1's
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
Or another option is to use row/column
index
replace(cor.mat, cbind(rep(1:9, each = 3),
c(sapply(list(1:3, 4:6, 7:9), rep, 3))), 0)
Or use outer
to construct a logical matrix and multiply with the cor.mat
nm1 <- sub("\\.\\d+$", "", colnames(cor.mat))
cor.mat * outer(nm1, nm1, `!=`)
Upvotes: 2
Reputation: 26343
One option is to reshape your data from wide to long such that it will contain three columns
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
#...
Create a logical vector based on the prefixes of Var1
and Var2
that indicates when these prefixes are the same. Use this vector to replace cor.mat_long$value
with 0
where it evaluates to TRUE
cor.mat_long$value[with(cor.mat_long, sub("\\.\\d+$", "", Var1) == sub("\\.\\d+$", "", Var2))] <- 0
Finally reshape to wide format again.
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
If you don't want Var1
as an explicit column, do
rownames(cor.mat2) <- cor.mat2$Var1
cor.mat2 <- cor.mat2[-1]
Don't know if this is much easier than your loop though.
Upvotes: 3