title: R: Avoid repeating lines of code using R subsets in scripts
body: <p>I'm very new to R - but have been developing SAS-programs (and VBA) for some years. Well, the thing is that I have 4 lines of R-code (scripts?) that I would like to repeat 44 times. Two times for each of 22 different train stations, indicating whether the train is in- or out-going. The four lines of code are:</p>
  
  <pre><code>dataGL_FLIin &lt;- subset( dataGL_all, select = c(Tidsinterval, Dag, M.ned, Ugenr.,Kode, Ugedag, FLIin))
  names(dataGL_FLIin)[names(dataGL_FLIin)=='FLIin'] &lt;- 'GL_Antal'
  dataGL_FLIin$DIR&lt;-"IN"
  dataGL_FLIin$STATION&lt;-"FLI    
  </code></pre>
  
  <p>To avoid repeating the 4 lines 44 times I need 2 "macro variables" (yes, I'm aware, that this is a SAS-thing only, sorry). One "macro variable" indicating the train station and one indicating the direction. In the example above the train station is <strong>FLI</strong> and the direction is <strong>in</strong>. Below  the same 4 lines are demonstrated for the train station <strong>FBE</strong>, this time in <strong>out</strong>-going direction.</p>
  
  <pre><code>dataGL_FBEout &lt;- subset( dataGL_all, select = c(Tidsinterval, Dag, M.ned, Ugenr.,Kode, Ugedag, FBEout))
  names(dataGL_FBEout)[names(dataGL_FBEout)=='FBEout'] &lt;- 'GL_Antal'
  dataGL_FBEout$DIR&lt;-"OUT"
  dataGL_FBEout$STATION&lt;-"FBE"
  </code></pre>
  
  <p>I have looked many places and tried many combinations of R-functions and R-lists, but I can't make it work. Quite possible I'm getting it all wrong. I apologize in advance if the question is (too) stupid, but will however be very grateful for any help on the matter.</p>
  
  <p>Pls. notice that I, in the end, want 44 different data-frames created:
  1) dataGL_FLIin 
  2) dataGL_FBEout
  3) Etc. ...</p>
  
  <p>ADDED: 2 STATION 2 DIRECTIONS EXAMPLE OF MY PROBLEM</p>
  
  <pre><code>'The one data frame I have'
  Date&lt;-c("01-01-15 04:00","01-01-15 04:20","01-01-15 04:40")
  FLIin&lt;-c(96,39,72)
  FLIout&lt;-c(173,147,103)
  FBEin&lt;-c(96,116,166)
  FBEout&lt;-c(32,53,120)
  dataGL_all&lt;-data.frame(Date, FLIin, FLIout, FBEin, FBEout)
  
  'The four data frames I would like'
  GL_antal&lt;-c(96,39,72)
  Station&lt;-("FLI")
  Dir&lt;-("IN")
  dataGL_FLIin&lt;-data.frame(Date, Station, Dir, GL_antal)
  
  GL_antal&lt;-c(173,147,103)
  Station&lt;-("FLI")
  Dir&lt;-("OUT")
  dataGL_FLIout&lt;-data.frame(Date, Station, Dir, GL_antal)
  
  GL_antal&lt;-c(96,116,166)
  Station&lt;-("FBE")
  Dir&lt;-("IN")
  dataGL_FBEin&lt;-data.frame(Date, Station, Dir, GL_antal)
  
  GL_antal&lt;-c(32,53,120)
  Station&lt;-("FBE")
  Dir&lt;-("OUT")
  dataGL_FBEout&lt;-data.frame(Date, Station, Dir, GL_antal)
  </code></pre>
  
  <p>Thanks, 
  lars</p>