【问题标题】:Apply a function to all column pairs of two matrices in R将函数应用于 R 中两个矩阵的所有列对
【发布时间】:2015-06-20 17:43:47
【问题描述】:

我有两个具有相同列名和行名的矩阵:

> metilacion[1:5,1:5]
            A2BP1       A2M     A2ML1     A4GALT       AAAS
paciente1   0.2804884 0.5816559 1.1814702 -0.6234276 -0.3997400
paciente2   0.5122471 1.2944264 0.5673766  0.4490407 -0.6045510
paciente3  -0.3116356 1.6085049 0.9970350  0.3379215 -0.4787046
paciente4  -0.7220941 0.8771948 2.1445474 -0.5837802 -0.4848246
paciente5  -0.3369999 1.5885716 0.8185654  0.2374583 -0.5698858
> expresion[1:5,1:5]
           A2BP1         A2M     A2ML1      A4GALT       AAAS
paciente1 -0.9082274 -0.17736185 0.8846485 -0.36059775 -0.5624139
paciente2 -1.7152290  1.62368019 0.3292617  1.35968899 -0.9220157
paciente3 -1.0581859  0.33028098 1.1020073  0.01870851 -0.9669236
paciente4 -0.8389615  1.33754885 0.5122861 -0.14583960 -0.8196533
paciente5 -1.5273835  0.06418637 0.2695209  0.03381359 -0.4461490

我想计算两个矩阵之间所有列对之间的相关系数,并为每个列对生成另一个具有相关值的对象。

例如,第一列之间的相关系数为:

> cor(metilacion[,1],expresion[,1])
[1] -0.09351992

所以,我想生成一个包含所有相关值的对象。

谢谢!

【问题讨论】:

  • 试试cor(metilacion,expresion) 根据?cor"If ‘x’ and ‘y’ are matrices then the covariances (or correlations) between the columns of ‘x’ and the columns of ‘y’ are computed.
  • 如果你只是寻找对应的列,那么diag(cor(metilacion,expresion))

标签: r matrix


【解决方案1】:

你可以cor

 cor(metilacion,expression)
 #          A2BP1         A2M      A2ML1     A4GALT        AAAS
 #A2BP1  -0.4887051  0.03682951 -0.0404260  0.5795882 -0.03534625
 #A2M    -0.5909642  0.01572799 -0.1469085  0.3503903 -0.19412101
 #A2ML1   0.8006633  0.17242226  0.1294179 -0.5827062 -0.05502329
 #A4GALT -0.8036390  0.18066923 -0.2026173  0.6824085 -0.32097886
 #AAAS    0.9033514 -0.54378874  0.7694163 -0.7995712  0.13676285

如果需要获取对应列的cor

 diag(cor(metilacion,expresion))

或者

 mapply(cor, as.data.frame(metilacion), as.data.frame(expresion))
 #    A2BP1         A2M       A2ML1      A4GALT        AAAS 
 #-0.48870510  0.01572799  0.12941787  0.68240850  0.13676285 

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 2021-09-13
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    相关资源
    最近更新 更多