r - 我如何根据 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"))
解决方案
这是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))
推荐阅读
- c# - Asp.net 使用 Request.Form 从 c# 中的 html 表单获取发布的值并获取数组值
- c# - 导入返回列表的 C# 函数
到 Python - angularjs - 当内容类型为 application/json 而不是 text/html 时,HTTP 请求未完成
- itext - iText 5 HTML+CSS 到 PDF/A-2:Helvetica 字体未嵌入错误
- javascript - 用脚本中断html页面的刷新过程
- angular - 在 Apache 2 上部署 Angular 6 应用程序
- c++ - 编译 C++ 代码时找不到 Kinect.h 文件
- mono - 如何判断 Mono 是否真的使用 LLVM?
- javascript - 使用 javascript 打印时未出现 Ajax Rating Control
- python - C++ 嵌入式 python 和环境变量