title: R for loop to extract info from a file and add it into tibble?
body: <p>I am not great with tidyverse so forgive me if this is a simple question. I have a bunch of files with data that I need to extract and add into distinct columns in a tibble I created.</p>
  <p>I want the the row names to start with the file IDs which I did manage to create:</p>
  <pre><code>filelist &lt;- list.fileS(pattern=&quot;.txt&quot;) # Gives me the filenames in current directory.
  # The filenames are something like AA1230.report.txt for example
  
  file_ID &lt;- trimws(filelist, whitespace=&quot;\\..*&quot;) # Gives me the ID which is before the &quot;report.txt&quot;
  
  metadata &lt;- as_tibble(file_ID[1:181]) # create dataframe with IDs as row names for 180 files.
  </code></pre>
  <p>Now in these report files are information on species and abundance (kraken report files for those familiar with kraken) and all I need is to extract the number of reads for each domain. I can easily search up in each file the domains and number of reads that fall into that domain using something like:</p>
  <pre><code>sample_data &lt;- as_tibble(read.table(&quot;AA1230.report.txt&quot;, sep=&quot;\t&quot;, header=FALSE, strip.white=TRUE))
  
  sample_data &lt;- rename(sample_data, Percentage=V1, Num_reads_root=V2, Num_reads_taxon=V3, Rank=V4, NCBI_ID=V5, Name=V6) # Just renaming the column headers for clarity
  
  sample_data %&gt;% filter(Rank==&quot;D&quot;) # D for domain
  </code></pre>
  <p>This gives me a clear output such as:</p>
  <pre><code>Percentage Num_Reads_Root Num_Reads_Taxon Rank  NCBI_ID Name     
         &lt;dbl&gt;          &lt;int&gt;           &lt;int&gt; &lt;fct&gt;   &lt;int&gt; &lt;fct&gt;    
  1      75.9           60533              28 D           2 Bacteria 
  2       0.48            386               0 D        2759 Eukaryota
  3       0.01              4               0 D        2157 Archaea  
  4       0.02             19               0 D       10239 Viruses  
  </code></pre>
  <p>Now, I want to just grab the info in the second column and final column and save this info into my tibble so that I can get something like:</p>
  <pre><code>&gt; metadata
  value     Bacteria_Counts    Eukaryota_Counts    Viruses_Counts     Archaea_Counts
  &lt;chr&gt;     &lt;int&gt;              &lt;int&gt;               &lt;int&gt;               &lt;int&gt;
   1 AA1230  60533             386                 19                   4 
   2 AB0566
   3 AA1231
   4 AB0567
   5 BC1148
   6 AW0001
   7 AW0002
   8 BB1121
   9 BC0001
  10 BC0002
  ....with 171 more rows
  </code></pre>
  <p>I'm just having trouble coming up with a for loop to create these sample_data outputs, then from that, extract the info and place into a tibble. I guess my first loop should create these sample_data outputs so something like:</p>
  <pre><code>for (files in file.list()) {
    &gt;&gt; get_domains &lt;&lt;
  }
  </code></pre>
  <p>Then another loop to extract that info from the above loop and insert it into my metadata tibble.
  Any suggestions? Thank you so much!
  PS: If regular dataframes in R is better for this let me know, I have just recently learned that tidyverse is a better way to organize dataframes in R but I have to learn more about it.</p>