首页 文章

R如何每月回滚12个月并进行总结

提问于
浏览
0

页面底部的示例数据 .

我的目标是:

每个月回顾过去12个月并根据它过滤数据 . 举个例子,如果我们选择2017-11-01 . 因此,12个月后,它是2016-12-01 . 因此,在2016-12-01期间过滤数据到2017-11-01在12个月内,计算每个组的唯一ID数量,每个月在数据框中重复一次,即每月返回12个月数据集并获取每个组的唯一ID计数如果在上面的点上成功执行,我可以在x轴上绘制日期的线图,并在每个月的y轴上绘制每个组的唯一ID计数 .

我的尝试:

foo %>%
filter(Date >= “2016-12-01” & Date <= “2017-11-01”) %>%
group_by(ID, Group) %>%
count() %>%
group_by(Group) %>%
count

输出:Min 2 sport 6 Xox 41

以上输出为2017-11-01 . 但是,我的目标是每个月重复此过程并将其存储在数据框中,以便我可以绘制线图 .

非常感谢社区的任何帮助 .

谢谢

这是我的示例数据框:

#>         ID       Date Group
#> 1   D_2313 2017-07-01   Xox
#> 2   D_2416 2017-07-01   Xox
#> 3    D_446 2017-02-01   Xox
#> 4   D_3466 2017-07-01   Xox
#> 5   D_1183 2017-01-01   Xox
#> 6   D_3751 2015-12-01   Xox
#> 7     D_76 2017-03-01 sport
#> 8    D_441 2015-10-01   Xox
#> 9   D_1417 2017-02-01   Xox
#> 10  D_2886 2016-12-01   Xox
#> 11  D_1027 2016-03-01   Xox
#> 12  D_1955 2016-05-01   Xox
#> 13  D_1227 2017-04-01   Xox
#> 14   D_371 2016-11-01   Xox
#> 15   D_293 2017-10-01 sport
#> 16  D_2712 2016-12-01   Xox
#> 17  D_1122 2016-06-01   Xox
#> 18   D_839 2015-11-01   Xox
#> 19    D_83 2016-10-01   Xox
#> 20  D_3286 2016-03-01   Xox
#> 21  D_1216 2017-09-01   Xox
#> 22  D_3182 2017-07-01   Xox
#> 23   D_376 2017-09-01   Xox
#> 24   D_946 2016-11-01   Xox
#> 25  D_2585 2017-06-01   Xox
#> 26   D_162 2017-03-01   Xox
#> 27  D_2485 2017-05-01   Min
#> 28  D_1994 2017-05-01 sport
#> 29   D_543 2016-12-01   Xox
#> 30  D_3338 2016-07-01   Xox
#> 31    D_72 2016-10-01   Xox
#> 32   D_840 2016-04-01   Xox
#> 33   D_331 2016-01-01   Xox
#> 34  D_2644 2017-11-01   Xox
#> 35  D_2884 2016-12-01   Xox
#> 36  D_2626 2017-03-01   Xox
#> 37  D_2937 2017-09-01   Xox
#> 38  D_2818 2017-02-01   Xox
#> 39  D_3636 2017-10-01   Xox
#> 40  D_1103 2016-12-01   Xox
#> 41  D_1922 2015-11-01   Xox
#> 42  D_1088 2017-05-01   Xox
#> 43  D_3460 2016-09-01   Xox
#> 44  D_1465 2016-07-01   Xox
#> 45  D_1974 2016-06-01   Xox
#> 46  D_3525 2015-10-01   Xox
#> 47  D_2650 2017-04-01 sport
#> 48  D_2691 2017-02-01   Xox
#> 49  D_3616 2017-03-01   Xox
#> 50  D_1104 2016-10-01   Xox
#> 51  D_1533 2017-10-01   Xox
#> 52  D_3431 2016-06-01   Xox
#> 53  D_3458 2016-09-01   Xox
#> 54  D_1632 2016-10-01   Xox
#> 55   D_687 2017-01-01   Xox
#> 56  D_2560 2016-03-01   Xox
#> 57  D_1545 2016-03-01   Xox
#> 58  D_3073 2016-02-01   Xox
#> 59   D_468 2017-02-01   Xox
#> 60  D_2891 2015-12-01   Xox
#> 61  D_2479 2016-05-01   Xox
#> 62   D_254 2016-11-01   Xox
#> 63  D_2410 2017-10-01   Xox
#> 64  D_3633 2016-09-01   Xox
#> 65  D_2773 2017-05-01   Xox
#> 66   D_749 2016-09-01   Xox
#> 67  D_3259 2017-02-01   Xox
#> 68   D_143 2015-11-01   Xox
#> 69  D_3272 2016-01-01   Xox
#> 70   D_545 2016-05-01   Xox
#> 71  D_1684 2017-09-01   Xox
#> 72   D_341 2017-09-01   Xox
#> 73  D_3351 2016-12-01   Xox
#> 74  D_2127 2016-04-01   Xox
#> 75  D_1364 2016-03-01   Xox
#> 76  D_3500 2017-06-01   Xox
#> 77  D_2290 2016-12-01 sport
#> 78  D_2847 2015-11-01   Xox
#> 79   D_724 2017-09-01   Xox
#> 80  D_2111 2017-02-01   Xox
#> 81  D_2225 2017-06-01   Xox
#> 82   D_720 2016-01-01   Xox
#> 83  D_2709 2016-06-01   Xox
#> 84  D_1648 2015-12-01   Xox
#> 85  D_3251 2016-10-01   Xox
#> 86   D_184 2017-06-01   Xox
#> 87   D_961 2016-07-01   Xox
#> 88  D_2671 2016-08-01   Xox
#> 89  D_3217 2017-05-01   Xox
#> 90  D_1077 2016-04-01   Xox
#> 91  D_1290 2016-05-01 sport
#> 92  D_3637 2017-08-01   Xox
#> 93   D_436 2016-10-01   Xox
#> 94  D_3819 2017-08-01 sport
#> 95    D_25 2015-10-01   Xox
#> 96   D_739 2017-05-01   Min
#> 97  D_3697 2016-08-01   Xox
#> 98  D_1851 2016-11-01   Xox
#> 99    D_50 2016-07-01   Xox
#> 100 D_1286 2016-02-01   Xox

