title: Using R to Bin Data into Quartiles
body: <p>I am write a very simple if else loop to create a new variable that bins another variable into quartiles. This seems to be a very simple procedure, however the loop groups all of my data into the median and third quartile (which violates the definition of a quartile). </p>
  
  <p>Here is the structure of my data: </p>
  
  <pre><code>&gt; str(tmp)
  Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   435 obs. of  12 variables:
   $ CD112FP             : chr  "01" "02" "03" "04" ...
   $ State               : chr  "ALABAMA" "ALABAMA" "ALABAMA" "ALABAMA" ...
   $ Year                : num  2011 2011 2011 2011 2011 ...
   $ Alignment           : num  0 0 0 0 0 0 1 0 0 0 ...
   $ State_Aligned       : num  0 0 0 0 0 0 0 1 0 0 ...
   $ PercentFunding      : num  0.0658 0.29 0.6764 0.0174 0.047 ...
   $ fips                : chr  "01" "01" "01" "01" ...
   $ ssa                 : int  1 1 1 1 1 1 1 NA 3 3 ...
   $ region              : int  3 3 3 3 3 3 3 NA 4 4 ...
   $ division            : int  6 6 6 6 6 6 6 NA 8 8 ...
   $ abb                 : chr  "AL" "AL" "AL" "AL" ...
   $ PercentFundingBinned: chr  "0.0625-0.1799" "0.0625-0.1799" "0.0625-0.1799" "0.0625-0.1799" ...
  </code></pre>
  
  <p>and this is the head of my data:</p>
  
  <pre><code> head(tmp)
  # A tibble: 6 x 12
    CD112FP State    Year Alignment State_Aligned PercentFunding fips    ssa region division abb   PercentFundingBinned
    &lt;chr&gt;   &lt;chr&gt;   &lt;dbl&gt;     &lt;dbl&gt;         &lt;dbl&gt;          &lt;dbl&gt; &lt;chr&gt; &lt;int&gt;  &lt;int&gt;    &lt;int&gt; &lt;chr&gt; &lt;chr&gt;               
  1 01      ALABAMA  2011         0             0         0.0658 01        1      3        6 AL    0.0625-0.1799       
  2 02      ALABAMA  2011         0             0         0.290  01        1      3        6 AL    0.0625-0.1799       
  3 03      ALABAMA  2011         0             0         0.676  01        1      3        6 AL    0.0625-0.1799       
  4 04      ALABAMA  2011         0             0         0.0174 01        1      3        6 AL    0.0625-0.1799       
  5 05      ALABAMA  2011         0             0         0.0470 01        1      3        6 AL    0.0625-0.1799       
  6 06      ALABAMA  2011         0             0         0.0440 01        1      3        6 AL    0.0625-0.1799       
  </code></pre>
  
  <p>I am using the following if else loop:</p>
  
  <pre><code>  tmp$PercentFundingBinned &lt;- NULL
    if (tmp$PercentFunding &gt;= quantile(tmp$PercentFunding, 0.75)) {
      tmp$PercentFundingBinned &lt;- paste0(round(quantile(tmp$PercentFunding, 0.75), 4), "-",
                                         round(max(tmp$PercentFundingBinned), 4))
    } else if (tmp$PercentFunding &gt;= median(tmp$PercentFunding)){
      tmp$PercentFundingBinned &lt;- paste0(round(median(tmp$PercentFunding),4), "-", 
                                         round(quantile(tmp$PercentFunding, 0.75),4))
    } else if (tmp$PercentFunding &gt;= quantile(tmp$PercentFunding, 0.25)){
      tmp$PercentFundingBinned &lt;- paste0(round(quantile(tmp$PercentFunding, 0.25),4), "-", 
                                         round(median(tmp$PercentFunding),4))
    } else {
      tmp$PercentFundingBinned &lt;- paste0(round(min(tmp$PercentFunding),4), "-", 
                                               round(quantile(tmp$PercentFunding, 0.25),4))
    }
  </code></pre>
  
  <p>and it returns the following category:</p>
  
  <pre><code>unique(tmp$PercentFundingBinned)
  [1] "0.0625-0.1799"
  </code></pre>
  
  <p>Not sure what to do or how to fit it. This seems like its should be a really easy procedure. Any advice helps, thank you! </p>