Applying a function to two lists?

后端 未结 1 568
半阙折子戏
半阙折子戏 2020-12-01 18:11

To find the row-wise correlation of two matrices X and Y, the output should have a correlation value for row 1 of X and row 1 of Y, ..., hence in total ten values (because t

相关标签:
1条回答
  • 2020-12-01 18:33

    You seem to be looking for mapply. Here's an example:

    listA <- list(matrix(rnorm(2000), nrow=10),
                  matrix(rnorm(2000), nrow=10))
    listB <- list(matrix(rnorm(2000), nrow=10),
                  matrix(rnorm(2000), nrow=10))
    mapply(function(X,Y) {
      sapply(1:10, function(row) cor(X[row,], Y[row,]))
      }, X=listA, Y=listB)
    
    0 讨论(0)
提交回复
热议问题