简体   繁体   English

多列的同比增长率基于唯一 ID 和行中的年份

[英]Year on year growth rates for multiple columns basis unique ID & years in rows

I have a data set where I have a unique proposal ID, application year & financial statement year.我有一个数据集,其中有一个唯一的提案 ID、申请年份和财务报表年份。 One proposal ID shall have one application year(t) & could have t-1 &(or) t-2 financial year statements.一个提案 ID 应有一个申请年 (t) 并且可以有 t-1 和(或)t-2 财政年度报表。 I have multiple columns for debt, equity,.networth etc & want to have two columns for YOY growth -F1 & YOY growth-2.我有多个债务、股权、.networth 等列,并且希望有两个列用于 YOY 增长 -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现在根据每个提案 ID,我需要额外的列来说明财务报表年份与我的申请年份之间的增长率

desired output:所需 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我想要的是一个 function——对于每个提案 ID,它估计 YOY 增长并取出最新的申请日期作为最后一行,列作为数据集中所有数字变量的 YOY 增长

Thank you in advance for the help: :)预先感谢您的帮助::)

This can be done use the dplyr::lead() formula in mutate() .这可以使用mutate()中的dplyr::lead()公式来完成。 The jantior::clean_names() is optional to make the code writing easier. jantior::clean_names()是可选的,以使代码编写更容易。

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

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_ID提案_ID Application_Year申请_年 Financial_Statement_Year财务报表年 Net_Sales净销售额 Growth_Difference成长差异 Growth_Rate增长率
P3 P3 2019 2019 2017 2017年 130 130 NA北美 NA北美
P2 P2 2019 2019 2018 2018年 120 120 -10 -10 -8.333 -8.333
P1 P1 2019 2019 2019 2019 100 100 -20 -20 -20.000 -20.000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM