Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于一个值计算R中以下行的总和_R_Sum_Rows - Fatal编程技术网

基于一个值计算R中以下行的总和

基于一个值计算R中以下行的总和,r,sum,rows,R,Sum,Rows,我有一个显示的数据库,并希望根据一列中的一个值计算以下行的总和 例如: 如果“安全库存=2个月”,则计算出的单位安全库存为同一产品下2行的总和 如果“安全库存=3个月”,则计算出的单位安全库存为同一产品的以下3行之和 有人知道在R区怎么走吗 假设“安全库存”是每个“产品”的单个值,我们可以使用数据。表。将'data.frame'转换为'data.table'(setDT(df1)),按'Product'分组,通过将'n'指定为'Safety_Stocks'的第一个元素的顺序,获得lead值,

我有一个显示的数据库,并希望根据一列中的一个值计算以下行的总和

例如:

  • 如果“安全库存=2个月”,则计算出的单位安全库存为同一产品下2行的总和
  • 如果“安全库存=3个月”,则计算出的单位安全库存为同一产品的以下3行之和
有人知道在R区怎么走吗


假设“安全库存”是每个“产品”的单个值,我们可以使用
数据。表
。将'data.frame'转换为'data.table'(
setDT(df1)
),按'Product'分组,通过将'n'指定为'Safety_Stocks'的第一个元素的顺序,获得
lead
值,并分配(
:=
)输出到新列(“new_SafetyStocks”)


请添加一个可复制的示例,以便其他人可以帮助您。例如,您可以使用R的
dput()
函数将数据导出为文本,并将其添加到您的问题中。谢谢!我得到下面的错误消息:>库(data.table)中的错误(data.table):没有名为'data.table'>setDT(df1)[,New\u SafetyStocks:=Reduce(
++
,shift(Sales\u Forecast,+type=“lead”,n=seq(Safety\u Stocks[1L]),by=Product]的包错误:找不到函数“setDT。你知道为什么吗?@Nico好像你没有安装这个软件包,试试
install.packages(“data.table”);库(data.table)
和代码
library(data.table)
setDT(df1)[, New_SafetyStocks := Reduce(`+`, shift(Sales_Forecast, 
           type="lead", n=seq(Safety_Stocks[1L]))) , by = Product]
df1
#      Product Sales_Forecast Safety_Stocks New_SafetyStocks
# 1: Product A            100             2              300
# 2: Product A            100             2              300
# 3: Product A            200             2              400
# 4: Product A            100             2              600
# 5: Product A            300             2              400
# 6: Product A            300             2              200
# 7: Product A            100             2              300
# 8: Product A            100             2              500
# 9: Product A            200             2              400
#10: Product A            300             2              200
#11: Product A            100             2               NA
#12: Product A            100             2               NA
#13: Product B            500             3             2000
#14: Product B            500             3             3000
#15: Product B           1000             3             3500
#16: Product B            500             3             3500
#17: Product B           1500             3             2500
#18: Product B           1500             3             2000
#19: Product B            500             3             3000
#20: Product B            500             3             3000
#21: Product B           1000             3             2500
#22: Product B           1500             3               NA
#23: Product B            500             3               NA
#24: Product B            500             3               NA
   
df1 <- data.frame(Product = rep(c("Product A", "Product B"), each = 12),
       Sales_Forecast = c(100, 100, 200, 100, 300, 300, 100, 100, 200, 300,
    100, 100, 500, 500, 1000, 500, 1500, 1500, 500, 500, 1000, 1500, 500, 500),
   Safety_Stocks = rep(c(2,3), each = 12))