在2个矩阵之间寻找对应的值

时间:2018-11-29 13:29:26

标签: r matrix

我有2个矩阵:

矩阵-test_nodes

head (test_nodes)

   1  2  3  4  5  6  7  8  9 10
1  9 17 32 35 20 35 41 40  9 48
2  9 14  8  8 16 16 14 13  4  9
3 24 17 21 35 20 35 33 48  9 48
4  9 14  8  8 16 16 14 13  4  9
5  9 30  8  8 16 16 14 20  4  9
6 42 38  6 12 50 13 36 44 51 13

矩阵-NAMETRANS

head (NAMETRANS)

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 6    5    4    8    8    8    4    5    4    8    
[2,] 8    8    6    11   14   13   10   6    9    9    
[3,] 9    13   8    12   16   16   12   12   11   10   
[4,] 12   14   15   14   18   23   14   13   14   13   
[5,] 17   16   21   16   19   26   17   17   18   16   
[6,] 20   17   22   17   20   28   18   18   19   20 


> NAMETRANS
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,] 6    5    4    8    8    8    4    5    4    8    
 [2,] 8    8    6    11   14   13   10   6    9    9    
 [3,] 9    13   8    12   16   16   12   12   11   10   
 [4,] 12   14   15   14   18   23   14   13   14   13   
 [5,] 17   16   21   16   19   26   17   17   18   16   
 [6,] 20   17   22   17   20   28   18   18   19   20   
 [7,] 21   20   23   18   21   29   19   20   20   22   
 [8,] 22   23   25   24   22   30   23   21   21   25   
 [9,] 23   25   29   26   23   31   24   22   23   27   
[10,] 24   26   30   28   24   32   27   24   24   28   
[11,] 30   28   31   32   27   33   30   26   28   29   
[12,] 32   30   32   34   29   35   32   28   32   31   
[13,] 35   31   33   35   31   36   33   32   34   32   
[14,] 36   33   35   37   32   37   35   33   36   34   
[15,] 38   36   37   38   34   38   36   34   37   36   
[16,] 40   37   40   39   37   41   38   39   39   37   
[17,] 41   38   41   40   39   42   41   40   42   38   
[18,] 42   40   42   41   44   43   42   43   43   41   
[19,] 44   41   43   43   45   45   44   44   44   43   
[20,] 46   42   45   44   46   46   45   45   45   45   
[21,] 47   43   46   46   47   47   46   46   46   48   
[22,] 48   44   48   47   48   48   48   48   47   49   
[23,] 49   46   50   49   49   49   49   50   48   51   
[24,] 50   47   51   50   50   50   50   51   49   52   
[25,] 51   NULL 52   51   53   51   51   52   51   53   
[26,] 53   NULL 53   52   54   52   52   53   52   54   
[27,] 55   NULL 54   55   55   53   53   54   54   55   
[28,] 56   NULL 57   56   57   NULL NULL 55   56   56   
[29,] 58   NULL 58   57   58   NULL NULL NULL 57   57   
[30,] 59   NULL 59   NULL 59   NULL NULL NULL NULL NULL 

我想创建第三个矩阵TRANSNAMES。 NAMETRANS将遍历test_nodes中的每一列,并将单元格中的值替换为相关列中的行号的值。例如,对于9值,它在test_nodes的列1中,并且在NAMETRANS中是行号3,因此它将在TRANSNAMES中得到值3。

TRANSNAMES [1,1] = 3
TRANSNAMES [2,1] = 3 

(还有9个)等

Here is the result, something is wrong:
> transnames
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,] 3    NULL 12   NULL 6    NULL NULL NULL NULL NULL 
 [2,] 3    NULL 3    NULL 3    NULL NULL NULL NULL NULL 
 [3,] 10   NULL 5    NULL 6    NULL NULL NULL NULL NULL 
 [4,] 3    NULL 3    NULL 3    NULL NULL NULL NULL NULL 
 [5,] 3    NULL 3    NULL 3    NULL NULL NULL NULL NULL 
 [6,] 18   NULL 2    NULL 24   NULL NULL NULL NULL NULL 
 [7,] 24   NULL 8    NULL 24   NULL NULL NULL NULL NULL 
 [8,] 10   NULL 5    NULL 15   NULL NULL NULL NULL NULL 
 [9,] 19   NULL 8    NULL 24   NULL NULL NULL NULL NULL 
