-1

currently I have to matrixs, and I am trying to return the index values where they match in z? This is what I have currently

           x y
matrixloc 0 0
          2 1
          2 3
          2 6
          5 5
          6 7
          4 9
> z
 x y
[1,] 2 6
[2,] 3 4
[3,] 5 5
[4,] 2 1
[5,] 2 3
[6,] 6 7  
[7,] 4 9
[8,] 6 8

currently the output I am getting is :

     match(matrixloc,z)
  NA  1  1  1  3  6  7 NA 12  2  6  3 14 15 

Any suggestions on how to fix this?

1 Answers1

0

Here is one (of many) ways using match on the pasted column entries

# Your sample data
matrixloc <- as.matrix(read.table(text =
    "x y
     0 0
     2 1
     2 3
     2 6
     5 5
     6 7
     4 9", header = T));
z <- as.matrix(read.table(text =
    "x y
     2 6
     3 4
     5 5
     2 1
     2 3
     6 7
     4 9
     6 8", header = T));

match(apply(matrixloc, 1, paste, collapse = "_"), apply(z, 1, paste, collapse = "_"));
#[1] NA  4  5  1  3  6  7

Also, the plural form of "matrix" is "matrices", not "matrixs".

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68