I am trying to scan a dataset using a loop in R, to see if the data points in the subset of data fulfill some rules, an example is pasted here:
loop.ward <- 1
loop.control.chart <- 1
while (loop.ward <= length(unique(control.chart[,"ward"])))
{
loop.weekly.count <- 3
while (loop.weekly.count <= nrow(control.chart[control.chart[,"ward"]==unique(control.chart[,"ward"])[loop.ward] ,]))
{
temp <- control.chart[control.chart[,"ward"]==unique(control.chart[,"ward"])[loop.ward] ,][(loop.weekly.count-2):(loop.weekly.count),]
if (nrow(temp[temp["UWL.out"]==1,])>=2 | nrow(temp[temp["LWL.out"]==1,])>=2)
{
control.chart[control.chart[,"ward"]==unique(control.chart[,"ward"])[loop.ward],][(loop.weekly.count),"rules.violated"] <- 2
}
loop.control.chart <- loop.control.chart + 1
loop.weekly.count <- loop.weekly.count + 1
}
loop.ward <- loop.ward + 1
}
The code is doing what I need, but in a very slow way (I have around 7000 data points total for all wards (i.e. max(loop.control.chart) = 7000), and 4 rules to do the scanning, it took me 10 minutes to finished one round.
I can't think of anyway of optimizing it (I thought of tapply, but don't know how to implement it), any suggestions?
Thanks!
(And one more trivial question, is the code here readable to you?)
Update
A portion of the data is attached here for your reference.