[10,] 10   NULL 5    NULL 6    NULL NULL NULL NULL NULL 
[11,] 3    NULL 16   NULL 6    NULL NULL NULL NULL NULL 
[12,] 10   NULL 5    NULL 6    NULL NULL NULL NULL NULL 
[13,] 12   NULL 18   NULL 10   NULL NULL NULL NULL NULL
>dput(test_nodes)
...
  c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")))
   1  2  3  4  5  6  7  8  9 10
1  9 17 32 35 20 35 41 40  9 48
2  9 14  8  8 16 16 14 13  4  9
3 24 17 21 35 20 35 33 48  9 48
4  9 14  8  8 16 16 14 13  4  9
5  9 30  8  8 16 16 14 20  4  9
6 42 38  6 12 50 13 36 44 51 13

>

 dput(NAMETRANS)
structure(list(6, 8, 9, 12, 17, 20, 21, 22, 23, 24, 30, 32, 35, 
    36, 38, 40, 41, 42, 44, 46, 47, 48, 49, 50, 51, 53, 55, 56, 
    58, 59, 5, 8, 13, 14, 16, 17, 20, 23, 25, 26, 28, 30, 31, 
    33, 36, 37, 38, 40, 41, 42, 43, 44, 46, 47, NULL, NULL, NULL, 
    NULL, NULL, NULL, 4, 6, 8, 15, 21, 22, 23, 25, 29, 30, 31, 
    32, 33, 35, 37, 40, 41, 42, 43, 45, 46, 48, 50, 51, 52, 53, 
    54, 57, 58, 59, 8, 11, 12, 14, 16, 17, 18, 24, 26, 28, 32, 
    34, 35, 37, 38, 39, 40, 41, 43, 44, 46, 47, 49, 50, 51, 52, 
    55, 56, 57, NULL, 8, 14, 16, 18, 19, 20, 21, 22, 23, 24, 
    27, 29, 31, 32, 34, 37, 39, 44, 45, 46, 47, 48, 49, 50, 53, 
    54, 55, 57, 58, 59, 8, 13, 16, 23, 26, 28, 29, 30, 31, 32, 
    33, 35, 36, 37, 38, 41, 42, 43, 45, 46, 47, 48, 49, 50, 51, 
    52, 53, NULL, NULL, NULL, 4, 10, 12, 14, 17, 18, 19, 23, 
    24, 27, 30, 32, 33, 35, 36, 38, 41, 42, 44, 45, 46, 48, 49, 
    50, 51, 52, 53, NULL, NULL, NULL, 5, 6, 12, 13, 17, 18, 20, 
    21, 22, 24, 26, 28, 32, 33, 34, 39, 40, 43, 44, 45, 46, 48, 
    50, 51, 52, 53, 54, 55, NULL, NULL, 4, 9, 11, 14, 18, 19, 
    20, 21, 23, 24, 28, 32, 34, 36, 37, 39, 42, 43, 44, 45, 46, 
    47, 48, 49, 51, 52, 54, 56, 57, NULL, 8, 9, 10, 13, 16, 20, 
    22, 25, 27, 28, 29, 31, 32, 34, 36, 37, 38, 41, 43, 45, 48, 
    49, 51, 52, 53, 54, 55, 56, 57, NULL), .Dim = c(30L, 10L))

1 个答案:

答案 0 :(得分:1)

这应该在base中完成:

for(j in 1:ncol(transnames)){
  for(i in 1:nrow(transnames)){
    tryCatch(transnames[i,j] <- which(nametrans[,j]==test_nodes[i,j]),
             error = function(e) return(NA))
  }
}
> transnames
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    3    6   NA   NA    6   NA   NA   NA    2    NA
[2,]    3    4    3    1    3    3    4    4    1     2
[3,]   NA    6    5   NA    6   NA   NA   NA    2    NA
[4,]    3    4    3    1    3    3    4    4    1     2
[5,]    3   NA    3    1    3    3    4   NA    1     2
[6,]   NA   NA    2    3   NA    2   NA   NA   NA     4

989所述,如果没有在nametrans的相应列中找到该值,则不会提供有关矩阵应包含的内容的信息,因此此循环仅返回这些值的NA