title: Selecting a range of values to group together
body: <p>I have a column of hours, and I need to group it up into a new column.  </p>
  
  <pre><code>structure(list(Q11aWalkHoursEdit.SS = c(0, 1, 2, 3, 4, 5, 6, 
  6, 7, 2.5, 6, 1.9, 0.5)), 
  .Names = "Q11aWalkHoursEdit.SS", 
  class = "data.frame", 
  row.names = c(NA, -13L))
  </code></pre>
  
  <p>I initially thought I'd use if, as follows:</p>
  
  <pre><code>grouphours &lt;- function(data){
    data$Q11aWalkHours.SS &lt;- NA
    if(data["Q11aWalkHoursEdit.SS"] &gt;= 5){
      data["Q11aWalkHours.SS"] = "5 hours +"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 4){
      data["Q11aWalkHours.SS"] = "4-5 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 3){
      data["Q11aWalkHours.SS"] = "3-4 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 2){
      data["Q11aWalkHours.SS"] = "2-3 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 1){
      data["Q11aWalkHours.SS"] = "1-2 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 0){
      data["Q11aWalkHours.SS"] = "0-1 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] == 0){
      data["Q11aWalkHours.SS"] = "0 hours"
    } else {
      data["Q11aWalkHours.SS"] = NA
    }
    return(data)    
  }
  test &lt;- grouphours(stuff)
  </code></pre>
  
  <p>But this doesn't work, because if doesn't work on vectors.  It gives the following error:</p>
  
  <pre><code>1: In if (data["Q11aWalkHoursEdit.SS"] &gt;= 5) { ... :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>I then started writing a for loop, which also doesn't work, and I don't really understand why:</p>
  
  <pre><code>grouphours &lt;- function(data){
    data$Q11aWalkHours.SS &lt;- NA
    l&lt;-length(stuff$Q11aWalkHoursEdit.SS)
    for(i in 1:l){
    if(data["Q11aWalkHoursEdit.SS"] &gt;= 5){
      data["Q11aWalkHours.SS"] = "5 hours +"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 4){
      data["Q11aWalkHours.SS"] = "4-5 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 3){
      data["Q11aWalkHours.SS"] = "3-4 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 2){
      data["Q11aWalkHours.SS"] = "2-3 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 1){
      data["Q11aWalkHours.SS"] = "1-2 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] &gt; 0){
      data["Q11aWalkHours.SS"] = "0-1 hours"
    } else if(data["Q11aWalkHoursEdit.SS"] == 0){
      data["Q11aWalkHours.SS"] = "0 hours"
    } else {
      data["Q11aWalkHours.SS"] = NA
    }
    }
    return(data)    
  }
  test &lt;- grouphours(stuff)
  </code></pre>
  
  <p>Regardless, I feel like I shouldn't need to use a loop - based on comments in this question:
  <a href="https://stackoverflow.com/questions/4367177/warning-the-condition-has-length-1-and-only-the-first-element-will-be-used">Warning &quot;the condition has length &gt; 1 and only the first element will be used&quot;</a>
  But I'm having trouble understanding how to apply those responses to my situation.</p>
  
  <p>EDIT:</p>
  
  <p>Thank you for helping everyone.  I used the following code which made it work perfectly.  I think <code>cut</code> would be a 'better' solution, but I don't understand it yet so will use ifelse for now.</p>
  
  <pre><code>stuff$test &lt;- ifelse(stuff$Q11aWalkHoursEdit.SS&gt;=5, "Five +", 
                ifelse (stuff$Q11aWalkHoursEdit.SS&gt;=4, "Four to five", 
                        ifelse (stuff$Q11aWalkHoursEdit.SS&gt;=3, "Three to four", 
                                ifelse (stuff$Q11aWalkHoursEdit.SS&gt;=2, "Two to three", 
                                        ifelse (stuff$Q11aWalkHoursEdit.SS&gt;=1, "One to two", 
                                                ifelse(stuff$Q11aWalkHoursEdit.SS&gt;0, "0 to one", "Zero")
                                        )
                                )
                        )
                )
  )
  </code></pre>