通过在R中使用dplyr计算融化数据框和分组

时间:2019-06-07 02:16:16

标签: r dataframe dplyr plyr reshape2

我的样本数据 `

git clone <github_url>
cd <repo_name>
git remote add gitlab <gitlab_url>
git push gitlab <branch_name>

`

我正在尝试按州和地区分组,然后从每年开始计算每月增长率。

每月计算的公式为:(1 + rates * growth_in_year)^(1/12)-1 纠正我的错误

`

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>)

,其他地区也是如此。 每个地区的费率必须每年递增。 我想使用日期格式而不是年份格式。

2 个答案:

答案 0 :(得分:1)

我们可以先将gather数据转换为长格式,然后再group_by statedistrictyear,找到新的每月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))

答案 1 :(得分:1)

我们可以使用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