title: How to convert a variable number of columns per row to multiple rows in R?
body: <p>Here is my problem: I have an individual database: 1 row = 1 person. For each person, there is a unique identifier (&quot;INAMI_key&quot;), individual variables (&quot;code_qualif&quot; in the example below) and one or several addresses filled in through different columns. The number of addresses is indicated in the &quot;n_addresses&quot; variable: 1 for one address, 2 for two addresses, etc. The different addresses are indicated in the variables &quot;travail_ruex&quot; (street) and &quot;travail_code_postalx&quot; (postal code). This is what it looks like:</p>
  <pre><code>   INAMI_key code_qualif n_adresses travail_rue1                       travail_code_postal1 travail_rue2                       travail_code_postal2 travail_rue3                       travail_code_postal3 travail_rue4                       travail_code_postal4 travail_rue5                       travail_code_postal5
   1 30000120  001         02         &quot;RUE VAN ARTEVELDE               &quot; &quot; 1000&quot;              &quot;paul pastur                     &quot; &quot; 6180&quot;              &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                  
   2 30000417  001         01         &quot;av Margueritr depasse           &quot; &quot; 6060&quot;              &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                  
   3 37603435  007         01         &quot;du Grand Veneur                 &quot; &quot; 1170&quot;              &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                  
   4 38300152  007         02         &quot;RUE WAUTERS                92-94&quot; &quot; 6040&quot;              &quot;de châtelet                     &quot; &quot; 6120&quot;              &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                  
   5 38707849  001         03         &quot;de Campine                      &quot; &quot; 4000&quot;              &quot;de Chestret                     &quot; &quot; 4000&quot;              &quot;de la Gare                      &quot; &quot; 4020&quot;              &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                  
   6 38813856  001         03         &quot;Torhoutste steenweg             &quot; &quot; 8400&quot;              &quot;Lage Kaart                      &quot; &quot; 2930&quot;              &quot;De Vrièrestraat                 &quot; &quot; 8301&quot;              &quot;&quot;                                 &quot;&quot;                   &quot;&quot;                                 &quot;&quot;                  
   7 38811084  001         04         &quot;chaussée de Waterloo            &quot; &quot; 1180&quot;              &quot;avenue Napoléon                 &quot; &quot; 1420&quot;              &quot;rue Léon Théodor                &quot; &quot; 1090&quot;              &quot;avenue de la Basilique          &quot; &quot; 1081&quot;              &quot;&quot;                                 &quot;&quot;                  
   8 39105054  001         04         &quot;EMILE  CLAUS                    &quot; &quot; 1050&quot;              &quot;RUE DU FOYER SCHAERBEEKOIS      &quot; &quot; 1030&quot;              &quot;RUE XAVIER DE BUE               &quot; &quot; 1180&quot;              &quot;BV LAMBERMONT                   &quot; &quot; 1030&quot;              &quot;&quot;                                 &quot;&quot;                  
   9 39117031  001         05         &quot;KERKSTRAAT                      &quot; &quot; 3850&quot;              &quot;Pater Richard van de Wouwerstraa&quot; &quot; 3271&quot;              &quot;Wilderenlaan                    &quot; &quot; 3803&quot;              &quot;Gyzevennestraat                 &quot; &quot; 3560&quot;              &quot;Molenveldstraat                 &quot; &quot; 3500&quot;             
  10 31823918  070         05         &quot;Route de l'Etat                 &quot; &quot; 1380&quot;              &quot;Avenue Paul Hymans              &quot; &quot; 1200&quot;              &quot;Avenue WInston Churchill        &quot; &quot; 1180&quot;              &quot;Avenue Winston Churchill        &quot; &quot; 1180&quot;              &quot;avenue hippocrate               &quot; &quot; 1200&quot;             
  </code></pre>
  <p>Here you have the code to import this example in R :</p>
  <pre><code>structure(list(INAMI_key = c(&quot;30000120&quot;, &quot;30000417&quot;, &quot;37603435&quot;, 
  &quot;38300152&quot;, &quot;38707849&quot;, &quot;38813856&quot;, &quot;38811084&quot;, &quot;39105054&quot;, &quot;39117031&quot;, 
  &quot;31823918&quot;), code_qualif = c(&quot;001&quot;, &quot;001&quot;, &quot;007&quot;, &quot;007&quot;, &quot;001&quot;, 
  &quot;001&quot;, &quot;001&quot;, &quot;001&quot;, &quot;001&quot;, &quot;070&quot;), n_adresses = c(&quot;02&quot;, &quot;01&quot;, 
  &quot;01&quot;, &quot;02&quot;, &quot;03&quot;, &quot;03&quot;, &quot;04&quot;, &quot;04&quot;, &quot;05&quot;, &quot;05&quot;), travail_rue1 = c(&quot;RUE VAN ARTEVELDE               &quot;, 
  &quot;av Margueritr depasse           &quot;, &quot;du Grand Veneur                 &quot;, 
  &quot;RUE WAUTERS                92-94&quot;, &quot;de Campine                      &quot;, 
  &quot;Torhoutste steenweg             &quot;, &quot;chaussée de Waterloo            &quot;, 
  &quot;EMILE  CLAUS                    &quot;, &quot;KERKSTRAAT                      &quot;, 
  &quot;Route de l'Etat                 &quot;), travail_code_postal1 = c(&quot; 1000&quot;, 
  &quot; 6060&quot;, &quot; 1170&quot;, &quot; 6040&quot;, &quot; 4000&quot;, &quot; 8400&quot;, &quot; 1180&quot;, &quot; 1050&quot;, 
  &quot; 3850&quot;, &quot; 1380&quot;), travail_rue2 = c(&quot;paul pastur                     &quot;, 
  &quot;&quot;, &quot;&quot;, &quot;de châtelet                     &quot;, &quot;de Chestret                     &quot;, 
  &quot;Lage Kaart                      &quot;, &quot;avenue Napoléon                 &quot;, 
  &quot;RUE DU FOYER SCHAERBEEKOIS      &quot;, &quot;Pater Richard van de Wouwerstraa&quot;, 
  &quot;Avenue Paul Hymans              &quot;), travail_code_postal2 = c(&quot; 6180&quot;, 
  &quot;&quot;, &quot;&quot;, &quot; 6120&quot;, &quot; 4000&quot;, &quot; 2930&quot;, &quot; 1420&quot;, &quot; 1030&quot;, &quot; 3271&quot;, 
  &quot; 1200&quot;), travail_rue3 = c(&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;de la Gare                      &quot;, 
  &quot;De Vrièrestraat                 &quot;, &quot;rue Léon Théodor                &quot;, 
  &quot;RUE XAVIER DE BUE               &quot;, &quot;Wilderenlaan                    &quot;, 
  &quot;Avenue WInston Churchill        &quot;), travail_code_postal3 = c(&quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot; 4020&quot;, &quot; 8301&quot;, &quot; 1090&quot;, &quot; 1180&quot;, &quot; 3803&quot;, &quot; 1180&quot;
  ), travail_rue4 = c(&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;avenue de la Basilique          &quot;, 
  &quot;BV LAMBERMONT                   &quot;, &quot;Gyzevennestraat                 &quot;, 
  &quot;Avenue Winston Churchill        &quot;), travail_code_postal4 = c(&quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot; 1081&quot;, &quot; 1030&quot;, &quot; 3560&quot;, &quot; 1180&quot;), travail_rue5 = c(&quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;Molenveldstraat                 &quot;, 
  &quot;avenue hippocrate               &quot;), travail_code_postal5 = c(&quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot; 3500&quot;, &quot; 1200&quot;)), row.names = c(NA, 
  -10L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p>What I would like to do is multiply the number of rows per individual to show the different addresses on different rows, but in the same fields. For example, if an individual has 3 addresses, create 3 rows for the same individual, keep the individual variables, but reorganize the addresses into columns that have the same name: &quot;travail_rue_total&quot; and &quot;travail_code_postal_total&quot; in the example below. If the individual has 1 adress, create one row, if he has 5 adresses, create 5 rows, etc. :</p>
  <pre><code>   INAMI_key code_qualif n_adresses travail_rue_total                travail_code_postal_total
   1  30000120           1          2 RUE VAN ARTEVELDE                                     1000
   2  30000120           1          2 paul pastur                                           6180
   3  30000417           1          1 av Margueritr depasse                                 6060
   4  37603435           7          1 du Grand Veneur                                       1170
   5  38300152           7          2 RUE WAUTERS                92-94                      6040
   6  38300152           7          2 de châtelet                                           6120
   7  38707849           1          3 de Campine                                            4000
   8  38707849           1          3 de Chestret                                           4000
   9  38707849           1          3 de la Gare                                            4020
  10  38813856           1          3 Torhoutste steenweg                                   8400
  11  38813856           1          3 Lage Kaart                                            2930
  12  38813856           1          3 De Vrièrestraat                                       8301
  13  38811084           1          4 chaussée de Waterloo                                  1180
  14  38811084           1          4 avenue Napoléon                                       1420
  15  38811084           1          4 rue Léon Théodor                                      1090
  16  38811084           1          4 avenue de la Basilique                                1081
  17  39105054           1          4 EMILE  CLAUS                                          1050
  18  39105054           1          4 RUE DU FOYER SCHAERBEEKOIS                            1030
  19  39105054           1          4 RUE XAVIER DE BUE                                     1180
  20  39105054           1          4 BV LAMBERMONT                                         1030
  21  39117031           1          5 KERKSTRAAT                                            3850
  22  39117031           1          5 Pater Richard van de Wouwerstraa                      3271
  23  39117031           1          5 Wilderenlaan                                          3803
  24  39117031           1          5 Gyzevennestraat                                       3560
  25  39117031           1          5 Molenveldstraat                                       3500
  26  31823918          70          5 Route de l'Etat                                       1380
  27  31823918          70          5 Avenue Paul Hymans                                    1200
  28  31823918          70          5 Avenue WInston Churchill                              1180
  29  31823918          70          5 Avenue Winston Churchill                              1180
  30  31823918          70          5 avenue hippocrate                                     1200
  </code></pre>
  <p>That's a simplified version of the data. In the whole database, i have 40 adresses x 15 variables for each (street, number, city, postal code, institution...).</p>
  <p>Thank you !</p>