![](/img/trans.png)
[英]R Studio - group by dataframe and get statistics using dplyr
[英]melt dataframe and group by using dplyr calculations in R
我的樣本數據
structure(list(state = c("AP", "AP"), district = c("krishna",
"guntur"), rate = c(170104.5156, 1343.78134), growth_in_2016 = c(0.3844595,
0.3678), growth_in_2017 = c(0.444595, 0.8445), growth_in_2018 = c(0.323699,
0.36213), growth_in_2019 = c(0.5777, 0.35256), growth_in_2020 = c(0.2669097,
0.9097)), class = c("data.table", "data.frame"), row.names = c(NA,-2L), .internal.selfref = <pointer: 0x00000000026c1ef0>)
`
我試圖按州和地區分組,然后從每年計算每月的增長率。
每月計算的公式為:(1 + rates * growth_in_year)^(1/12)-1如果輸入錯誤,請糾正我
`
state district date rates
AP krishna 2016-12-31 x
AP krishna 2017-01-31 y
AP krishna 2017-02-28 z
AP krishna 2017-03-30 a
AP krishna 2017-04-31 b
AP krishna 2017-05-30 c
AP krishna 2017-06-31 d
對其他地區也是如此。 每個地區的費率必須每年遞增。 我想使用日期格式而不是年份格式。
我們可以先gather
長格式的數據,然后再按state
, district
和year
group_by
,找到新的每月rate
,從列名稱中提取年份,並創建一個代表整個月份的最后一天的日期list
,最后計算累積rate
總和以獲得每月的增量值。
library(dplyr)
library(tidyr)
df %>%
gather(key, value, -(1:3)) %>%
group_by(state, district, key) %>%
mutate(rate = (1 + rate * value)^(1/12) - 1,
year = sub(".*(\\d{4})", "\\1", key),
dates = list(seq(as.Date(paste0(year, "-01-01")),
as.Date(paste0(year, "-12-01")), by = "month")- 1)) %>%
unnest() %>%
mutate(rate = cumsum(rate)) %>%
select(-year)
# state district rate key value dates
# <chr> <chr> <dbl> <chr> <dbl> <date>
# 1 AP krishna 1.52 growth_in_2016 0.384 2015-12-31
# 2 AP krishna 3.04 growth_in_2016 0.384 2016-01-31
# 3 AP krishna 4.56 growth_in_2016 0.384 2016-02-29
# 4 AP krishna 6.08 growth_in_2016 0.384 2016-03-31
# 5 AP krishna 7.60 growth_in_2016 0.384 2016-04-30
# 6 AP krishna 9.12 growth_in_2016 0.384 2016-05-31
# 7 AP krishna 10.6 growth_in_2016 0.384 2016-06-30
# 8 AP krishna 12.2 growth_in_2016 0.384 2016-07-31
# 9 AP krishna 13.7 growth_in_2016 0.384 2016-08-31
#10 AP krishna 15.2 growth_in_2016 0.384 2016-09-30
# … with 110 more rows
數據
df <- structure(list(state = c("AP", "AP"), district = c("krishna",
"guntur"), rate = c(170104.5156, 1343.78134), growth_in_2016 = c(0.3844595,
0.3678), growth_in_2017 = c(0.444595, 0.8445), growth_in_2018 = c(0.323699,
0.36213), growth_in_2019 = c(0.5777, 0.35256), growth_in_2020 = c(0.2669097,
0.9097)), class = c("data.table", "data.frame"), row.names = c(NA, -2L))
我們可以使用mutate_at
在“增長”列上進行費率計算,然后gather
為“長”格式,從“日期”中刪除子字符串,按“州”,“地區”分組,獲取“值”的cumsum
柱
library(tidyverse)
out <- df %>%
mutate_at(vars(starts_with('growth')), list(~ (1 + rate * .)^(1/12) - 1)) %>%
gather(date, value, matches("growth")) %>%
mutate(date = str_remove(date, ".*_")) %>%
group_by(state, district) %>%
mutate(value = cumsum(value))
out %>%
filter(district == "krishna")
# A tibble: 5 x 5
# Groups: state, district [1]
# state district rate date value
# <chr> <chr> <dbl> <chr> <dbl>
#1 AP krishna 170105. 2016 1.52
#2 AP krishna 170105. 2017 3.07
#3 AP krishna 170105. 2018 4.55
#4 AP krishna 170105. 2019 6.16
#5 AP krishna 170105. 2020 7.60
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.