【问题标题】:How to create a date vector every 6 months using R?如何使用 R 每 6 个月创建一个日期向量?
【发布时间】:2015-02-01 23:38:21
【问题描述】:

我想创建一个为期 14 年的 6 个月间隔向量。目的是我必须使用 PerformanceAnalytics 保存一些图:更具体地说:charts.PerformanceSummary() 每六个月。我不想手动输入每 6 个月的范围,而是想创建一个带有日期的向量,以便我可以使用循环来促进该过程。

我希望向量包含从 01-01-2000 到 12-31-2014 的日期,格式如下:

    DATES
"200001/200006"
"200007/200012"
"200101/200106"
"200107/200112"
"200201/200206"
"200207/200212"
...
"201407/201412"

但是我不知道该怎么做!

例如:

library(PerformanceAnalytics)

# Load DATA "edhec"
data(edhec)

# Now I can view the chart from "01-01-1999" to "06-30-1999"
charts.PerformanceSummary(edhec[,1]["199901/199906"])

但如果我有 DATES 向量,我可以执行以下操作:

# Save formatted dates to "DATES"
 DATES <- c("200001/200006","200007/200012")

# Plot Selected Dates only
 charts.PerformanceSummary(edhec[DATES[1]])

# OR:
charts.PerformanceSummary(edhec[DATES[2]])

感谢任何帮助!!!

【问题讨论】:

    标签: r date xts


    【解决方案1】:

    你可以试试

    indx <- seq(as.Date('2000-01-01'), length.out=30, by='6 month')
    indx2 <- seq(as.Date('2000-06-01'), length.out=30, by='6 month')
    DATES <- paste(format(indx, '%Y%m'), format(indx2, '%Y%m'), sep="/")
    DATES
    # [1] "200001/200006" "200007/200012" "200101/200106" "200107/200112"
    # [5] "200201/200206" "200207/200212" "200301/200306" "200307/200312"
    # [9] "200401/200406" "200407/200412" "200501/200506" "200507/200512"
    # [13] "200601/200606" "200607/200612" "200701/200706" "200707/200712"
    # [17] "200801/200806" "200807/200812" "200901/200906" "200907/200912"
    # [21] "201001/201006" "201007/201012" "201101/201106" "201107/201112"
    # [25] "201201/201206" "201207/201212" "201301/201306" "201307/201312"
    # [29] "201401/201406" "201407/201412"
    

    或者您可以在 loop 中创建两个日期

    DATES1 <- Reduce(function(...) paste(..., sep="/"),
               lapply(as.Date(c('2000-01-01', '2000-06-01')), 
                function(x) format(seq(x,length.out=30, by='6 month'), '%Y%m')))
    
    identical(DATES, DATES1)
    #[1] TRUE
    

    【讨论】:

    • 惊人的@akrun 正是我想要的,非常感谢!
    【解决方案2】:

    这里和lubridate一样

    library(lubridate)
    idx1 <- ymd("2000-01-01") + months(seq(from=0, length.out=30, by=6))
    idx2 <- idx1 + months(5)
    paste0(format(idx1, "%Y%m"), "/", format(idx2, "%Y%m"))
    
    ##  [1] "200001/200006" "200007/200012" "200101/200106" "200107/200112"
    ##  [5] "200201/200206" "200207/200212" "200301/200306" "200307/200312"
    ##  [9] "200401/200406" "200407/200412" "200501/200506" "200507/200512"
    ## [13] "200601/200606" "200607/200612" "200701/200706" "200707/200712"
    ## [17] "200801/200806" "200807/200812" "200901/200906" "200907/200912"
    ## [21] "201001/201006" "201007/201012" "201101/201106" "201107/201112"
    ## [25] "201201/201206" "201207/201212" "201301/201306" "201307/201312"
    ## [29] "201401/201406" "201407/201412"
    

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 2022-01-01
      • 2015-12-05
      • 1970-01-01
      相关资源
      最近更新 更多