【问题标题】:Convert daily to weekly where week starts on every Tuesday in R将每天转换为每周在 R 中每周二开始的地方
【发布时间】:2011-03-29 10:16:38
【问题描述】:

这是代码:编辑:请参阅下面的可复制代码

>require("quantmod")   
>
> corn <- as.xts(read.zoo("~/CORN.csv", sep=",", format ="%m/%d/%Y", header=TRUE))
> 
> head(corn)
           [,1]
1962-01-03 4.03
1962-01-04 3.99
1962-01-05 4.02
1962-01-08 4.03
1962-01-09 4.05
1962-01-10 4.07
> 
> corn <- to.weekly(corn)[,4]
> 
> head(corn)
           corn.Close
1962-01-05       4.02
1962-01-12       4.08
1962-01-19       4.11
1962-01-26       4.11
1962-02-02       4.08
1962-02-09       4.05

每周二你是怎么开始的?类似于

indexAt='startof("Tuesday")'

其中 indexAt 是 to.weekly() 函数中的参数变量。

这样做的目的是与每周 COT 数据对齐。

编辑##################

由于没有提供可重现的代码,我造成了一些混乱,所以这里有一些根据 J. Winchester 的建议合并的部分解决方案:

> getSymbols("GLD")
[1] "GLD"
> GLD <- GLD[,4]
> head(GLD, n=2)
           GLD.Close
2007-01-03     62.28
2007-01-04     61.65
> tues <- weekdays(time(GLD)) == "Tuesday"
> gold <- merge(GLD, tues)
> head(gold, n=5)
           GLD.Close tues
2007-01-03     62.28    0
2007-01-04     61.65    0
2007-01-05     60.17    0
2007-01-08     60.48    0
2007-01-09     60.85    1

【问题讨论】:

    标签: r dayofweek


    【解决方案1】:

    这个怎么样(基于来自 Alaiacano 的虚拟数据框)。

    corn$tuesdays <- weekdays(corn$dates) == "Tuesday"
    
    # count days from any partial week at the beginning
    start_len <- ifelse(corn$tuesdays[1], 0, rle(corn$tuesdays)$lengths[1])
    
    # assign a week value to every row
    corn$week <- c(rep(0, start_len), 1 + seq_len(nrow(corn) - start_len) %/% 7)
    
    # concatenate the start date of the first (possibly incomplete) week
    # to the start dates for all the following weeks
    week_starts <- as.Date(corn$dates[corn$tuesdays])
    if(start_len > 0) week_starts <- c(week_starts[1] - 7, week_starts) 
    
    # calculate weekly means and assemble to a data frame
    corn_values <- aggregate(value~week, data = corn, FUN = mean)$value
    corn_weekly <- data.frame(week_starts, corn_values)
    

    【讨论】:

    • 我已将您上面的部分解决方案与可重现的代码结合在一起。
    • 在我用 time(corn) 替换了corn$dates 之后,我让它一直工作到每周均值计算,但这些是我的数据特有的问题。它适用于虚拟数据。没有参数值可以传递给 to.weekly 或一些类似的函数来让一周从星期二开始,因此需要这些额外的步骤。谢谢。
    【解决方案2】:

    可能有一个更优雅的解决方案,但这应该可以解决问题

    # generate some fake data for 2011
    corn <- data.frame(dates=as.Date("2011-01-01") + seq(0,365), value=runif(366))
    
    # get the day of week
    corn$dow <- chron::day.of.week(as.numeric(strftime(corn$dates, "%Y")),
                                   as.numeric(strftime(corn$dates, "%m")),
                                   as.numeric(strftime(corn$dates, "%d")))
    # take subset     
    corn <- subset(corn, dow==2)
    

    输出如下所示:

    > head(corn)
            dates      value dow
    11 2011-01-11 0.54688767   2
    17 2011-01-17 0.22249506   2
    22 2011-01-22 0.61725913   2
    28 2011-01-28 0.45681763   2
    36 2011-02-05 0.77839486   2
    41 2011-02-10 0.07201445   2
    

    您可以删除星期几和/或使用 row.names 代替corn$dates

    【讨论】:

    • 在我的数据上生成一堆 NA,并且在虚拟数据上似乎不稳定(不是每 7 天)。
    • 恐怕我在环境中同名的 data.frame 上尝试了您的解决方案。我的问题是一个动物园,xts 对象,所以当我说你的解决方案产生了一堆 NA 时我写错了。 corn$dates 变量不存在。您可以使用 time(corn) 来获取日期变量。
    猜你喜欢
    • 1970-01-01
    • 2014-02-05
    • 2015-05-16
    • 2021-03-17
    • 2020-06-13
    • 2013-05-02
    • 2020-12-18
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多