简体   繁体   English

如何最好地计算 R 的同比差异

[英]How best to calculate a year over year difference in R

Below is the sample code.下面是示例代码。 The task at hand is to create a year over year difference (2021 q4 value - 2020 q4 value) for only the fourth quarter and percentage difference.手头的任务是仅针对第四季度和百分比差异创建同比差异(2021 年第四季度值 - 2020 年第四季度值)。 Desired result is below.期望的结果如下。 Usually I would do a pivot_wider and such.通常我会做一个 pivot_wider 等。 However, how does one do this and not take all quarters into account?但是,如何做到这一点而不考虑所有方面?

  year <- c(2020,2020,2020,2020,2021,2021,2021,2021,2020,2020,2020,2020,2021,2021,2021,2021)
  qtr <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
  area <- c(1012,1012,1012,1012,1012,1012,1012,1012,1402,1402,1402,1402,1402,1402,1402,1402)
  employment <- c(100,102,104,106,108,110,114,111,52,54,56,59,61,66,65,49)

  test1 <- data.frame (year,qtr,area,employment)

  area       difference     percentage
  1012           5              4.7%
  1402           -10            -16.9

You would use filter on quarter:您将在季度使用filter

test1 |>
  filter(qtr == 4) |>
  group_by(area) |>
    mutate(employment_lag = lag(employment),
           diff = employment - employment_lag) |>
    na.omit() |>
  ungroup() |>
  mutate(percentage = diff/employment_lag)

Output:输出:

# A tibble: 2 × 7
   year   qtr  area employment  diff employment_start percentage
  <dbl> <dbl> <dbl>      <dbl> <dbl>            <dbl>      <dbl>
1  2021     4  1012        111     5              106     0.0472
2  2021     4  1402         49   -10               59    -0.169 

Update: Adding correct percentage.更新:添加正确的百分比。

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

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