title: Extracting Location and Value of Vector
body: <p>So I want to extract both the location and the value of a vector. Here is the example code: </p>
  
  <pre><code>x &lt;- matrix(1:96, nrow = 8)
  x
  y &lt;- c(0,8,0,0,0,7,0,0,0)
  y
  which(y &gt; 0)
  </code></pre>
  
  <p>This gives and output of</p>
  
  <pre><code>[1] 2 6
  </code></pre>
  
  <p>I want to then find the values of <code>x[2,8]</code> and <code>x[6,7]</code>. I have tried to do: </p>
  
  <pre><code>test &lt;- ifelse(y &gt; 0, x[which(y &gt; 0 ),y], 0)
  </code></pre>
  
  <p>But that only produces </p>
  
  <pre><code>[1]  0 62  0  0  0 62  0  0  0
  </code></pre>
  
  <p>Which is actually <code>x[6,8]</code>. There is probably a relatively easy way to do this in R and I am missing something fairly simple. </p>