对于完整的数据,以防万一月不足以回溯12个月:https://www.dropbox.com/s/xslrswrys0hkl32/foo_complete.csv?dl=0

2 回答

  • 0

    使用magrittr表示法定义函数 count_ids ,该函数采用逗号分隔ID的向量并输出唯一ID的数量 .

    然后将其转换为宽动态范围对象,每个组一列聚 Contract 时聚合,以便使用 toString 连接特定日期/组中的所有ID . 在其上运行 rollapply 以计算过去12个月中的唯一ID .

    最后将数据转换回长格式数据框 .

    请注意, read.zoo 将使用 NA 填写任何缺失的年/月计数,只要每个组中没有缺少年/月 . 显示的数据没有每个组中缺少的年/月,事实上,几乎没有任何缺失值,所以它应该工作 . (取消注释第一行以安装动物园的开发版本 - 这是必需的 . )

    # install.packages("zoo", repos="http://R-Forge.R-project.org")
    library(magrittr)
    library(zoo)
    
    # . is a vector of comma-separated strings of IDs
    count_ids <- . %>%
        textConnection %>%
        scan(what = "", sep = ",", quiet = TRUE, strip.white = TRUE) %>%
        unique %>%
        length
    
    foo <- read.csv("foo_complete.csv")
    foo %>%
        read.zoo(index = "Date", split = "Group", aggregate = toString) %>%
        rollapplyr(12, count_ids) %>%
        fortify.zoo(melt = TRUE, names = c("Date", "Group", "Count"))
    

    如果一个宽动物园对象可以作为输出(参见下面的输出),那么我们可以省略最后一行 .

    动物园

    如果我们希望在没有magrittr的情况下表达上述内容,我们可以写:

    library(zoo)
    
    count_ids <- function(x) {
        s <- scan(textConnection(x), what = "", sep = ",", quiet = TRUE, strip.white = TRUE)
        length(unique(s))
    }
    
    foo <- read.csv("foo_complete.csv")
    z <- read.zoo(foo, index = "Date", split = "Group", aggregate = toString)
    z.roll <- rollapplyr(z, 12, count_ids)
    fortify.zoo(z.roll, melt = TRUE, names = c("Date", "Group", "Count"))
    

    rollapplyr输出

    rollapplyr 行的输出如下(最后一行的输出太长而无法显示):

    > foo <- read.csv("foo_complete.csv")
    > foo %>%
    +     read.zoo(index = "Date", split = "Group", aggregate = toString) %>%
    +     rollapplyr(12, count_ids) 
               Min sport  Xox
    2016-07-01  17   137 2165
    2016-08-01  17   142 2273
    2016-09-01  19   149 2361
    2016-10-01  19   154 2429
    2016-11-01  21   156 2532
    2016-12-01  22   161 2578
    2017-01-01  22   163 2585
    2017-02-01  25   163 2594
    2017-03-01  25   167 2632
    2017-04-01  25   168 2648
    2017-05-01  26   166 2659
    2017-06-01  23   166 2686
    2017-07-01  24   162 2740
    2017-08-01  27   163 2758
    2017-09-01  28   162 2788
    2017-10-01  28   165 2831
    2017-11-01  28   158 2786
    2017-12-01  29   153 2688
    

    更新

    • 现在需要动物园的开发版本

    • 固定情况,其中ID出现在多个月内

    • summarize 语句合并到 read.zoo 聚合参数中,以便它现在使用magrittr和动物园

    • rollapply 中添加右对齐

    • 显示 rollapplyr 的输出

  • 2

    我曾经用rubridate包更好地处理日期 . 我修改了你的filter子句并添加了两个for循环,猜测你的第一个日期是1.1.2015 . 其余的只是你的示例代码 .

    library(lubridate)
    result_list <- list()
    for(y in 2015:2017){
      for(m in 1:12){
        d <- as.Date(paste0(y,"-",m, '-01'), format = '%Y-%m-%d')
        d2 <-  d %m-% months(12)
        result_list[[paste0(y,'_',m)]] <- foo %>%
          filter(Date >= d2 & Date < d) %>%
          group_by(ID, Group) %>%
          count() %>%
          group_by(Group) %>%
          count
    
      }
    }
    

    我把结果放到一个列表中 . 也许您必须修改它以更容易地绘制结果 .

相关问题