首页 > 解决方案 > 在 R 中对逐步模拟进行矢量化

问题描述

我在 R 中编写了一个脚本,在其中模拟进出仓库的库存流动:

set.seed(10)

#Create dataframe
df1 <- data.frame(date = seq(1,20),
                  #Stock in to warehouse on date
                  stockIn = round(10+10*runif(10),0),
                  #Stock out of warehouse on date
                  stockOut = round(10+10*runif(10),0))

#The initial inventory level of the warehouse on date 1
initBalance <- 20

#Create a column of NAs which holds the end of day stock level
df1$endStockBalance <- NA

#Loop through each day
for(i in 1:nrow(df1)){
  #If it's the first day, put initBalance into endStockBalance 
  if(i == 1){
    df1[i,4] <- initBalance
  #For other days, take the maximum of the previous day's inventory plus the difference between stock in and stock out, and 0 (we can't have negative stock levels)
  } else {
    df1[i,4] <- max(df1[i-1,4] + df1[i,2] - df1[i,3],0)
  }
}

这适用于 for 循环,但我想知道是否有通过矢量化它的更优雅的方法,因为这对于小列表来说很好,但对于更大的数量来说会很慢。

我看过使用lagindplyr但由于脚本的逐步性质不起作用。

标签: r

解决方案


您基本上可以将循环更改为

cumsum(c(initBalance, df1$stockIn[-1] - df1$stockOut[-1]))
#[1] 20 17 20 21 18 16 18 18 20 16 14 11 14 15 12 10 12 12 14 10

endStockBalance这与我们在运行for循环后得到的相同

identical(df1$endStockBalance, 
           cumsum(c(initBalance, df1$stockIn[-1] - df1$stockOut[-1])))
#[1] TRUE

如果您想为负数分配 0,您可以使用pmax

pmax(cumsum(c(initBalance, df1$stockIn[-1] - df1$stockOut[-1])), 0)

推荐阅读