title: Matching and indexing through two dataframes and one matrix
body: <ol>
  <li>I have a dataframe <code>events</code> with xy-coords of unique points.</li>
  <li>I have a dataframe <code>all_nodes</code> with xy-coords of network nodes. All points of <code>events</code> are also in <code>all_nodes</code>, but not necessarily only once, and at different positions, i.e., the index (row id) of a point in <code>events</code> does not correspond to <code>all_nodes</code>.</li>
  <li>I have a matrix <code>ma</code> of dimension <code>nrow(all_nodes)</code> times <code>nrow(all_nodes)</code> with calculated pairwise interaction terms between all nodes. <code>ma</code>rows and cols correspond with the index (row_ids) of <code>all_nodes</code>.</li>
  </ol>
  <p><strong>My overall goal</strong> is to identify the row ids of <code>events</code> in <code>all_nodes</code>. With this I am aiming to extract a submatrix of pairwise interaction from my matrix <code>ma</code> according to the detected row ids. Finally I want to change the order of the submtarix such that the ids and correponding points correspond to <code>events</code>. Any kind of help (code/reference/hint) is much appreciated!</p>
  <p><strong>Toy data</strong> (you can find real data below)</p>
  <pre><code># coords of unique events 
  events &lt;- data.frame(x = c(1,2,3,4),
                       y = c(4,3,2,1))
  # all_nodes 
  all_nodes &lt;- data.frame(x = c(2,1,120,3,150,4,1),
                       y = c(3,4,120,2,150,1,4))
  # matrix corresponding to the index of all_nodes
  ma &lt;- matrix(data = rnorm(n = 49, mean = 3, sd = 1), 
               nrow = nrow(all_nodes), ncol = nrow(all_nodes))
  ma[6, ] &lt;- ma[2, ]
  </code></pre>
  <p><strong>My effort</strong> which isn't quite helpful, since I ran in several problems.</p>
  <pre><code># coords of unique events 
  events # see toy data
  
  # ------------------------------------------------
  # from object g of class  &quot;sfnetwork&quot; &quot;tbl_graph&quot; &quot;igraph&quot; 
  # all rounded coords of nodes; from g ma is used 
  # in several steps 
  # cols and rows in ma correspond to node ids of g/all_nodes
  
  # all_nodes &lt;- g %&gt;% tidygraph::activate(&quot;nodes&quot;) %&gt;%
  # as.data.frame(geometry)
  # all_nodes &lt;- as.data.frame(matrix(unlist(all_nodes$geometry), ncol = 2, byrow = TRUE))
  # names(all_nodes) &lt;- c('x', 'y')
  # all_nodes &lt;- round(all_nodes, 2)
  # --------------------------------------------------
  
  # matching based on x-coord only 
  ix &lt;- which(all_nodes$x %in% events$x)
  # Problem A
  length(ix) == nrow(events) # different length
  # Problem B
  # and the event with coords x=1, y=4 occurs twice in ix 
  
  sub &lt;- ma[ix, ix]
  # If problems A+B were eleminated, sub would correspond to 
  # all events, but I different indexing makes it unusable  #(several permutations possible)
  </code></pre>
  <p>I also played around with <code>st_equals {sf}</code> to compare geometries directlly using <code>events &lt;- sf::st_as_sf(events[, c('x', 'y')], coords = c('x', 'y'))</code> in a previous step.</p>
  <p><strong>Real data</strong></p>
  <pre><code># removed 
  </code></pre>