【问题标题】:Summarize Values By Product By Month XTS Object in RR中按月XTS对象按产品汇总值
【发布时间】:2016-10-14 12:04:03
【问题描述】:

我有一个包含 900 列 (x1-x900) 每日值的 xts 对象,我需要将其计算为每月收益。

x <- data.frame(date=seq(as.Date("2016/7/4"), as.Date("2016/10/1"), "day"),x1=runif(90,.95,1.07),x2=runif(90,.95,1.07),
      x3=runif(90,.95,1.07),x4=runif(90,.95,1.07),x5=runif(90,.95,1.07),x6=runif(90,.95,1.07),x7=runif(90,.95,1.07))

以上是我正在处理的数据示例。我需要做的是获取每个月的值的乘积,对于每一列,对于 2016 年 7 月 31 日、2016 年 8 月 31 日、2016 年 9 月 30 日等。我不想使用 @987654322 @ 因为每个月的长度显然不是固定的。我已经尝试过汇总、汇总,但我还没有弄清楚这一点,并且我试图避免必须执行“for”循环。

最终目标是获得data.frame,例如:

Date       x1      x2      x3       x4
7/31/16   1.03    0.98    1.01     1.03
8/31/16   1.01    0.95    1.03     1.01
9/30/16   0.97    1.02    0.94     0.98
10/31/16  0.99    0.98    1.01     1.04

【问题讨论】:

    标签: r aggregate xts


    【解决方案1】:
    xts.x <- xts(x[, !colnames(x) %in% "date"], order.by = x[, "date"])
    xts.x.mthly <- apply.monthly(xts.x, FUN = function(x) unlist(lapply(x, prod)))
    
    > xts.x.mthly
                      x1       x2        x3        x4        x5       x6        x7
    2016-07-31 0.9924681 1.306556 1.0919181 0.8019117 1.3563864 1.853631 0.8563263
    2016-08-31 1.4780971 1.946373 1.4265027 1.8508386 1.4926483 1.651613 1.4224733
    2016-09-30 1.5926547 1.478231 1.0414107 1.4204825 1.2540149 1.374734 1.0768668
    2016-10-01 1.0643725 1.005987 0.9813467 1.0545426 0.9964061 1.005145 1.0146190
    
    # If you want data.frame output with explicit date column:
    df.mthly <- data.frame("date" = index(xts.x.mthly), coredata(xts.x.mthly))
    

    【讨论】:

    • 这里是我的解决方案和 FXQuantTrader 的快速比较。毫无疑问,他的速度要快得多(大约 4 倍)! library(microbenchmark) ; microbenchmark(Apom = { x %&gt;% group_by(yearmon = paste(year(date), month(date), sep = "-")) %&gt;% mutate(Date = max(date)) %&gt;% group_by(Date) %&gt;% summarise_each(funs(prod), - c(yearmon, date, Date)) }, FXQuantTrader = { xts.x &lt;- xts(x[, !colnames(x) %in% "date"], order.by = x[, "date"]) apply.monthly(xts.x, FUN = function(x) unlist(lapply(x, prod))) })
    【解决方案2】:

    这里是dplyrlubridate 包的解决方案:

    set.seed(1) ; x <- data.frame(date=seq(as.Date("2016/7/4"), as.Date("2016/10/1"), "day"),x1=runif(90,.95,1.07),x2=runif(90,.95,1.07),
                    x3=runif(90,.95,1.07),x4=runif(90,.95,1.07),x5=runif(90,.95,1.07),x6=runif(90,.95,1.07),x7=runif(90,.95,1.07))
    library(dplyr) ; library(lubridate)
    x %>% 
      group_by(yearmon = paste(year(date), month(date), sep = "-")) %>% 
      summarise_each(funs(prod), - c(date, yearmon)) 
    

    它省略了您想要获得当月最后一天的部分。希望它仍然有用。

    (编辑:对于缺少的部分,这里有一个解决方法:

    x %>% 
      group_by(yearmon = paste(year(date), month(date), sep = "-")) %>% 
      mutate(Date = max(date)) %>% 
      group_by(Date) %>% 
      summarise_each(funs(prod), - c(yearmon, date, Date)) 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    相关资源
    最近更新 更多