title: Vectorized ifelse conundrum
body: <p>I have two arrays "begin" and "end_a" which contain some integer indices, except that some of the entries in "end_a" are <code>NA</code>.</p>
  
  <p>And <code>panelDataset</code> is a matrix which contains the data. I want to take the means of the rows of <code>panelDataset</code> corresponding to non-NA entries of begin and end_a.</p>
  
  <p>I have this working in serial fashion and it works fine, but when I tried to vectorize it as follows</p>
  
  <pre><code>switch_mu=ifelse(!is.na(end_a),mean(panelDataset[begin: end_a,4]),NA)
  </code></pre>
  
  <p>It gives an error: <code>Error in begin:end_a : NA/NaN argument.</code></p>
  
  <p>When I check the entries of end_a separately for <code>NA</code>s using <code>is.na(end_a)</code>, it does show the correct entries of the array as <code>NA</code>. So, that is not an issue.</p>
  
  <p>I know I am missing something trivial. Any thoughts?</p>