如何在R中组合一个矩阵的上.tri和另一个矩阵的下.tri?
我有两个长度相同的对称矩阵(一个包含相关系数,另一个包含p值)如何在R中组合一个矩阵的上.tri和另一个矩阵的下.tri?,r,matrix,R,Matrix,我有两个长度相同的对称矩阵(一个包含相关系数,另一个包含p值) 我正在尝试制作一个矩阵,使上面的.tri包含相关系数,下面的.tri包含相关的p值。假设你的矩阵是correl和pval # create a new matrix that is the pvalues new <- pval # not sure what you want the diagonal to be, lets make it NA diag(new) <- NA # replace the upper
我正在尝试制作一个矩阵,使上面的.tri包含相关系数,下面的.tri包含相关的p值。假设你的矩阵是
correl
和pval
# create a new matrix that is the pvalues
new <- pval
# not sure what you want the diagonal to be, lets make it NA
diag(new) <- NA
# replace the upper triangle of this new matrix with the
# upper triangle of the correlation matrix
new[upper.tri(new)] <- correl[upper.tri(correl)]
#创建一个新的矩阵,该矩阵是pvalues
你是说correl[upper.tri(correl)]是新的吗?@Atticus29如果它们大小相同,那就没关系了upper.tri
只返回一组逻辑上的真/假,真在上三角。试试upper.tri(矩阵(1:9,ncol=3))
@我最近收到的邮件,上面和下面都没有关系。如果有意义的话,我很难找到使一个矩阵的上三元组成为另一个矩阵的上三元组的语法。