繁体   English   中英

对于R中的循环(寻找替代方法)

[英]For Loop in R (Looking for an alternative)

以下代码运行一个循环,但问题是速度; 它需要几个小时才能完成,我正在寻找替代方案,这样我就不必等待那么长时间。

基本上,该代码执行以下计算:

1.-It calculates the mean of the values of the 60 days.
2.-It gets the standard deviation of the values of the 60 days.
3.-It gets the Max of the values of the 60 days.
4.-It gets the Min of the values of the 60 days.
5.-Then with the previous calculations the code "smooths" the peaks up and down.
6.-Then the code simply get the means from 60, 30, 15 and 7 Days.

因此,这些代码的目的是使用已经提到的方法消除数据的峰值。

这是代码:

options(stringsAsFactors=F)

DAT <- data.frame(ITEM = "x", CLIENT = as.numeric(1:100000), matrix(sample(1:1000, 60, replace=T), ncol=60, nrow=100000, dimnames=list(NULL,paste0('DAY_',1:60))))

DATT <- DAT

nRow <- nrow(DAT)
TMP  <- NULL
for(iROW in 1:nRow){#iROW <- 1
 print(c(iROW,nRow))

  Demand <- NULL
  for(iCOL in 3:ncol(DAT)){#iCOL <- 1
      Demand  <- c(Demand,DAT[iROW,iCOL])

  }

  ww <- which(!is.na(Demand))

  if(length(ww) > 0){
    Average <- round(mean(Demand[ww]),digits=4)
    DesvEst  <- round(sd(Demand,na.rm=T),digits=4)
    Max      <- round(Average + (1 * DesvEst),digits=4)
    Min      <- round(max(Average - (1 * DesvEst), 0),digits=4)
    Demand  <- round(ifelse(is.na(Demand), Demand, ifelse(Demand > Max, Max, ifelse(Demand < Min, Min, Demand))))
    Prom60   <- round(mean(Demand[ww]),digits=4)
    Prom30   <- round(mean(Demand[intersect(ww,(length(Demand) - 29):length(Demand))]),digits=4)
    Prom15   <- round(mean(Demand[intersect(ww,(length(Demand) - 14):length(Demand))]),digits=4)
    Prom07   <- round(mean(Demand[intersect(ww,(length(Demand) - 6):length(Demand))]),digits=4)

  }else{
    Average <- DesvEst <- Max <- Min <- Prom60 <- Prom30 <- Prom15 <- Prom07 <- NA

  }

  DAT[iROW,3:ncol(DAT)] <- Demand
  TMP <- rbind(TMP, cbind(DAT[iROW,], Average, DesvEst, Max, Min, Prom60, Prom30, Prom15, Prom07))
}
DAT <- TMP

如果一个人通过分析器运行您的代码(行数较少),则会发现主要问题是最后的rbind ,然后是@Riverarodrigoa提到的c

我们可以通过创建适当大小的数字矩阵并使用它们来集中精力处理这两个问题。 仅最后创建最终的data.frame

options(stringsAsFactors=F)
N <- 1000
set.seed(42)
DAT <- data.frame(ITEM = "x", 
                  CLIENT = as.numeric(1:N), 
                  matrix(sample(1:1000, 60, replace=T), ncol=60, nrow=N, dimnames=list(NULL,paste0('DAY_',1:60))))

nRow <- nrow(DAT)
TMP  <- matrix(0, ncol = 8, nrow = N,  
               dimnames = list(NULL, c("Average", "DesvEst", "Max", "Min", "Prom60", "Prom30", "Prom15", "Prom07")))
DemandMat <- as.matrix(DAT[,3:ncol(DAT)])

for(iROW in 1:nRow){
  Demand <- DemandMat[iROW, ]

  ww <- which(!is.na(Demand))

  if(length(ww) > 0){
    Average <- round(mean(Demand[ww]),digits=4)
    DesvEst  <- round(sd(Demand,na.rm=T),digits=4)
    Max      <- round(Average + (1 * DesvEst),digits=4)
    Min      <- round(max(Average - (1 * DesvEst), 0),digits=4)
    Demand  <- round(ifelse(is.na(Demand), Demand, ifelse(Demand > Max, Max, ifelse(Demand < Min, Min, Demand))))
    Prom60   <- round(mean(Demand[ww]),digits=4)
    Prom30   <- round(mean(Demand[intersect(ww,(length(Demand) - 29):length(Demand))]),digits=4)
    Prom15   <- round(mean(Demand[intersect(ww,(length(Demand) - 14):length(Demand))]),digits=4)
    Prom07   <- round(mean(Demand[intersect(ww,(length(Demand) - 6):length(Demand))]),digits=4)

  }else{
    Average <- DesvEst <- Max <- Min <- Prom60 <- Prom30 <- Prom15 <- Prom07 <- NA

  }
  DemandMat[iROW, ] <- Demand 
  TMP[iROW, ] <- c(Average, DesvEst, Max, Min, Prom60, Prom30, Prom15, Prom07)
}
DAT <- cbind(DAT[,1:2], DemandMat, TMP)

对于1000行,这大约需要0.2 s,而不是4 s。 对于10.000行,我得到2秒而不是120秒。

显然,这不是很漂亮的代码。 使用tidyversedata.table 我只是发现值得注意的是,R中的for循环不一定很慢。但是动态增长的数据结构却很慢。

暂无
暂无

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

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