title: How can I separate the df from a list
body: <p>I have a list of df lst1. I would like to generate an excel file for each subject ID and each file will contain mutiple sheets, which equal to the df in lst. And at the end, I would like to add label on top the col names from dfs in lst.</p>
  <pre><code>lst&lt;-list(Demographics = structure(list(SubjectID = c(&quot;101-01-101&quot;, 
  &quot;101-02-102&quot;, &quot;101-03-103&quot;, &quot;101-04-104&quot;, &quot;104-05-201&quot;), BRTHDTC = c(&quot;1953-07-07&quot;, 
  &quot;1963-07-02&quot;, &quot;1940-09-11&quot;, &quot;1955-12-31&quot;, &quot;1950-12-04&quot;), SEX = c(&quot;Female&quot;, 
  &quot;Female&quot;, &quot;Male&quot;, &quot;Male&quot;, &quot;Female&quot;)), row.names = c(NA, -5L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;)), DiseaseStatus = structure(list(SubjectID = c(&quot;101-01-101&quot;, 
  &quot;101-02-102&quot;, &quot;101-03-103&quot;, &quot;101-04-104&quot;, &quot;104-05-201&quot;), DSDT = c(&quot;2016-03-14&quot;, 
  &quot;2017-04-04&quot;, NA, &quot;2016-05-02&quot;, &quot;2018-07-06&quot;), DSDT_P = c(NA, 
  NA, &quot;UN-UNK-2015&quot;, NA, NA)), row.names = c(NA, -5L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;)), Visits = structure(list(SubjectID = c(&quot;101-01-101&quot;, 
  &quot;101-02-102&quot;, &quot;101-03-103&quot;, &quot;101-04-104&quot;, &quot;104-05-201&quot;), Visit = c(&quot;Screening: -28 Days to Day 1&quot;, 
  &quot;Screening: -28 Days to Day 1&quot;, &quot;Screening: -28 Days to Day 1&quot;, 
  &quot;Screening: -28 Days to Day 1&quot;, &quot;Screening: -28 Days to Day 1&quot;
  ), VISND = c(NA_character_, NA_character_, NA_character_, NA_character_, 
  NA_character_)), row.names = c(NA, -5L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;)))
  
  Label&lt;-structure(list(Var = c(&quot;SubjectID&quot;, &quot;BRTHDTC&quot;, &quot;SEX&quot;, &quot;DSDT&quot;, 
  &quot;DSDT_P&quot;, &quot;Visit&quot;, &quot;VISND&quot;), label = c(&quot;Subject ID&quot;, &quot;Birthday&quot;, 
  &quot;Gender&quot;, &quot;DS Date&quot;, &quot;DS Date Prob&quot;, &quot;Date of Visit&quot;, &quot;ND Visit&quot;
  )), row.names = c(NA, -7L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  
  IDlist = c(&quot;101-01-101&quot;, 
  &quot;101-02-102&quot;, &quot;101-03-103&quot;, &quot;101-04-104&quot;, &quot;104-05-201&quot;)
  
  </code></pre>
  <p>The outcome should looks like sth as</p>
  <pre><code>out
  $Demographics
  # A tibble: 2 x 3
    SubjectID  BRTHDTC    SEX   
    &lt;chr&gt;      &lt;chr&gt;      &lt;chr&gt; 
  1 Subject ID Birthday   Gender
  2 101-01-101 1953-07-07 Female
  
  
  $DiseaseStatus
  # A tibble: 2 x 3
    SubjectID  DSDT       DSDT_P      
    &lt;chr&gt;      &lt;chr&gt;      &lt;chr&gt;       
  1 Subject ID DS Date    DS Date Prob
  2 101-01-101 2016-03-14 &lt;NA&gt;        
         
  
  $Visits
  # A tibble: 2 x 3
    SubjectID  Visit                        VISND   
    &lt;chr&gt;      &lt;chr&gt;                        &lt;chr&gt;   
  1 Subject ID Date of Visit                ND Visit
  2 101-01-101 Screening: -28 Days to Day 1 &lt;NA&gt;    
  </code></pre>
  <p>The code I have so is :</p>
  <pre><code>Indivadual_xlsx&lt;-function(Subject_id){
  lst %&gt;% map(~ {nm1 &lt;- deframe(Label)[names(.x)];  filter(., SubjectID == Subject_id) %&gt;% 
      bind_rows(as.list(setNames(names(nm1), nm1)), setNames(., nm1))  })%&gt;%
       write.xlsx(paste0(Subject_id, &quot;.xlsx&quot;))
  }
  IDlist %&gt;% map(Indivadual_xlsx) 
  </code></pre>