title: Faster way to summarise variables based on column
body: <p>I wish to summarise some variables in my data frame based on a column. However my data frame is rather large (>30,000,000 rows) and using the summarise function in dplyr takes ages to run. Is there a faster way in R to speed up the summarising process? </p>
  
  <p>I have a data frame df in the format:</p>
  
  <pre><code>  proid  X1  X2  X3 X4
  1     1  zz   a   e  a
  2     2  ff   g   z  b
  3     1  cd   s   f  d 
  4     3  ab   t   e  e
  5     2  ta   b   h  k
        ....
  </code></pre>
  
  <p>I wish to concatenate the variables X1 to X4 when they have the same prodid value. The concatenated strings are separated by commas. So the above table should give me the new table:</p>
  
  <pre><code>  proid     X1   X2   X3  X4
  1     1  zz,cd  a,s  e,f a,d 
  2     2  ff,ta  g,b  z,h b,k
  3     3     ab    t    e   e
        ....
  </code></pre>
  
  <p>I have used the following dplyr code:</p>
  
  <pre><code>concat &lt;- function(x){
    x &lt;- na.omit(x)
    if(length(x)==0){
      return(as.character(NA))
    }else{
      return(paste(x,collapse=","))
    }
  }
  
  dg&lt;-group_by(df,proid)
  df&lt;-summarise(dg,proid,concat(X1),concat(X2),concat(X3),concat(X4))
  </code></pre>