title: Error - replacement has [x] rows, data has [y]
body: <p>I have a numeric column ("value") in a dataframe ("df"), and I would like to generate a new column ("valueBin") based on "value." I have the following conditional code to define df$valueBin:</p>
  
  <pre><code>df$valueBin[which(df$value&lt;=250)] &lt;- "&lt;=250"
  df$valueBin[which(df$value&gt;250 &amp; df$value&lt;=500)] &lt;- "250-500"
  df$valueBin[which(df$value&gt;500 &amp; df$value&lt;=1000)] &lt;- "500-1,000"
  df$valueBin[which(df$value&gt;1000 &amp; df$value&lt;=2000)] &lt;- "1,000 - 2,000"
  df$valueBin[which(df$value&gt;2000)] &lt;- "&gt;2,000"
  </code></pre>
  
  <p>I'm getting the following error:</p>
  
  <blockquote>
    <p>"Error in <code>$&lt;-.data.frame</code>(<code>*tmp*</code>, "valueBin", value = c(NA, NA, NA,  : 
      replacement has 6530 rows, data has 6532"</p>
  </blockquote>
  
  <p>Every element of <code>df$value</code> should fit into one of my <code>which()</code> statements. There are no missing values in <code>df$value</code>. Although even if I run just the first conditional statement (&lt;=250), I get the exact same error, with <code>"...replacement has 6530 rows..."</code> although there are way fewer than 6530 records with value&lt;=250, and value is never NA.</p>
  
  <p>This SO link notes a similar error when using aggregate() was a bug, but it recommends installing the version of R I have. Plus the bug report says its fixed.
  <a href="https://stackoverflow.com/questions/26353512/r-aggregate-error-replacement-has-foo-rows-data-has-bar">R aggregate error: &quot;replacement has &lt;foo&gt; rows, data has &lt;bar&gt;&quot;</a></p>
  
  <p>This SO link seems more related to my issue, and the issue here was an issue with his/her conditional logic that caused fewer elements of the replacement array to be generated. I guess that must be my issue as well, and figured at first I must have a "&lt;=" instead of an "&lt;" or vice versa, but after checking I'm pretty sure they're all correct to cover every value of "value" without overlaps.
  <a href="https://stackoverflow.com/questions/28614199/r-error-in-data-frame-replacement-has-items-need">R error in &#39;[&lt;-.data.frame&#39;... replacement has # items, need #</a></p>