【问题标题】:How do I combine the upper.tri of one matrix with the lower.tri of another in R?如何在R中将一个矩阵的upper.tri与另一个矩阵的lower.tri结合起来?
【发布时间】:2025-11-30 18:20:04
【问题描述】:

我有两个长度相同的对称矩阵(一个包含相关系数,另一个包含 p 值)。

我正在尝试制作一个矩阵,使得 upper.tri 包含相关系数,而 lower.tri 包含相关的 p 值。

【问题讨论】:

    标签: r matrix


    【解决方案1】:

    假设您的矩阵是 correlpval

    # 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)]
    

    【讨论】:

    • 你的意思是correl[upper.tri(correl)]?
    • @Atticus29 如果它们的大小相同,那应该没关系。 upper.tri 只返回一堆逻辑 TRUE/FALSEes,上三角为真。试试upper.tri(matrix(1:9,ncol=3))
    • @thelatemail 我知道上下都没有关系。如果有意义的话,我很难找到使一个矩阵的上三角成为另一个矩阵的上三角的语法。
    最近更新 更多