akaDrHouse
akaDrHouse

Reputation: 2240

Create vector of half of correlation matrix

I have a large matrix generated using cor() on 141 variables. I want to generate a histogram of the correlations and thus need to pull out the lower or upper diagonal values into a vector.

My data look like:

          AD02_1083 CD02_1083 AD03_1083 CD03_1083
AD02_1083     1.000     0.998     0.997     0.993
CD02_1083     0.998     1.000     0.993     0.992
AD03_1083     0.997     0.993     1.000     0.997
CD03_1083     0.993     0.992     0.997     1.000  

I want to systematically go through a large matrix and grab such that I would create a vector that consists of c(0.998, 0.997, 0.993, 0.993, 0.992, 0.997) so that I can then examine a histogram of these correlations.

I thought about looping through each column starting with i+1 through 141, then grabbing with a loop like this:

my_vec <- vector()
for (i in 2:length(colnames(test))){
    my_vec <- c(my_vec,test[1:i-1,i])    
}

This worked, although I don't need the labels.

>my_vec
          AD02_1083 CD02_1083 AD02_1083 CD02_1083 AD03_1083 
    0.998     0.997     0.993     0.993     0.992     0.997

Is there a more elegant way to do this?

data:

structure(c(1, 0.998, 0.997, 0.993, 0.998, 1, 0.993, 0.992, 0.997, 
0.993, 1, 0.997, 0.993, 0.992, 0.997, 1), .Dim = c(4L, 4L), .Dimnames = list(
    c("AD02_1083", "CD02_1083", "AD03_1083", "CD03_1083"), c("AD02_1083", 
    "CD02_1083", "AD03_1083", "CD03_1083")))

Upvotes: 1

Views: 1115

Answers (1)

Jan
Jan

Reputation: 5254

With upper.tri() we can get a logical matrix that identifies all elements of the upper matrix. Diagonal elements are omitted by default. That we can use to subset the matrix m1.

m1 <- structure(c(1, 0.998, 0.997, 0.993, 0.998, 1, 0.993, 0.992, 0.997, 
                  0.993, 1, 0.997, 0.993, 0.992, 0.997, 1), 
                .Dim = c(4L, 4L), 
                .Dimnames = list(c("AD02_1083", "CD02_1083", "AD03_1083", "CD03_1083"), 
                                 c("AD02_1083", "CD02_1083", "AD03_1083", "CD03_1083")))
m1[upper.tri(m1)]
#> [1] 0.998 0.997 0.993 0.993 0.992 0.997

Created on 2021-09-30 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions