首页 > 解决方案 > 我如何根据 R 中的过去 7 天计算每日增长率

问题描述

我有这个数据集,我想根据过去 7 天从cu_deaths 列计算每日增长率。任何想法?如果可能的话:dplyr

输入:

structure(list(Country.Region = c("Greece", "Greece", "Greece", 
"Greece", "Greece", "Greece", "Greece"), date = structure(c(1586577600, 
1586664000, 1586750400, 1586836800, 1586923200, 1587009600, 1587096000
), class = c("POSIXct", "POSIXt"), tzone = ""), confirmed = c(70, 
33, 31, 25, 22, 15, 17), death = c(1, 5, 1, 2, 1, 3, 3), recovered = c(0, 
0, 0, 0, 0, 0, 0), cu_cases = c(2081, 2114, 2145, 2170, 2192, 
2207, 2224), cu_deaths = c(93, 98, 99, 101, 102, 105, 108), days_elapsed = c(21, 
22, 23, 24, 25, 26, 27), end_label = c(NA, NA, NA, NA, NA, NA, 
"Greece"), index = c(1, 2, 3, 4, 5, 6, 7)), row.names = c(NA, 
-7L), class = c("tbl_df", "tbl", "data.frame"))

标签: rdplyrtime-seriesdataset

解决方案


这是dplyr计算每天增长百分比的解决方案:

library(dplyr)

df <- df %>%
  mutate(diff_deaths = cu_deaths - lag(cu_deaths),
         diff_day = 1,
         percent_growth = ((diff_deaths/diff_day)/cu_deaths*100))

如果您还想查看每周的平均每日增长,您可以使用:

library(dplyr)
library(lubridate)

df$date <- ymd(df$date)

df <- df %>%
  mutate(diff_deaths = cu_deaths - lag(cu_deaths),
         diff_day = 1,
         percent_growth = ((diff_deaths/diff_day)/cu_deaths*100),
         week_number = isoweek(date)) %>%
  group_by(week_number) %>%
  mutate(weekly_avg_growth = mean(percent_growth))

推荐阅读