title: R: Conditional Replacement of values in several columns in data frame
body: <p>I have read several posts on this but they all applied to only changing one column/variable. I need to replace values in a number of columns in a dataframe at once. I thought this should work but it's not and I cannot figure out why.</p>
  
  <pre><code>positive &lt;- c("Yes", "Science")
  temp1 &lt;- c("Yes", "No","","Science", "Only-Child")
  temp2 &lt;- c("Yes", "No",""," Yay people!", "Pessimist")
  temp3 &lt;- cbind(temp1,temp2)
  colnames(temp3) &lt;- c("Feature1","Feature2")
  temp &lt;- as.data.frame(temp3)
  </code></pre>
  
  <p>This does not work:</p>
  
  <pre><code>for (i in temp) {
    ifelse(i %in% positive, 1, i)
  }
  </code></pre>
  
  <p>However, doing it on one column works:</p>
  
  <pre><code>test &lt;- ifelse(temp$Feature1 %in% positive, 1, temp$Feature1)
  test
  </code></pre>
  
  <p>So I suspect the i is not what I want it to be but a check results in what I expected:</p>
  
  <pre><code>for (i in temp) {
    print(i %in% positive)
  }
  </code></pre>
  
  <p>The output should look like this:</p>
  
  <pre><code>  Feature1     Feature2
           1            1
          No           No
  
           1  Yay people!
  Only-Child    Pessimist
  </code></pre>
  
  <p>So what am I missing?</p>