R中从互相关到列格式
我有以下互相关矩阵: df= 我想把它做成以下格式:R中从互相关到列格式,r,cross-correlation,R,Cross Correlation,我有以下互相关矩阵: df= 我想把它做成以下格式: A B 7 A C 1 B C 9 任何简单的R代码都可以做这样的事情吗?不是简单的方法,而是基本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]
A B 7
A C 1
B C 9
任何简单的R代码都可以做这样的事情吗?不是简单的方法,而是基本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
资料
另一个基本R选项
inds <- which(col(mat) < row(mat), arr.ind = TRUE)
data.frame(
col = colnames(mat)[inds[, "col"]],
row = rownames(mat)[inds[, "row"]],
val = mat[inds]
)
去图书馆玩吧;dist2dfas.distmat。或者可以像.data.frameas.tablemat[lower.trimat,]那样做。
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")))
inds <- which(col(mat) < row(mat), arr.ind = TRUE)
data.frame(
col = colnames(mat)[inds[, "col"]],
row = rownames(mat)[inds[, "row"]],
val = mat[inds]
)
col row val
1 A B 7
2 A C 1
3 B C 9