Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中组合一个矩阵的上.tri和另一个矩阵的下.tri?_R_Matrix - Fatal编程技术网

如何在R中组合一个矩阵的上.tri和另一个矩阵的下.tri?

如何在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

我有两个长度相同的对称矩阵(一个包含相关系数,另一个包含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 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))
@我最近收到的邮件,上面和下面都没有关系。如果有意义的话,我很难找到使一个矩阵的上三元组成为另一个矩阵的上三元组的语法。