Answers.Body: <p>So you have data in "long format" and want to convert it to "wide format". You can use <code>spread()</code> from <code>tidyr</code> for that:</p>
  
  <pre class="lang-r prettyprint-override"><code>require(tidyr)
  # Assuming your input is called `foo`
  spread(foo, Sample, value)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-02T10:22:40.117Z
Answers.LastActivityDate: 2019-05-02T10:22:40.117Z
Answers.UserDisplayName: Devon Ryan
Body: <p>I have called and annotated structural variants and I have a table by name of samples and structural events like below</p>
  
  <pre><code>Sample  Type
  t_005   DEL
  t_005   INS
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DUP
  t_005   DUP
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DUP
  t_005   BND
  t_005   DEL
  t_005   DEL
  t_005   BND
  t_005   DEL
  t_005   DEL
  t_005   DUP
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DEL
  </code></pre>
  
  <p>How can I count the number of events to get the below table?</p>
  
  <pre><code>&gt; head(SV_Counts[,1:4])
                 t_005      o_005       t_013         o_013
      DEL          27          40          25          30
      DUP          11          10          13          22
      BND          23          20          10          12
      TRA           3           2           4           4
      &gt;
  </code></pre>
  
  <p>I tried this from google but I failed to adapt my data to that</p>
  
  <pre><code>    &gt; melt(table(a))
     Sample Type value
  1   o_036  BND   280
  2   o_005  BND     8
  3   o_013  BND   160
  4   o_021  BND   144
  5   o_036  BND     0
  6   t_005  BND    15
  7   t_013  BND   108
  8   o_036  DEL    63
  9   o_005  DEL    15
  10  o_013  DEL   121
  11  o_021  DEL   243
  12  o_036  DEL     0
  13  t_005  DEL    40
  14  t_013  DEL    76
  15  o_036  DUP    34
  16  o_005  DUP     4
  17  o_013  DUP    57
  18  o_021  DUP    88
  19  o_036  DUP     0
  20  t_005  DUP    14
  21  t_013  DUP    48
  22  o_036  INS    58
  23  o_005  INS     0
  24  o_013  INS     4
  25  o_021  INS    10
  26  o_036  INS     1
  27  t_005  INS     1
  28  t_013  INS     2
  </code></pre>
  
  <p>But hereafter I don't know what to do</p>
  
Tags: <r><structural-variation><wgs>
Title: Generate table for total number of SV events per sample
ThreadText: # Generate table for total number of SV events per sample
  
  (Asked by: username_0 on 2019-05-02)
  
  I have called and annotated structural variants and I have a table by name of samples and structural events like below
  
  ```
  Sample  Type
  t_005   DEL
  t_005   INS
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DUP
  t_005   DUP
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DUP
  t_005   BND
  t_005   DEL
  t_005   DEL
  t_005   BND
  t_005   DEL
  t_005   DEL
  t_005   DUP
  t_005   DEL
  t_005   DEL
  t_005   DEL
  t_005   DEL
  
  ```
  
  How can I count the number of events to get the below table?
  
  ```
  > head(SV_Counts[,1:4])
                 t_005      o_005       t_013         o_013
      DEL          27          40          25          30
      DUP          11          10          13          22
      BND          23          20          10          12
      TRA           3           2           4           4
      >
  
  ```
  
  I tried this from google but I failed to adapt my data to that
  
  ```
      > melt(table(a))
     Sample Type value
  1   o_036  BND   280
  2   o_005  BND     8
  3   o_013  BND   160
  4   o_021  BND   144
  5   o_036  BND     0
  6   t_005  BND    15
  7   t_013  BND   108
  8   o_036  DEL    63
  9   o_005  DEL    15
  10  o_013  DEL   121
  11  o_021  DEL   243
  12  o_036  DEL     0
  13  t_005  DEL    40
  14  t_013  DEL    76
  15  o_036  DUP    34
  16  o_005  DUP     4
  17  o_013  DUP    57
  18  o_021  DUP    88
  19  o_036  DUP     0
  20  t_005  DUP    14
  21  t_013  DUP    48
  22  o_036  INS    58
  23  o_005  INS     0
  24  o_013  INS     4
  25  o_021  INS    10
  26  o_036  INS     1
  27  t_005  INS     1
  28  t_013  INS     2
  
  ```
  
  But hereafter I don't know what to do
  
  **Tags:** r, structural-variation, wgs
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-05-02. Score: 2 | Accepted answer
  
  So you have data in "long format" and want to convert it to "wide format". You can use `spread()` from `tidyr` for that:
  
  ```r
  require(tidyr)
  # Assuming your input is called `foo`
  spread(foo, Sample, value)
  
  ```
  
  
  ---
  
  