根据日期在R中创建运行计数

发布于 2025-02-11 04:27:34 字数 3476 浏览 1 评论 0原文

我正在尝试根据日期在R中创建运行计数,并变得有些困惑。

我所拥有的是:

DF1

ID日期Inved0 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
XYZ12021-04-0100 0
xyz12021-04-029 99
xyz12021-04-04-0300
xyz12021-04-04-04-04150
xyz12021-04-050 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 0 00
士板XYZ12021-04-0674
XYZ12021-04-0700 0
XYZ12021-04-0805
XYZ12021-04-09120
XYZ12021-04-11 2021-04-11180
XYZ12021-04-120XY4-12 0 0
XYZ12021 2021 2021 2021 2021 2021 -04-131210
XYZ12021-04-14180
xyz12021-04-1505

我想要INV的运行计数。如果在第1天有9个报告Invoicing1,而同一第9天则发票1。然后在第2天,开票1应该= 0。用于获取以下计算的方程式上的任何帮助将不胜感激。

我想要的是: DF2

ID日期INVINVED
XYZ12021-04-0100
XYZ12021-04-0299 9
XYZ12021-04-0309
XYZ12021-04-04-04-04-04-04-04-05159
XYZ12021-04-04-04-04159
XYZ12021-04--04-04-04-04-04-04-04-04-04-04-04-04-04- 061813
xyz12021-04-071813
xyz12021-04-081318
xyz12021-04-092518
xyz12021-04-114318
xyz12021-2021-04-04-12
28
XYZ12021-04-146328
XYZ12021-04-155833

这是数据的可再现样本:

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

IDDateInvInved
XYZ12021-04-0100
XYZ12021-04-0299
XYZ12021-04-0300
XYZ12021-04-04150
XYZ12021-04-0500
XYZ12021-04-0674
XYZ12021-04-0700
XYZ12021-04-0805
XYZ12021-04-09120
XYZ12021-04-11180
XYZ12021-04-1200
XYZ12021-04-131210
XYZ12021-04-14180
XYZ12021-04-1505

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

IDDateInvInved
XYZ12021-04-0100
XYZ12021-04-0299
XYZ12021-04-0309
XYZ12021-04-04159
XYZ12021-04-05159
XYZ12021-04-061813
XYZ12021-04-071813
XYZ12021-04-081318
XYZ12021-04-092518
XYZ12021-04-114318
XYZ12021-04-124318
XYZ12021-04-134528
XYZ12021-04-146328
XYZ12021-04-155833

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埖埖迣鎅 2025-02-18 04:27:34

这是一个尝试,但第二行中只有数字更改

df1$Inv <- cumsum(df1$Inv - df1$Inved)

df1$Inved <- cumsum(df1$Inved)

Here is a try but with only number changed in the second row

df1$Inv <- cumsum(df1$Inv - df1$Inved)

df1$Inved <- cumsum(df1$Inved)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文