Search code examples
rmatrixvectorrow

Switch rows between two matrices in R


Let the matrices A and B.

A=c(1:5)*matrix(1,5,5)
B=10*A

that is,

> A
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    2    2    2    2    2
[3,]    3    3    3    3    3
[4,]    4    4    4    4    4
[5,]    5    5    5    5    5

> B
     [,1] [,2] [,3] [,4] [,5]
[1,]   10   10   10   10   10
[2,]   20   20   20   20   20
[3,]   30   30   30   30   30
[4,]   40   40   40   40   40
[5,]   50   50   50   50   50

I would like, for example, to switch the first rows between the matrices A and B, that is

> A
     [,1] [,2] [,3] [,4] [,5]
[1,]   10   10   10   10   10
[2,]    2    2    2    2    2
[3,]    3    3    3    3    3
[4,]    4    4    4    4    4
[5,]    5    5    5    5    5

> B
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]   20   20   20   20   20
[3,]   30   30   30   30   30
[4,]   40   40   40   40   40
[5,]   50   50   50   50   50

using a function, and without using any indermediate vector or a for loop.


Solution

  • Update

    As per you update in the comment, you can try

    lapply(
      1:nrow(B),
      function(k) {
        setNames(
          Map(
            function(x, ind,r) {
              x[ind, ] <- r
              x
            },
            list(A, B),
            list(1,k),
            list(B[k, ], A[1, ])
          ), c("A", "B")
        )
      }
    )
    

    which gives

    [[1]]
    [[1]]$A
         [,1] [,2] [,3] [,4] [,5]
    [1,]   10   10   10   10   10
    [2,]    2    2    2    2    2
    [3,]    3    3    3    3    3
    [4,]    4    4    4    4    4
    [5,]    5    5    5    5    5
    
    [[1]]$B
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    1    1    1    1
    [2,]   20   20   20   20   20
    [3,]   30   30   30   30   30
    [4,]   40   40   40   40   40
    [5,]   50   50   50   50   50
    
    
    [[2]]
    [[2]]$A
         [,1] [,2] [,3] [,4] [,5]
    [1,]   20   20   20   20   20
    [2,]    2    2    2    2    2
    [3,]    3    3    3    3    3
    [4,]    4    4    4    4    4
    [5,]    5    5    5    5    5
    
    [[2]]$B
         [,1] [,2] [,3] [,4] [,5]
    [1,]   10   10   10   10   10
    [2,]    1    1    1    1    1
    [3,]   30   30   30   30   30
    [4,]   40   40   40   40   40
    [5,]   50   50   50   50   50
    
    
    [[3]]
    [[3]]$A
         [,1] [,2] [,3] [,4] [,5]
    [1,]   30   30   30   30   30
    [2,]    2    2    2    2    2
    [3,]    3    3    3    3    3
    [4,]    4    4    4    4    4
    [5,]    5    5    5    5    5
    
    [[3]]$B
         [,1] [,2] [,3] [,4] [,5]
    [1,]   10   10   10   10   10
    [2,]   20   20   20   20   20
    [3,]    1    1    1    1    1
    [4,]   40   40   40   40   40
    [5,]   50   50   50   50   50
    
    
    [[4]]
    [[4]]$A
         [,1] [,2] [,3] [,4] [,5]
    [1,]   40   40   40   40   40
    [2,]    2    2    2    2    2
    [3,]    3    3    3    3    3
    [4,]    4    4    4    4    4
    [5,]    5    5    5    5    5
    
    [[4]]$B
         [,1] [,2] [,3] [,4] [,5]
    [1,]   10   10   10   10   10
    [2,]   20   20   20   20   20
    [3,]   30   30   30   30   30
    [4,]    1    1    1    1    1
    [5,]   50   50   50   50   50
    
    
    [[5]]
    [[5]]$A
         [,1] [,2] [,3] [,4] [,5]
    [1,]   50   50   50   50   50
    [2,]    2    2    2    2    2
    [3,]    3    3    3    3    3
    [4,]    4    4    4    4    4
    [5,]    5    5    5    5    5
    
    [[5]]$B
         [,1] [,2] [,3] [,4] [,5]
    [1,]   10   10   10   10   10
    [2,]   20   20   20   20   20
    [3,]   30   30   30   30   30
    [4,]   40   40   40   40   40
    [5,]    1    1    1    1    1
    

    You can try the code below

    list2env(
      setNames(
        Map(
          function(x, r) {
            x[1, ] <- r
            x
          },
          list(A, B),
          list(B[1, ], A[1, ])
        ), c("A", "B")
      ),
      envir = .GlobalEnv
    )