r - 在 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 循环,但我想知道是否有通过矢量化它的更优雅的方法,因为这对于小列表来说很好,但对于更大的数量来说会很慢。
我看过使用lag
indplyr
但由于脚本的逐步性质不起作用。
解决方案
您基本上可以将循环更改为
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)
推荐阅读
- cognos-10 - 有没有办法从 Cognos 10 导出已部署的报告?
- flutter - 什么是颤振“网络服务器”?
- php - 根据值将一个字段从一个数组添加到另一个数组
- httpclient - 如何使用 Progress OpenEdge 11.7.3 HTTP 客户端使用安全令牌
- elasticsearch - 根据查询获取内部嵌套对象的相关计数
- javascript - 通过javascript将数据发送到airtable
- c# - Windows UDP 转发到多个端口 (.NET)
- c++ - 我刚刚了解了 C++ 中的动态内存分配
- html - html提交按钮不调用闪亮的服务器
- c# - 无法将数据库类型 char 转换为 String