how to optimize looped searching in R?
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开发者_C百科.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.
Could you give a sample of the data? It is difficult to divine the structure from what you've given. I think plyr might be helpful, but I can't be that much more specific without example data.
The code is not that readable. I understand what you are doing, but it seem unnecessarily verbose. It also appears based on some of the stuff you done to be a bit hacked together particularly the if conditional. There is also quite a bit of duplicate code.
I don't mean to be harsh as I'm sure the code makes perfect sense from your view, Perhaps it'll seem clearer with example data.
精彩评论