title: Looping over a column and replacing values if condition is met in r
body: <p>I'm still a beginner to R, and I'm trying to figure out how to rewrite my indexing in my loop.</p>
  <p>I have a column <code>party</code> that just lists political party affiliations. I have a loop that I wrote to replace any values that are not either <code>democrat</code> or <code>republican</code> in the column.</p>
  <pre><code>for(i in 1:length(party3$party)){
    if(party3$party[i] != &quot;democrat&quot; || party3$party[i] != &quot;republican&quot;){
    party3$party[i] &lt;- &quot;I&quot; }}
  
  </code></pre>
  <p>My column <code>party</code> looks something like this:</p>
  <pre><code>party
  republican
  independent
  democrat
  libertarian
  independent
  american independent
  republican
  democrat
  </code></pre>
  <p>And I'm trying to keep <code>democrat</code> and <code>republican</code> intact:</p>
  <pre><code>party
  republican
  I
  democrat
  I
  I
  I
  republican
  democrat
  </code></pre>
  <p>Problem is, everything ends up being replaced with &quot;I&quot;. How do I fix this loop's indexing?</p>