title: Extract team & member identifiers from the file and extract the variables from the data to run multiple loops
body: <p>I was wondering how I can make the loop that is most efficient when more than one loop is expected.</p>
  <ul>
  <li>The data I have is in a form of: GXXX_Y.csv where XXX represents the
  group identifier (about 80 teams) and Y represents the group member
  (1, 2, 3). <em>To be noted, there are some teams that do not have all 3
  members.</em></li>
  <li>Moreover, there are 10 variables to be selected in each file, to be
  run for each (Step 3 below). They are in forms of: VXX_r</li>
  </ul>
  <p>The following is the code that I plan to run for all groups/group members (example of Group 001 below), then compiling the results into one table for all teams (that is, Step 6). I think the loop needs to work on the identifiers for <strong>Step 1</strong>, <strong>Step 3</strong>, and <strong>Step 6</strong> below.</p>
  <p>I would really appreciate it if you could advise and help me on building efficient loops for this context!</p>
  <pre><code># Step 1. 3 members' dataframes in Group001
  i1 &lt;- fread(&quot;data/G001_1.csv&quot;)
  i2 &lt;- fread(&quot;data/G001_2.csv&quot;)
  i3 &lt;- fread(&quot;data/G001_3.csv&quot;)
  
  # Step 2. Trim the lengths for each member
  trimToLength1 &lt;- floor( nrow(i1) / 60) * 60 
  trimToLength2 &lt;- floor( nrow(i2) / 60) * 60 
  trimToLength3 &lt;- floor( nrow(i3) / 60) * 60 
  
  # Step 3. Select a specific variable one by one (in this case, V01_r) for each member
  x1 &lt;- i1$V01_r[1:(trimToLength1)]
  x2 &lt;- i2$V01_r[1:(trimToLength2)]
  x3 &lt;- i3$V01_r[1:(trimToLength3)]
  
  # Step 4. Run the function of correlations matrix among the members in Group 001
  wcc1 &lt;- calcWCC(x1, x2, maxLag=60, winSize=30, windowInc=30, lagInc=30)
  wcc2 &lt;- calcWCC(x1, x3, maxLag=60, winSize=30, windowInc=30, lagInc=30)
  wcc3 &lt;- calcWCC(x2, x3, maxLag=60, winSize=30, windowInc=30, lagInc=30)
  
  # Step 5. Extract two means from each of the matrix above
  sync1a &lt;- aggWCC(wcc1, method=&quot;mean&quot;)
  sync1b &lt;- aggWCC(wcc1, method=&quot;peak&quot;)
  sync2a &lt;- aggWCC(wcc2, method=&quot;mean&quot;)
  sync2b &lt;- aggWCC(wcc2, method=&quot;peak&quot;)
  sync3a &lt;- aggWCC(wcc3, method=&quot;mean&quot;)
  sync3b &lt;- aggWCC(wcc3, method=&quot;peak&quot;)
  
  # Step 6. Make a table for all teams
  result_table &lt;- data.frame(TeamID=&quot;G001&quot;, From=c(&quot;x1&quot;,&quot;x1&quot;,&quot;x2&quot;), To=c(&quot;x2&quot;,&quot;x3&quot;,&quot;x3&quot;), Mean=c(sync1a,sync2a,sync3a), Peak=c(sync1b,sync2b,sync3b))
  </code></pre>