根据日期在R中创建运行计数
我正在尝试根据日期在R中创建运行计数,并变得有些困惑。
我所拥有的是:
DF1
ID | 日期 | Inved | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
---|---|---|---|
XYZ1 | 2021-04-01 | 0 | 0 0 |
xyz1 | 2021-04-02 | 9 9 | 9 |
xyz1 | 2021-04-04-03 | 0 | 0 |
xyz1 | 2021-04-04-04-04 | 15 | 0 |
xyz1 | 2021-04-05 | 0 0 0 0 0 0 0 0 0 0.0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | 0 0 00 |
士板XYZ1 | 2021-04-06 | 7 | 4 |
XYZ1 | 2021-04-07 | 0 | 0 0 |
XYZ1 | 2021-04-08 | 0 | 5 |
XYZ1 | 2021-04-09 | 12 | 0 |
XYZ1 | 2021-04-11 2021-04-11 | 18 | 0 |
XYZ1 | 2021-04-12 | 0 | XY4-12 0 0 |
XYZ1 | 2021 2021 2021 2021 2021 2021 -04-13 | 12 | 10 |
XYZ1 | 2021-04-14 | 18 | 0 |
xyz1 | 2021-04-15 | 0 | 5 |
我想要INV的运行计数。如果在第1天有9个报告Invoicing1,而同一第9天则发票1。然后在第2天,开票1应该= 0。用于获取以下计算的方程式上的任何帮助将不胜感激。
我想要的是: DF2
ID | 日期 | INV | INVED |
---|---|---|---|
XYZ1 | 2021-04-01 | 0 | 0 |
XYZ1 | 2021-04-02 | 9 | 9 9 |
XYZ1 | 2021-04-03 | 0 | 9 |
XYZ1 | 2021-04-04-04-04-04-04-04-05 | 15 | 9 |
XYZ1 | 2021-04-04-04-04 | 15 | 9 |
XYZ1 | 2021-04--04-04-04-04-04-04-04-04-04-04-04-04-04- 06 | 18 | 13 |
xyz1 | 2021-04-07 | 18 | 13 |
xyz1 | 2021-04-08 | 13 | 18 |
xyz1 | 2021-04-09 | 25 | 18 |
xyz1 | 2021-04-11 | 43 | 18 |
xyz1 | 2021-2021-04-04-12 | | |
| | | 28 |
XYZ1 | 2021-04-14 | 63 | 28 |
XYZ1 | 2021-04-15 | 58 | 33 |
这是数据的可再现样本:
df1 <- data.frame(ID = c('XYZ1','XYZ1', 'XYZ1', 'XYZ1', 'XYZ1', 'XYZ1', 'XYZ1', 'XYZ1', 'XYZ1','XYZ1', 'XYZ1', 'XYZ1', 'XYZ1','XYZ1'),
Date = c(2021-04-01, 2021-04-02, 2021-04-03, 2021-04-04, 2021-04-05, 2021-04-06, 2021-04-07, 2021-04-08, 2021-04-09, 2021-04-10, 2021-04-11, 2021-04-12, 2021-04-13, 2021-04-14),
Inv = c(0,9,0,15,0,7,0,0,12,18,0,12,18,0),
Inved = c(0,9,0,0,0,4,0,5,0,0,0,10,0,5))
我想创建从DF1到DF2所需的计算。
我本质上想弄清楚如何编写从DF1到DF2的方程式。
我认为正在发生的事情是:
1:INV被Inved减去。 df1 $ row3 = df1 $ row3inv = df1 $ row3inved。
2:在第3行中,INV现在为0,因为发票已转移到发票中。
3:如果INV继续随着INV的发展而不断变化,则需要添加INV。
4:随着Inv的增长,如果Inved增加了,则必须减去
I am trying to create a running count in R based on Dates and getting a bit confused.
What I have is:
DF1
ID | Date | Inv | Inved |
---|---|---|---|
XYZ1 | 2021-04-01 | 0 | 0 |
XYZ1 | 2021-04-02 | 9 | 9 |
XYZ1 | 2021-04-03 | 0 | 0 |
XYZ1 | 2021-04-04 | 15 | 0 |
XYZ1 | 2021-04-05 | 0 | 0 |
XYZ1 | 2021-04-06 | 7 | 4 |
XYZ1 | 2021-04-07 | 0 | 0 |
XYZ1 | 2021-04-08 | 0 | 5 |
XYZ1 | 2021-04-09 | 12 | 0 |
XYZ1 | 2021-04-11 | 18 | 0 |
XYZ1 | 2021-04-12 | 0 | 0 |
XYZ1 | 2021-04-13 | 12 | 10 |
XYZ1 | 2021-04-14 | 18 | 0 |
XYZ1 | 2021-04-15 | 0 | 5 |
I want a running count of Inv. If there are 9 reports Invoicing1 on Day1 and that same day 9 are Invoiced1. Then on Day 2, Invoicing 1 should = 0. Any help on the equation used to get to the calculation below would be appreciated.
What I want is:
DF2
ID | Date | Inv | Inved |
---|---|---|---|
XYZ1 | 2021-04-01 | 0 | 0 |
XYZ1 | 2021-04-02 | 9 | 9 |
XYZ1 | 2021-04-03 | 0 | 9 |
XYZ1 | 2021-04-04 | 15 | 9 |
XYZ1 | 2021-04-05 | 15 | 9 |
XYZ1 | 2021-04-06 | 18 | 13 |
XYZ1 | 2021-04-07 | 18 | 13 |
XYZ1 | 2021-04-08 | 13 | 18 |
XYZ1 | 2021-04-09 | 25 | 18 |
XYZ1 | 2021-04-11 | 43 | 18 |
XYZ1 | 2021-04-12 | 43 | 18 |
XYZ1 | 2021-04-13 | 45 | 28 |
XYZ1 | 2021-04-14 | 63 | 28 |
XYZ1 | 2021-04-15 | 58 | 33 |
Here is a reproducible sample of the data:
df1 <- data.frame(ID = c('XYZ1','XYZ1', 'XYZ1', 'XYZ1', 'XYZ1', 'XYZ1', 'XYZ1', 'XYZ1', 'XYZ1','XYZ1', 'XYZ1', 'XYZ1', 'XYZ1','XYZ1'),
Date = c(2021-04-01, 2021-04-02, 2021-04-03, 2021-04-04, 2021-04-05, 2021-04-06, 2021-04-07, 2021-04-08, 2021-04-09, 2021-04-10, 2021-04-11, 2021-04-12, 2021-04-13, 2021-04-14),
Inv = c(0,9,0,15,0,7,0,0,12,18,0,12,18,0),
Inved = c(0,9,0,0,0,4,0,5,0,0,0,10,0,5))
What I would like to create the calculations required to go from DF1 to DF2.
I would essentially like to figure out how to write the equation to go from DF1 to DF2.
What I believe is happening is:
1: Inv is subtracted by Inved. DF1$Row3 = DF1$Row3Inv = DF1$Row3Inved.
2: In Row 3, Inv is now 0 as the Invoicing have been transfered to Invoiced.
3: If Inv continues to grow as Inved doesn't change then Inv needs to be added up.
4: As Inv grows, if there is a increased in Inved then that must be subtracted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个尝试,但第二行中只有数字更改
Here is a try but with only number changed in the second row