[英]How to create a new column that consecutively sums from another column in R?
[英]R help - how to calculate a new column that sums a column from a different table filtered by two dates?
我有两个 tbls。
工作订单:
# A tibble: 6 x 6
cleaningindex wonumber createddate nextdate
<chr> <int> <date> <date>
1 1 2093 2017-01-11 2017-02-09
2 2 2514 2017-02-09 2017-03-03
3 3 2904 2017-03-03 2017-03-24
4 4 3070 2017-03-24 2017-06-06
5 5 3669 2017-06-06 2017-07-17
6 6 3997 2017-07-17 2017-08-24
和批收据:
# A tibble: 6 x 9
datetimeindex `Batch-Num` `Receipt-Num` `Receipt-Date` `Receipt-Time` Quantity datetime
<chr> <int> <int> <dttm> <chr> <dbl> <chr> <chr>
1 1 99241 88678 2017-01-11 00:00:00 00:57:55 46500 2017-01~
2 2 99322 88689 2017-01-11 00:00:00 05:09:29 45800 2017-01~
3 3 99323 88703 2017-01-11 00:00:00 05:29:51 45000 2017-01~
4 4 99242 88704 2017-01-11 00:00:00 13:04:20 44600 2017-01~
5 5 99243 88711 2017-01-11 00:00:00 13:08:36 45000 2017-01~
6 6 99353 88733 2017-01-12 00:00:00 03:47:23 45225 2017-01~
我需要做的是我需要在第一个表中创建一个名为“quantity”的新列,它是 batchreceipts 表中“Quantity”列的总和,经过过滤,以便 workorders 表中的数量列仅对数量求和基于batchreceipts$`Receipt-Date` >= workorders$`createddate`
AND batchreceipts$`Receipt-Date <= workorders$`nextdate`
我没有找到任何资源来建议一种方法来构建这样的新列添加。 任何人都可以提供指导吗?
** 编辑以显示我需要/预期的 output 将是什么(我添加了数量列并输入了一些任意值):
工作订单:
# A tibble: 6 x 6
cleaningindex wonumber createddate nextdate quantity
<chr> <int> <date> <date> <int>
1 1 2093 2017-01-11 2017-02-09 800000
2 2 2514 2017-02-09 2017-03-03 925000
3 3 2904 2017-03-03 2017-03-24 1200000
4 4 3070 2017-03-24 2017-06-06 715000
5 5 3669 2017-06-06 2017-07-17 945000
6 6 3997 2017-07-17 2017-08-24 400000
一种解决方案是将mapply()
与 function 一起使用,该 function 根据它们是否在“收据日期”在“createdate”和“nextdate”之间的范围内连续排列您的“数量”:
quantity_sum <- function(date1, date2) {
rdate <- batchreceipts$`Receipt-Date`
matching_rows <- (rdate >= date1) & (rdate <= date2)
sum(batchreceipts$Quantity[matching_rows])
}
workorders$`Quantity Sum` <- mapply(quantity_sum, workorders$createddate, workorders$nextdate)
这给了我们:
# A tibble: 6 x 5
cleaningindex wonumber createddate nextdate `Quantity Sum`
<dbl> <dbl> <date> <date> <dbl>
1 1 2093 2017-01-11 2017-02-09 272125
2 2 2514 2017-02-09 2017-03-03 0
3 3 2904 2017-03-03 2017-03-24 0
4 4 3070 2017-03-24 2017-06-06 0
5 5 3669 2017-06-06 2017-07-17 0
6 6 3997 2017-07-17 2017-08-24 0
注意:将来,请提供代码以重现您的数据。 我在这里使用tribble()
但更好的方法是从dput(workorders)
复制 output 。
library(dplyr)
workorders <- tribble(
~cleaningindex, ~wonumber, ~createddate, ~nextdate,
1, 2093, "2017-01-11", "2017-02-09",
2, 2514, "2017-02-09", "2017-03-03",
3, 2904, "2017-03-03", "2017-03-24",
4, 3070, "2017-03-24", "2017-06-06",
5, 3669, "2017-06-06", "2017-07-17",
6, 3997, "2017-07-17", "2017-08-24"
)
batchreceipts <- tribble(
~datetimeindex, ~`Batch-Num`, ~`Receipt-Num`, ~`Receipt-Date`, ~`Receipt-Time`, ~Quantity,
1, 99241, 88678, "2017-01-11 00:00:00", "00:57:55", 46500,
2, 99322, 88689, "2017-01-11 00:00:00", "05:09:29", 45800,
3, 99323, 88703, "2017-01-11 00:00:00", "05:29:51", 45000,
4, 99242, 88704, "2017-01-11 00:00:00", "13:04:20", 44600,
5, 99243, 88711, "2017-01-11 00:00:00", "13:08:36", 45000,
6, 99353, 88733, "2017-01-12 00:00:00", "03:47:23", 45225
)
workorders[[3]] <- as.Date(workorders[[3]])
workorders[[4]] <- as.Date(workorders[[4]])
batchreceipts[[4]] <- as.Date(batchreceipts[[4]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.