繁体   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