title: How to extract data from one file based on the IDs for another
body: <p>I am running code where I take one gene from a gene list, find it's Sentinel gene in Data1, and then select all rows with a matching rssnp1 ID to the gene at it's Sentinel row. However, currently my code runs this at only one gene at a time, I am trying to run it so it will iterate through each gene in my gene list in Data2, creating either 1 file with all outputs in or individual files per gene. </p>
  
  <p>My data looks like this:</p>
  
  <p>Data1 (filename: loci):</p>
  
  <pre><code>Chr  Start   End     rssnp1        Type    gene
  1   1244733 1244734 rs2286773   LD_SNP  ACE
  1   1257536 1257436 rs301159    LD_SNP  CPEB4
  1   1252336 1252336 rs2286773   Sentinel    CPEB4
  1   1252343 1252343 rs301159    LD_SNP  CPEB4
  1   1254841 1254841 rs301159    LD_SNP  CPEB4
  1   1256703 1267404 rs301159    LD_SNP  CPEB4
  1   1269246 1269246 rs301159    LD_SNP  CPEB4
  1   1370168 1370168 rs301159    LD_SNP  GLUPA1
  1   1371824 1371824 rs301159    LD_SNP  GLUPA1
  1   1372591 1372591 rs301159    LD_SNP  GLUPA1
  </code></pre>
  
  <p>Data2 (filename: genelist):</p>
  
  <pre><code>    gene
      CPEB4
      GML
      TBX2
      PNKD
      JMJD1C
      SKI
      MYH11
      ACE
  </code></pre>
  
  <p>I also have a 3rd data file which is the class the gene was predicted as with keras machine learning (a column of labels 0-3 for each gene), and I append this column after extracting the data for the genes at that rssnp1 location.</p>
  
  <p>Data3 (filename: keraspredictions)</p>
  
  <pre><code>    gene        keras.prediction
      CPEB4          2
      GML            1 
      TBX2           1
      PNKD           3
      JMJD1C         0
      SKI            1
      MYH11          2
       ACE           1
  </code></pre>
  
  <p>With this I am trying:</p>
  
  <pre><code>loci &lt;-read.csv('genelocifilter.csv') #Data1
  geneslist &lt;-read.csv('genelist.csv')  #Data2
  keras &lt;-read.csv('Keraspredictions.csv')  #Machine learning prediction
  
  getFromData1 &lt;- function(geneFromData2 = NULL) {
       if (is.null(geneFromData2)) return()
        geneSentinelSNP &lt;- (loci %&gt;% filter(Type == "Sentinel" &amp; gene == geneFromData2))$rssnp1
        loci %&gt;% filter(rssnp1 == geneSentinelSNP)
    } 
  
  gene &lt;- getFromData1(geneFromData2 = "CPEB4")
  #if error here Type == Sentinel needs to be LD_SNP
  
  gene_class &lt;- left_join(gene, keras %&gt;% select(gene, Keras.prediction) , by = "gene")
  
  write.csv(gene_class, "./CPEB4.csv", row.names = FALSE)
  
  </code></pre>
  
  <p>This works to output:</p>
  
  <pre><code>Chr Start   End   rssnp1        Type    gene      Keras.prediction
  1   1244733 1244734 rs2286773   LD_SNP  ACE             1
  
  1   1252336 1252336 rs2286773   Sentinel    CPEB4       2    
  </code></pre>
  
  <p>How can I make this iterate over each gene as opposed to 1 at a time that I am naming manually? I am a beginner so I am not sure where to start with making a for loop for this, any help would be appreciated.</p>