多列的同比增长率基于唯一 ID 和连续几年

发布于 2025-01-14 17:57:27 字数 921 浏览 2 评论 0原文

我有一个数据集,其中有唯一的提案 ID、申请年份和申请年份。财务报表年度。一个提案 ID 应具有一个申请年份(t) &可以有 t-1 和(或)t-2 财政年度报表。我有多个关于债务、股权、净资产等的栏目。想要有两列用于同比增长 -F1 和 F1同比增长-2。

数据集:

Proposal ID Application Year Financial statement year Net sales
P1          2019             2019                     100
P1          2019             2018                     120
P1          2019             2017                     130 

现在,根据每个提案 ID,我需要根据我的申请年度所需的输出来了解财务报表年份之间的增长率的附加列

Proposal ID Application Year Financial statement year Net sales YOY - netsales-g1
P1          2019             2019                     100             (100-120)/120...
P1          2019             2018                     120
P1          2019             2017                     130 

我需要对我拥有的所有列执行相同的步骤。

我想要的是一个函数——对于每个提案 ID,它会估计 YOY 增长和增长情况。取出最新的申请日期作为最后一行,列作为数据集中所有数值变量的同比增长情况

提前感谢您的帮助! :)

I have a data set where I have a unique proposal ID, application year & financial statement year. One proposal ID shall have one application year(t) & could have t-1 &(or) t-2 financial year statements. I have multiple columns for debt, equity, networth etc & want to have two columns for YOY growth -F1 & YOY growth-2.

dataset :

Proposal ID Application Year Financial statement year Net sales
P1          2019             2019                     100
P1          2019             2018                     120
P1          2019             2017                     130 

Now basis each proposal ID I need additional columns on growth rates between financial statement years against my application year

desired output :

Proposal ID Application Year Financial statement year Net sales YOY - netsales-g1
P1          2019             2019                     100             (100-120)/120...
P1          2019             2018                     120
P1          2019             2017                     130 

this same step I need to do for all columns I have.

What I want is a function -- for each proposal ID it estimates the YOY growth & take out the latest application date as the final row with columns as YOY growth for all numeric variables in dataset

Thank you in advance for the help! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半夏半凉 2025-01-21 17:57:27

我不确定,但这是你需要的吗?

library(dplyr)
library(tidyverse)
data %>% arrange(Financial_Statement_Year) %>%
  mutate(Growth_Difference = Net_Sales - lag(Net_Sales)) %>%
  mutate(Growth_Rate = (Growth_Difference / Net_Sales) * 100)
Proposal_IDApplication_YearFinancial_Statement_YearNet_SalesGrowth_DifferenceGrowth_Rate
P320192017130NANA
P220192018120-10-8.333
P120192019100-20-20.000

I am not sure but is it what you need?

library(dplyr)
library(tidyverse)
data %>% arrange(Financial_Statement_Year) %>%
  mutate(Growth_Difference = Net_Sales - lag(Net_Sales)) %>%
  mutate(Growth_Rate = (Growth_Difference / Net_Sales) * 100)
Proposal_IDApplication_YearFinancial_Statement_YearNet_SalesGrowth_DifferenceGrowth_Rate
P320192017130NANA
P220192018120-10-8.333
P120192019100-20-20.000
-残月青衣踏尘吟 2025-01-21 17:57:27

这可以使用 mutate() 中的 dplyr::lead() 公式来完成。 jantior::clean_names() 是可选的,以使代码编写更容易。

df %>% 
  janitor::clean_names() %>% 
  mutate(YoY_net_sales=(net_sales-lead(net_sales,n=1L))/lead(net_sales,n=1L))

This can be done use the dplyr::lead() formula in mutate(). The jantior::clean_names() is optional to make the code writing easier.

df %>% 
  janitor::clean_names() %>% 
  mutate(YoY_net_sales=(net_sales-lead(net_sales,n=1L))/lead(net_sales,n=1L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文