title: Collapse columns in a dataframe (R)
body: <p>Basically, I have a dataframe, df</p>
  
  <pre><code>                  Beginning1 Protein2    Protein3    Protein4    Biomarker1
        Pathway3    A         G           NA           NA           F
        Pathway8    Z         G           NA           NA           E
        Pathway9    A         G           Z            H            F
        Pathway6    Y         G           Z            H            E
        Pathway2    A         G           D            NA           F
        Pathway5    Q         G           D            NA           E
        Pathway1    A         D           K            NA           F
        Pathway7    A         B           C            D            F
        Pathway4    V         B           C            D            E
  </code></pre>
  
  <p>And I want to combine the dataframe so that those rows when are identical from "Protein2" to "Protein4" are condense, giving the following:</p>
  
  <pre><code>            Beginning1 Protein2     Protein3     Protein4     Biomarker1
  Pathway3    A,Z         G           NA           NA           F,E
  Pathway9    A,Y         G           Z            H            F,E
  Pathway2    A,Q         G           D            NA           F,E
  Pathway1    A           D           K            NA           F
  Pathway7    A,V         B           C            D            F,E
  </code></pre>
  
  <p>This is very similar to a question that I asked before (<a href="https://stackoverflow.com/questions/44809293/consolidating-duplicate-rows-in-a-dataframe">Consolidating duplicate rows in a dataframe</a>), however the difference is that I am also consolidating the "Beginning1" row.  </p>
  
  <p>So far, I have tried:</p>
  
  <pre><code>library(dat.table)
  dat&lt;-data.table(df)
  
  Total_collapse &lt;- dat[, .(
  Biomarker1 = paste0(Biomarker1, collapse = ", ")),
  by = .(Beginning1, Protein1, Protein2, Protein3)]
  
  Total_collapse &lt;- dat[, .(
  Beginning1 = paste0(Beginning1, collapse = ", ")),
  by = .(Protein1, Protein2, Protein3)]
  </code></pre>
  
  <p>which gives the output:</p>
  
  <pre><code>            Beginning1  Protein2    Protein3      Protein4      Biomarker1
  Pathway3    G           NA           NA           F,E
  Pathway9    G           Z            H            F,E
  Pathway2    G           D            NA           F,E
  Pathway1    D           K            NA           F
  Pathway7    B           C            D            F,E
  </code></pre>
  
  <p>Does anyone know how to fix this problem?  I have also tried duplicating the solution from <a href="https://stackoverflow.com/questions/15933958/collapse-concatenate-aggregate-a-column-to-a-single-comma-separated-string-w">Collapse / concatenate / aggregate a column to a single comma separated string within each group</a>, but have had no success. </p>
  
  <p>I am sorry if it is a simple error- I am pretty new to R.</p>