title: How to overwrite a factor in R
body: <p>I have a dataset:</p>
  
  <pre><code>&gt; k
         EVTYPE FATALITIES INJURIES
  198704   HEAT        583        0
  862634   WIND        158     1150
  68670    WIND        116      785
  148852   WIND        114      597
  355128   HEAT         99        0
  67884    WIND         90     1228
  46309    WIND         75      270
  371112   HEAT         74      135
  230927   HEAT         67        0
  78567    WIND         57      504
  </code></pre>
  
  <p>The variables are as follows. As per the first answer by joran, unused levels can be dropped by <code>droplevels</code>, so no worry about the 898 levels, the illustrative <code>k</code> I'm showing is the <strong>complete</strong> dataset obtained from <code>k &lt;- d1[1:10, 3:4]</code> where <code>d1</code> is the original dataset.</p>
  
  <pre><code>&gt; str(k)
  'data.frame':   10 obs. of  3 variables:
   $ EVTYPE    : Factor w/ 898 levels "   HIGH SURF ADVISORY",..: 243 NA NA NA 243 NA NA 243 243 NA
   $ FATALITIES: num  583 158 116 114 99 90 75 74 67 57
   $ INJURIES  : num  0 1150 785 597 0 ...
  </code></pre>
  
  <p>I'm trying to overwrite the <code>WIND</code> factor:</p>
  
  <pre><code>&gt; k[k$EVTYPE==factor("WIND"), ]$EVTYPE &lt;- factor("AFDAF")
  &gt; k[k$EVTYPE=="WIND", ]$EVTYPE &lt;- factor("AFDAF")
  </code></pre>
  
  <p>But both commands give me error messages: <code>level sets of factors are different</code> or <code>invalid factor level, NA generated</code>.</p>
  
  <p>How should I do this?</p>