title: R code for substituting values from one column to the missing values in another column
body: <p>I have a data set named <code>one</code> with four columns: <code>D1</code>, <code>D2</code>, <code>D3</code> and <code>D4</code>.  <code>D1</code> is the id. <code>D2</code> has seven levels (<code>a</code>, <code>b</code>, <code>c</code>, <code>d</code>, <code>e</code>, <code>f</code>, <code>g</code>). <code>D3</code> has missing data, which I want to fill by matching conditions from columns <code>D2</code> and <code>D4</code>. I am selecting values from column <code>D4</code> corresponding to four levels (<code>a</code>, <code>c</code>, <code>d</code>, <code>e</code>) of column <code>D2</code> and then replacing the missing values of column <code>D3</code> with those from <code>D4</code>. </p>
  
  <pre><code>D1  D2  D3  D4
  1   a   .   5
  2   c   12  6
  3   e   .   3
  4   b   .   7
  5   f   .   8
  6   e   .   9
  7   e   11  8
  8   c   .   3
  9   c   52  5
  10  a   .   6
  11  b   4   7
  12  f   .   2
  13  f   .   10
  14  d   .   12
  15  d   .   13
  16  e   .   24
  17  a   1   54
  18  b   2   19
  19  c   5   21
  </code></pre>
  
  <p>I have following solution but it is not working. Any suggestion or help? Thanks.</p>
  
  <pre><code>index &lt;- with(one, D2 %in% c('a','c','d','e'))
  one$D4[index] &lt;- one$D3[index]
  one
  </code></pre>