【问题标题】:Timeseries - Appending Growth Data by Row Sequentailly时间序列 - 按行顺序附加增长数据
【发布时间】:2020-05-06 18:46:33
【问题描述】:

我是 R 的新手(ish),正在寻求有关问题的帮助。我在 stackoverflow 上搜索了很多类似的问题,但绝对有可能我使用了错误的单词进行搜索。

我想做的是简单地说,用 dplyr 管道在行上按顺序做一个方程,这样一行中的结果取决于它前面的行中的值。

出于本次讨论的目的,我创建了一个示例数据框(命名为 sample):

> sample
# A tibble: 10 x 4
    year variable variable_growth_rates other_growth_rates
   <dbl>    <dbl>                 <dbl>              <dbl>
 1  2010    20000              NA                   NA    
 2  2011    23450               0.173                0.15 
 3  2012    25110               0.0708               0.12 
 4  2013    25350               0.00956              0.105
 5  2014    24920              -0.0170               0.075
 6  2015    25010               0.00361              0.05 
 7  2016       NA              NA                    0.025
 8  2017       NA              NA                    0.032
 9  2018       NA              NA                    0.041
10  2019       NA              NA                    0.071

我想将 other_growth_rates 中的增长率仅应用于去年数据的列变量,保留历史数据。我尝试过循环、应用以及我自己的函数,但我怀疑我在这些方面的技能很糟糕,所以我无法让它们工作。

我可以做长格式:

##### Append Growth rates from variable other_growth_rates to our historical data in variable
sample_rvsd <- sample %>%
  mutate(variable_rvsd = ifelse(!is.na(variable), 
variable, lag(variable)*(1+other_growth_rates))) %>%
  mutate(variable_rvsd = ifelse(!is.na(variable), variable, lag(variable)*(1+other_growth_rates))) %>%  
  mutate(variable_rvsd = ifelse(!is.na(variable), variable, lag(variable)*(1+other_growth_rates))) %>% 
  mutate(variable_rvsd = ifelse(!is.na(variable), variable, lag(variable)*(1+other_growth_rates)))

如果只有几年和一个变量,这很好,但我需要用多个变量和十多年的时间来做到这一点。这也是我经常遇到的问题,所以我可以看到我的代码在匆忙中变得混乱。

我尝试编写一个函数 - 我认为这是最佳的(因为我确实需要经常这样做)。再说一次,我的技能不是很好:

编写 append_growth 函数
# Function
append_growth <- function(x, y) {
  for (i in seq_along(x)) {
    x_new <- ifelse(!is.na(x), x, lag(x) * y)
  }
}

##### Apply growth rates function
sample_rvsd_fail <- sample %>%
  mutate(variable_rvsd = append_growth(variable, other_growth_rates))

感谢任何帮助。

【问题讨论】:

    标签: r function dplyr iteration


    【解决方案1】:

    有很多方法可以做到这一点,所以下面只是使用 base R 的一个答案。我是在 base 中做的,因为它最类似于你开始的方式。您提到想在 dplyr 中执行此操作,这将是解决此问题的好方法。查看mutate_at/mutate_if 上的文档。

    sample <- data.frame(year = c(2010:2019),
                         variable = c(20000, 23450, 25110, 25350, 24920, 25010, NA, NA, NA, NA),
                         variable_growth_rates = c(NA, .173, .0708, .00956, -.0170, .00361, NA, NA, NA, NA),
                         other_growth_rates = c(NA, .15, .12, .105, .075, .05, .025, .032, .041, .071))
    
    
    append_growth <- function(data, variable, rate){
      for(x in 1:nrow(data)){
        data[x, variable] <- ifelse(is.na(data[x, variable]), 
                                        (data[x-1, variable]*(1+data[x, rate])), data[x, variable])
      }
      data
    }
    
    df <- append_growth(sample, "variable", "other_growth_rates")
    
    

    【讨论】:

    • 谢谢。这很完美 - 跨越多个不同的数据帧。我会按照建议检查 mutate_at 。现在,我喜欢这个函数,因为它最大限度地减少了我在每个示例中需要做的编码。
    猜你喜欢
    • 1970-01-01
    • 2023-01-31
    • 2020-03-31
    • 2019-03-26
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多