0

I have 2 matrices:

Matrix - 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

Matrix - 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 

I would like to create a third matrix TRANSNAMES. NAMETRANS will go through each column in test_nodes and will replace the value in the cell with the value of the row number in the relevant column. For instance, for the 9 value it is in the column 1 in test_nodes and is the row number 3 in NAMETRANS so it will get value 3 in TRANSNAMES.

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

(also 9) etc.

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))
Avi
  • 2,247
  • 4
  • 30
  • 52
  • 1
    How about e.g., 24 and 42 in the first column of `test_nodes`? That is, does `NAMETRANS` contain all entires of `test_nodes`? – 989 Nov 29 '18 at 13:41

1 Answers1

1

This should do in 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

As 989 already mentioned, you are not providing information on what the matrix should contain if the value is not found in the respective column of nametrans, so this loop just returns NA for those values.

Ben
  • 784
  • 5
  • 14
  • Thanks a lot @Ben T. I get the right results for the first column but something is wrong for the second column. Please see my edit in the question body. – Avi Nov 29 '18 at 14:25
  • I think this is because the elements of one of the matrix is num while the other are int. – Avi Nov 29 '18 at 14:34
  • It would be best if you would provide our data using `dput`, see also here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example e. g. for your data `dput(test_nodes)`. then post the resulting string (beginning with `structure(` here. You can also coerce your "num" (I believe you mean double or so) type matrix with `storage.mode(x) <- "integer"` However, `NULL` as a result looks pretty strange to me – Ben Nov 29 '18 at 14:55
  • ,Please find the dput in the question body. – Avi Nov 29 '18 at 15:35
  • I think I found why. The last values of some of the columns is NULL. Maybe this cause the problem. How can I fix it? In addition, why does it make a mess? – Avi Nov 29 '18 at 15:57
  • how do you create the nametrans matrix? you should consider changing this so you do not end up with a list type containing NULLS – Ben Nov 29 '18 at 16:04
  • Thanks. I initialized it with 0L and it solved the problem. – Avi Nov 29 '18 at 16:50