title: Concatenating variable number of rows in a group to create a single row and cast from long to wide format preferably in dplyr
body: <p>I am using R tidyverse to clean data downloaded from pubmed to get author affiliations from some articles. I have gotten this far and have data that looks like this:</p>
  <pre><code>pubmed_id   variable_id entry
  29542687    PMID    29542687
  29542687    FAU Kato
  29542687    FAU Gregory J
  29542687    AU  Kato GJ
  29542687    AD  Heart
  29542687    AD  Lung and Blood Vascular Medicine Institute and the Division of
  29542687    AD  Hematology-Oncology
  29542687    AD  Department of Medicine
  29542687    AD  University of Pittsburgh
  29542687    AD  200 Lothrop
  29542687    AD  Street
  29542687    AD  Pittsburgh
  29542687    AD  PA 15261
  29542687    AD  USA.
  29542687    FAU Piel
  29542687    FAU Fredrich
  29542687    AU  Piel FB
  29542687    AD  PHE Centre for Environment and Health
  29542687    AD  Department of Epidemiology and
  29542687    AD  Biostatistics
  29542687    AD  School of Public Health
  29542687    AD  Faculty of Medicine
  29542687    AD  Imperial College
  29542687    AD  London
  29542687    AD  London
  29542687    AD  UK.
  29542687    FAU Reid
  29542687    FAU Clarice D
  29542687    AU  Reid CD
  29542687    AD  Sickle Cell Disease Branch
  29542687    AD  National Heart
  29542687    AD  Lung and Blood Institute
  29542687    AD  NIH
  29542687    AD  Bethesda
  29542687    AD  
  29542687    AD  MD
  29542687    AD  USA.
  29542687    FAU Gaston
  29542687    FAU Marilyn H
  29542687    AU  Gaston MH
  29542687    AD  The Gaston and Porter Health Improvement Center
  29542687    AD  Potomac
  29542687    AD  MD
  29542687    AD  USA.
  </code></pre>
  <p>I want to convert this to data that looks like this:</p>
  <pre><code>pubmed_id   full_author         author       address
  29542687    Kato, Gregory J     Kato GJ      Heart Lung and Blood Vascular Medicine Institute and the Division of Hematology-Oncology Department of Medicine University of Pittsburgh 200 Lothrop Street Pittsburgh PA 15261 USA.
  29542687    Piel, Fredrich B    Piel FB      PHE Centre for Environment and Health Department of Epidemiology and Biostatistics School of Public Health Faculty of Medicine Imperial College London London UK.
  29542687    Reid, Clarice D     Reid CD      National Heart Lung and Blood Institute NIH Bethesda MD USA.
  29542687    Gaston, Marilyn H   Gaston MH    The Gaston and Porter Health Improvement Center Potomac MD USA.
  </code></pre>
  <p>Initial thought was to somehow rank each author before combining consecutive rows but struggling to create a ranking that can handle a variable number of AD (address rows). The FAU row is the full author name but is listed on two lines. The pubmed_id field is used to track all the authors that are listed on a single article. Sample data:</p>
  <pre><code>structure(list(pubmed_id = c(29542687L, 29542687L, 29542687L, 
  29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 
  29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 
  29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 
  29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 
  29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 
  29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 29542687L, 
  29542687L, 29542687L, 29542687L, 29542687L, 29542687L), variable_id = c(&quot;PMID&quot;, 
  &quot;FAU&quot;, &quot;FAU&quot;, &quot;AU&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, 
  &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;FAU&quot;, &quot;FAU&quot;, &quot;AU&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, 
  &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;FAU&quot;, &quot;FAU&quot;, &quot;AU&quot;, &quot;AD&quot;, &quot;AD&quot;, 
  &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;, &quot;FAU&quot;, &quot;FAU&quot;, &quot;AU&quot;, &quot;AD&quot;, 
  &quot;AD&quot;, &quot;AD&quot;, &quot;AD&quot;), entry = c(&quot;29542687&quot;, &quot;Kato&quot;, &quot;Gregory J&quot;, 
  &quot;Kato GJ&quot;, &quot;Heart&quot;, &quot;Lung and Blood Vascular Medicine Institute and the Division of&quot;, 
  &quot;Hematology-Oncology&quot;, &quot;Department of Medicine&quot;, &quot;University of Pittsburgh&quot;, 
  &quot;200 Lothrop&quot;, &quot;Street&quot;, &quot;Pittsburgh&quot;, &quot;PA 15261&quot;, &quot;USA.&quot;, &quot;Piel&quot;, 
  &quot;Fredrich&quot;, &quot;Piel FB&quot;, &quot;PHE Centre for Environment and Health&quot;, 
  &quot;Department of Epidemiology and&quot;, &quot;Biostatistics&quot;, &quot;School of Public Health&quot;, 
  &quot;Faculty of Medicine&quot;, &quot;Imperial College&quot;, &quot;London&quot;, &quot;London&quot;, 
  &quot;UK.&quot;, &quot;Reid&quot;, &quot;Clarice D&quot;, &quot;Reid CD&quot;, &quot;Sickle Cell Disease Branch&quot;, 
  &quot;National Heart&quot;, &quot;Lung and Blood Institute&quot;, &quot;NIH&quot;, &quot;Bethesda&quot;, 
  &quot;&quot;, &quot;MD&quot;, &quot;USA.&quot;, &quot;Gaston&quot;, &quot;Marilyn H&quot;, &quot;Gaston MH&quot;, &quot;The Gaston and Porter Health Improvement Center&quot;, 
  &quot;Potomac&quot;, &quot;MD&quot;, &quot;USA.&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -44L))
  </code></pre>
  <p>Greatly appreciate any pointers on cleaning this data. Thanks!</p>