title: How do group values in ranges in a column in R e.g 5-10?
body: <p>I am new to R and I am trying to display data in a data frame such that it is in the form 5-10. Here is the sample data I have:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>count</th>
  <th>percentage</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>0.1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>0.1</td>
  </tr>
  <tr>
  <td>3</td>
  <td>0.3</td>
  </tr>
  <tr>
  <td>5</td>
  <td>0.5</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I would like the output data to look like this</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>count</th>
  <th>percentage</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1-2</td>
  <td>0.1</td>
  </tr>
  <tr>
  <td>3-4</td>
  <td>0.3</td>
  </tr>
  <tr>
  <td>5</td>
  <td>0.5</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>The count column values are of factor type.</p>
  <p>Here is what I have so far but it is not working. I tried to compare two values at a time and then if the difference was not 1, then I wanted to output it as a character as 1-2:</p>
  <pre><code>count &lt;- factor(c(1,1,3,5))
  percentage &lt;- c(0.1, 0.1,0.3,0.5)
  
  info &lt;- data.frame(count, percentage)
  str(info)
  hits &lt;-as.numeric(as.character(info$count))
  hits
  
  for(i in hits){
    #print(i)
    if(hits[i+1] - hits[i] != 1){
      df[i]=as.character(i) + '-' + hits[i+1] - 1
    }
  }
  </code></pre>