如何计算13个月前的数据滚动

时间:2019-06-24 19:38:41

标签: r

我希望获取13个月的滚动数据并将其分配到另一列中。

我想找出每13个月内最旧期间的资产值。我当前正在使用的代码正在连续13个月的时间内找到min(Assets)。相反,我想要最古老的月份的资产。在此方面的任何帮助将不胜感激。因此在此示例中,对于Group__C == Eperiod == 6/1/2018,我希望Month13Asset显示22175.618而不是21059.052

x <- x %>% 
  arrange(Group__c, Fund, Period) %>% 
  group_by(Group__c, Fund) %>% 
  mutate(Month13Asset=RcppRoll::roll_min(x=Assets,13, align="right", fill=NA))

# Group__c          Fund    Period  Assets  Month13Asset
# A   FundA 8/1/2018    0   NA
# B   FundA 8/1/2018    0   NA
# D FundA   8/1/2018    0   NA
# D FundA   9/1/2018    6973.97 NA
# D FundA   10/1/2018   6870.799    NA
# D FundA   11/1/2018   6132.729    NA
# D FundA   12/1/2018   6347.007    NA
# D FundA   1/1/2019    5664.626    NA
# D FundA   2/1/2019    6292.03 NA
# D FundA   3/1/2019    6705.801    NA
# D FundA   4/1/2019    6802.498    NA
# D FundA   5/1/2019    7056.608    NA
# E FundA   6/1/2017    22175.618   NA
# E FundA   7/1/2017    21263.086   NA
# E FundA   8/1/2017    21207.543   NA
# E FundA   9/1/2017    21059.052   NA
# E FundA   10/1/2017   21505.484   NA
# E FundA   11/1/2017   22293.494   NA
# E FundA   12/1/2017   22938.228   NA
# E FundA   1/1/2018    23104.586   NA
# E FundA   2/1/2018    23296.339   NA
# E FundA   3/1/2018    21539.104   NA
# E FundA   4/1/2018    24010.93    NA
# E FundA   5/1/2018    23965.739   NA
# E FundA   6/1/2018    24944.856   21059.052
# E FundA   7/1/2018    24944.856   21059.052
# E FundA   8/1/2018    25562.452   21059.052

1 个答案:

答案 0 :(得分:0)

Tidyverse方法:我使用dplyr::lag()将Assets数据偏移12行,并在使用dplyr::do()进行分组之后将其添加到数据框中。只有在您的数据间隔均匀且没有丢失的月份的情况下,这才起作用。

library(dplyr)
library(lubridate)

x %>% 
  mutate(Period = mdy(Period)) %>%
  arrange(Group__c, Fund, Period) %>% 
  group_by(Group__c, Fund) %>% 
  do(
    mutate(., Month13Asset = lag(.$Assets, 12))
  )

   Group__c  Fund     Period    Assets Month13Asset
1         A FundA 2018-08-01     0.000           NA
2         B FundA 2018-08-01     0.000           NA
3         D FundA 2018-08-01     0.000           NA
4         D FundA 2018-09-01  6973.970           NA
5         D FundA 2018-10-01  6870.799           NA
6         D FundA 2018-11-01  6132.729           NA
7         D FundA 2018-12-01  6347.007           NA
8         D FundA 2019-01-01  5664.626           NA
9         D FundA 2019-02-01  6292.030           NA
10        D FundA 2019-03-01  6705.801           NA
11        D FundA 2019-04-01  6802.498           NA
12        D FundA 2019-05-01  7056.608           NA
13        E FundA 2017-06-01 22175.618           NA
14        E FundA 2017-07-01 21263.086           NA
15        E FundA 2017-08-01 21207.543           NA
16        E FundA 2017-09-01 21059.052           NA
17        E FundA 2017-10-01 21505.484           NA
18        E FundA 2017-11-01 22293.494           NA
19        E FundA 2017-12-01 22938.228           NA
20        E FundA 2018-01-01 23104.586           NA
21        E FundA 2018-02-01 23296.339           NA
22        E FundA 2018-03-01 21539.104           NA
23        E FundA 2018-04-01 24010.930           NA
24        E FundA 2018-05-01 23965.739           NA
25        E FundA 2018-06-01 24944.856     22175.62
26        E FundA 2018-07-01 24944.856     21263.09
27        E FundA 2018-08-01 25562.452     21207.54