使用tidyverse创建一个以恒定速率增加的列

时间:2018-03-28 19:56:10

标签: r dplyr tidyverse purrr

我有一个时间序列的人口,我想比较人口增长与某些增长率的关系。因此,我试图创建一个列,将我的初始人口值乘以一些不变的增长率,然后将该值乘以相同的恒定增长率等等。我不能仅仅乘以使用mutate的增长率因为它不会使用之前的值。

注意:我已在下面回答了我自己的问题,但已将此作为资源提供给其他人。如果还有其他方法可以实现相同的目标,我会对此感兴趣。

library(ggplot2)
library(tibble)
library(dplyr)

growth_rate <- 0.05 # percent

# the "estimated" column is what I want.
df <- tibble(year = seq(2000, 2005, by = 1),
             population = seq(1, 2, length = 6),
             estimated = c(1.00, 1.05, 1.10, 1.16, 1.22, 1.28))

2 个答案:

答案 0 :(得分:3)

为什么我们需要purrr::accumulate,使用简单的公式可以实现同样的目标:

library(tidyverse)
growth_rate <- 0.05 # percent
df %>% mutate(Calculated = first(estimated)*((1+growth_rate)^(row_number()-1)))
# # A tibble: 6 x 4
# year population estimated Calculated
# <dbl>      <dbl>     <dbl>      <dbl>
# 1  2000       1.00      1.00       1.00
# 2  2001       1.20      1.05       1.05
# 3  2002       1.40      1.10       1.10
# 4  2003       1.60      1.16       1.16
# 5  2004       1.80      1.22       1.22
# 6  2005       2.00      1.28       1.28

修改

@Frank已经使用复合利率指出comment在上面的一个答案中计算growth_rate

答案 1 :(得分:2)

使用purrr::accumulate以递增方式将初始值乘以增长率,并保留中间值。在这里,.x是您的累积值。有关详细信息,请参阅documentation

library(ggplot2)
library(tibble)
library(dplyr)
library(purrr)

# alteratively, load the tidyverse 
# library(tidyverse)

growth_rate <- 0.05 # percent

df <- tibble(year = seq(2000, 2005, by = 1),
             population = seq(1, 2, length = 6),
             estimated = c(1.00, 1.05, 1.10, 1.16, 1.22, 1.28))

df <- df %>%
  mutate(with_purr = accumulate(population, ~ .x * (1 + growth_rate)))

df
#> # A tibble: 6 x 4
#>    year population estimated with_purr
#>   <dbl>      <dbl>     <dbl>     <dbl>
#> 1 2000.       1.00      1.00      1.00
#> 2 2001.       1.20      1.05      1.05
#> 3 2002.       1.40      1.10      1.10
#> 4 2003.       1.60      1.16      1.16
#> 5 2004.       1.80      1.22      1.22
#> 6 2005.       2.00      1.28      1.28