I have the following cross-correlation matrix:
df =
A B C
A 1 7 1
B 7 1 9
C 1 9 1
and I would like to make it into the following format:
A B 7
A C 1
B C 9
any straightforward R code that can do such thing?
Not a straightforward way but an option in base R :
mat[upper.tri(mat, diag = TRUE)] <- NA
tmp <- which(!is.na(mat), arr.ind = TRUE)
data.frame(col = colnames(mat)[tmp[, 2]], row = rownames(tmp), val = mat[tmp])
# col row val
#1 A B 7
#2 A C 1
#3 B C 9
data
mat <- structure(c(1L, 7L, 1L, 7L, 1L, 9L, 1L, 9L, 1L), .Dim = c(3L,
3L), .Dimnames = list(c("A", "B", "C"), c("A", "B", "C")))