1
votes

For a correlation matrix similar to the one below (but much larger) I want to select all correlations > 0.8 and subsequently have the row / column labels returned instead of the values. However, I'm stuck with returning the labels. I tried to add colnames to the syntax but I cannot get it to work

dat <- mtcars
dat2 <- cor(dat)
diag(dat2) <- NA
dat3 <- which(dat2 > 0.8, arr.ind = TRUE)
dat3

     row col
disp   3   2
hp     4   2
cyl    2   3
wt     6   3
cyl    2   4
disp   3   6

So I can solve this manually using e.g. colnames(dat2)[3] but is there a way to have dat3 filled with all the labels automatically?

1
It returns a vector ("disp" "hp" "cyl" "wt" "cyl" "disp"). But it doesn't say which combination of variables it relates to. - Joep_S

1 Answers

1
votes

We can use the index to subset the row names and column names

data.frame(rn = row.names(dat2)[dat3[,1]], cn = colnames(dat2)[dat3[,2]])

Or using the dimnames

Map(`[`, dimnames(dat2), as.data.frame(dat3))