簡體   English   中英

優化R中的循環

[英]Optimizing a loop in R

我正在編寫此代碼以遍歷數據並比較值。 這是我的代碼:

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和prob_times_end_new和ob_times是帶有此格式字符串的向量,

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

probs_new只是一個帶有“ PROB30”或“ PROB40”的向量num_of_false30,num_of_false40,num_of_hits30,num_of_hits40是從0開始並根據代碼中的條件進行計數的整數。

我知道這是很多代碼,如果您不了解任何代碼,請提出問題。 這應該做的是搜索向量,並檢查ob_times中是否有任何東西落在開始和結束時間間隔之間,是否命中,如果不是,則為假。

現在,當我運行此代碼時,它可以工作,但是大約需要2分鍾才能完成所有這些工作。 如果我可以使它更快,它將為我節省很多時間。 我看到了一些有關vertorization的帖子,但我自己嘗試這樣做,但很不走運。 如果有人可以幫助我,將不勝感激。 提前致謝

在使用向量之前,請先分配它們。 例如,你有

dates <- c()

替換為

dates <- vector('Date', length)

無論長度如何。 然后,不要串聯日期,而是訪問元素

dates[n] <- value

這將為您帶來最大的收益。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM