title: Replace cell values in data frame based on column name and variable match
body: <p>I would like to replace the value in a data frame cell based on the column name matching a row variable, as in the example code.  I know enough to know that the nested loop is not the preferred way to do this (and that a more efficient solution is probably quite simple), but haven't been able to figure out a clean vectorized approach.  I've been messing with nested apply, i.e., an apply inside a function called by apply, but haven't been successful, nor does that seem like it would be the best way, either.</p>
  
  <p>Input:</p>
  
  <pre><code>test.df
    Group         G1        G2         G3
  1    G1 0.63910462 0.5738143 0.97428347
  2    G2 0.62578294 0.4653417 0.92010090
  3    G3 0.01136336 0.3163722 0.20266664
  4    G1 0.09054996 0.1984567 0.17488220
  5    G2 0.74865266 0.9862222 0.80725355
  6    G3 0.82855980 0.2668529 0.06786335
  7    G1 0.74310410 0.8861169 0.44801963
  8    G2 0.97329786 0.6682355 0.69658779
  9    G3 0.68696020 0.7362139 0.77452962
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>test.df
    Group         G1        G2        G3
  1    G1         NA 0.5738143 0.9742835
  2    G2 0.62578294        NA 0.9201009
  3    G3 0.01136336 0.3163722        NA
  4    G1         NA 0.1984567 0.1748822
  5    G2 0.74865266        NA 0.8072535 
  6    G3 0.82855980 0.2668529        NA
  7    G1         NA 0.8861169 0.4480196
  8    G2 0.97329786        NA 0.6965878
  9    G3 0.68696020 0.7362139        NA
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>test.df &lt;- data.frame("Group"=rep(c("G1", "G2", "G3"), 3), "G1"=runif(9, 0, 1), "G2"=runif(9, 0, 1), "G3" = runif(9,0,1))
  
  for (j in 1:ncol(test.df)) {
    for(i in 1:nrow(test.df)) {
      if(colnames(test.df)[j] == test.df$Group[i]) {
        test.df[i,j] &lt;- NA
      }
    }
  }
  </code></pre>
  
  <p>I think I could use dplyr filter to get the values that need to be replaced, replace them, and then reassemble the data frame, but I'm interested in learning other options.</p>