[R] Eliminating For Loop in Comparison and Replacement

From: Knudson, Anthony Hh (Corvallis-R&D) <knudson_at_hp.com>
Date: Fri 05 May 2006 - 07:37:16 EST


I have two dataframes: (Fails, Refurb) each is identical in structure. The structure has columns consisting of the following: EquipID(Equipment Identification), CustID(Customer Identification), FailTimes(Time Equipment Failed). The for loop below is part of a larger Monte Carlo simulation involving the reliability of equipment and customers.

for (i in 1:length(Fails$CustID)){
  while (j <= length(Refurb$CustID)){
    if (Refurb$FailTimes[j] < Fails$FailTimes[i]){



What this loop does is compare the Fails dataframe to the Refurb dataframe and if the fail time of one of the Refurb items is less than the fail time of the Fails item, the Fails customer gets the Refurb item. The CustReceiver tracks which customer receives a Refurb item, the PrintDonor tracks the Equipment ID that that particular customer will receive. Once a Refurb item is given to a customer it is removed from the Refurb dataframe. The loop then moves on to the next customer to see if a Refurb unit exists for them, and resets the Refurb counter to one.

I've managed to eliminate all other for loops in my simulation except this one. What I would like to do is use Rs indexing capability instead of a for loop to accomplish this. Any thoughts?

Tony Knudson
Reliability Knowledge Center

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri May 05 07:53:31 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 05 May 2006 - 10:10:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.