对于R中的循环(寻找替代方法) [英] For Loop in R (Looking for an alternative)

查看:110
本文介绍了对于R中的循环(寻找替代方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code runs a loops but the problem is the speed; it takes several hours to finish and I am looking for an alternative so that I don´t have to wait so long.

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

Basically what the code does the follolling calculations:

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.

所以这些代码的目的是使用已经提到的方法删除数据的峰值.

So the purpose of these code is to remove the peaks of the data using the method already mentioned.

这是代码:

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:

If one runs your code (with smaller number of rows) through a profiler, one sees that the main issue is the rbind in the end, followed by the c mentioned by @Riverarodrigoa:

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

We can focus on these two by creating numeric matrices of suitable size and working with those. Only in the end the final data.frame is created:

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秒.

For 1000 rows this takes about 0.2 s instead of over 4 s. For 10.000 rows I get 2 s instead of 120 s.

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

Obviously, this is not really pretty code. One could do this much nicer using tidyverse or data.table. I just find it worth noting that for loops are not necessarily slow in R. But dynamically growing data structures is.

这篇关于对于R中的循环(寻找替代方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