简体   繁体   English

优化R中的循环

[英]Optimizing a loop in R

I am writing this code to go through data and compare values. 我正在编写此代码以遍历数据并比较值。 This is my code: 这是我的代码:

for (t in 1:(length(prob_times_start_new))){
count3 <- 0
testcount <- 0
dates <- c()
count2 <- 0
for(n in 1:length(ob_times)){
    issue <- substr(prob_times_start_new[t],1,10)
    issue2 <- substr(prob_times_end_new[t],1,10)
    count2 <- count2 + 1
    if (grepl(issue,ob_times[n])|grepl(issue2,ob_times[n])){
        if ((ob_times[n] >= prob_times_start_new[t]) & (ob_times[n] <= prob_times_end_new[t])){
            count3 <- count3 + 1}
        if ((ob_times[n] >= prob_times_start_new[t]) & (ob_times[n] <= prob_times_end_new[t]) & (count3 <= 1)){

            if (probs_new[t] == "PROB30"){
                num_of_hits30 <- num_of_hits30 + 1}
            else if (probs_new[t] == "PROB40"){
                num_of_hits40 <- num_of_hits40 + 1}
            }
        if ((ob_times[n]<prob_times_start_new[t]) | (ob_times[n] > prob_times_end_new[t])){
            testcount <- testcount + 1}
        dates <- c(dates,ob_times[n])
        }

    nums <- length(ob_times)
    if ((!(grepl(issue,ob_times[nums])))&(!(grepl(issue2,ob_times[1])))){

        if (((prob_times_start_new[t]>ob_times[nums])|(prob_times_end_new[t]<ob_times[1]))&count2<=1){

            if (probs_new[t] == "PROB30"){
                num_of_false30 <- num_of_false30 + 1}
            else if (probs_new[t] == "PROB40"){
                num_of_false40 <- num_of_false40 + 1}}}}
if((!(is.null(dates)))){
    if((testcount==length(dates))){

        if (probs_new[t] == "PROB30"){
            num_of_false30 <- num_of_false30 + 1}
        else if (probs_new[t] == "PROB40"){
            num_of_false40 <- num_of_false40 + 1}}}


for (k in 2:length(ob_times)){
    if(((!(grepl(issue,ob_times[k])))&(!(grepl(issue2,ob_times[k]))))&((!(grepl(issue,ob_times[k-1]))) & (!(grepl(issue,ob_times[k-1]))))){
        if ((prob_times_start_new[t]>ob_times[k-1]) & (prob_times_start_new[t]<ob_times[k]) & (prob_times_end_new[t]>ob_times[k-1]) & (prob_times_end_new[t]<ob_times[k])){

            if (probs_new[t] == "PROB30"){
                num_of_false30 <- num_of_false30 + 1}
            else if (probs_new[t] == "PROB40"){
                num_of_false40 <- num_of_false40 + 1}}}}}

prob_times_start_new and prob_times_end_new and ob_times are vectors with strings in this format, prob_times_start_new和prob_times_end_new和ob_times是带有此格式字符串的向量,

"2010-03-12 22:12:20" (Year-Month-Day Hour:Minute:Second)

probs_new is just a vector with either "PROB30" or "PROB40" num_of_false30, num_of_false40, num_of_hits30, num_of_hits40 are integers starting off at 0 and counting according to the criteria in the code. probs_new只是一个带有“ PROB30”或“ PROB40”的向量num_of_false30,num_of_false40,num_of_hits30,num_of_hits40是从0开始并根据代码中的条件进行计数的整数。

I know this is a lot of code and ask questions if you don't understand any of the code. 我知道这是很多代码,如果您不了解任何代码,请提出问题。 What this is supposed to do is search through a vector and check if anything in ob_times falls between the start and end time interval, if it does it is a hit and if not it is a false. 这应该做的是搜索向量,并检查ob_times中是否有任何东西落在开始和结束时间间隔之间,是否命中,如果不是,则为假。

Right now when I run this code it works but it takes about 2 minutes to do all of this. 现在,当我运行此代码时,它可以工作,但是大约需要2分钟才能完成所有这些工作。 It would save me a lot of time if I could get this to be faster. 如果我可以使它更快,它将为我节省很多时间。 I saw some posts about vertorization but I tried doing that myself but was out of luck. 我看到了一些有关vertorization的帖子,但我自己尝试这样做,但很不走运。 If someone could help me out it would be greatly appreciated. 如果有人可以帮助我,将不胜感激。 Thanks in advance 提前致谢

Allocate the vectors before using them. 在使用向量之前,请先分配它们。 For example, you have 例如,你有

dates <- c()

Replace that with 替换为

dates <- vector('Date', length)

for whatever the length may be. 无论长度如何。 Then, instead of concatenating the dates, access the element 然后,不要串联日期,而是访问元素

dates[n] <- value

This will give you the most bang for your buck. 这将为您带来最大的收益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM