title: Vectorization in R
body: <p>I am new to R, and I have researched vectorization. But I am still trying to train my mind to think in vectorization terms. Very often examples of vectorization instead of loops are either too simple, so it's difficult for me to generalize them, or not present at all.</p>
  
  <p>Can anyone suggest how I can vectorize the following?</p>
  
  <pre><code>Model2 &lt;- subset(Cor.RMA, MODEL == Models.Sort[2,1])
  RCM2 &lt;- count(Model2$REPAIR_CODE)
  colnames(RCM2) &lt;- c("REPAIR_CODE", "FREQ")
  M2M &lt;- merge(RCM.Sort, RCM2, by = "REPAIR_CODE", all.x = TRUE)
  M2M.Sort &lt;- M2M[order(M2M$FREQ.x, decreasing = TRUE), ]
  M2M.Sort[is.na(M2M.Sort)] &lt;- 0
  </code></pre>
  
  <p>In the above code, each "2" needs to run from 2 to 85</p>
  
  <pre><code>writeWorksheetToFile(file="CL2 - TC - RC.xlsx", 
                       data = M2M.Sort[ ,c("FREQ.y")], 
                       sheet = "RC by Model", 
                       clearSheets = FALSE,
                       startRow = 6,
                       startCol = 6)
  </code></pre>
  
  <p>In the above code, "data" should from from "M2M..." to "M85M..." and "startCol" should run from 6 to 89 for an Excel printout.</p>
  
  <p>The data frame this comes from (Cor.RMA) has columns "MODEL", "REPAIR_CODE", and others that are unused.
  RCM.Sort is a frequency table of each "REPAIR_CODE" across all models that I use as a Master list to adjoin Device-specific Repair Code counts. (left-join: all.x = TRUE)
  Models.Sort is a frequency table I generated using the "count" function from the plyr package, so I can create subsets for each MODEL.
  Then I merge a list of each "REPAIR_CODE" that I generated using the "unique" function.</p>
  
  <p>Sample Data:</p>
  
  <pre><code>CASE_NO   DEVICE_TYPE   MODEL   TRIAGE_CODE   REPAIR_CODE
  12341     Smartphone    X       TC01          RC01
  12342     Smartphone    Y       TC02          RC02
  12343     Smartphone    Z       TC01, TC05    RC05
  12344     Tablet        AA      TC02          RC37
  12345     Wearable      BB      TC05          RC37
  12346     Smartphone    X       TC07          RC01
  12347     Smartphone    Y       TC04          RC02
  </code></pre>
  
  <p>I very much appreciate your time and effort if you are willing to help.</p>