title: R group rows based on a value of specific column and keep lower and greater value on others columns
body: <p>I have a dataframe </p>
  
  <pre><code>library(data.table)
  
  test_df=data.frame(chr=c("chr1","chr1","chr1","chr2","chr2","chr1","chr1","chr2","chr1"),
                     start=c(700,800,900,350,400,650,800,200,1000),
                     end=c(750,850,950,400,450,700,850,250,1050),
                     id=c("id_1","id_1","id_1","id_2","id_2","id_1","id_1","id_2","id_3"))
  </code></pre>
  
  <p>I'm trying to groups the rows by ID column.
  For rows which have same values in chr and id, keep the lower value of start column and greater value for end column.</p>
  
  <p>I expect for this dataframe something like :</p>
  
  <pre><code>chr     start     end     id
  chr1     650      950     id_1
  chr1     1000     1050    id_3
  chr2     200      450     id_2
  </code></pre>
  
  <p>I tried this : </p>
  
  <pre><code>final_test_df=setDF(setDT(test_df)[, .(chr=chr[1],start=start[1], end=end[.N]),by=id])
  </code></pre>
  
  <p>and it gives me the first and last value for same id, not the lower/greater values.</p>
  
  <pre><code>id     chr     start     end
  id_1   chr1     700      850     
  id_2   chr2     350      250 
  id_3   chr1     1000     1050    
  </code></pre>