将每日转换为每周,其中 R 中的每周从每周二开始

发布于 2024-10-27 13:05:02 字数 1198 浏览 4 评论 0原文

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

>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")'

类似于to.weekly() 函数中 indexAt 是参数变量

。这样做的目的是与每周 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

Here is the code: EDIT: SEE REPRODUCIBLE CODE BELOW

>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

How do you start every week on Tuesday? Something along the lines of

indexAt='startof("Tuesday")'

where indexAt is a parameter variable in the to.weekly() function.

The purpose for this is to line up with weekly COT data.

EDIT ##################

I've created some confusion by not providing reproducible code, so here is some with a partial solution incorporated based on suggestions by J. Winchester below:

> 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-11-03 13:05:02

这个怎么样(基于 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)

How about this (based on the dummy data frame from 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)
最丧也最甜 2024-11-03 13:05:02

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

# 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

There may be a more elegant soltuion, but this should do the trick

# 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)

Output looks like this:

> 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

You can drop the day of week and/or use the row.names instead of corn$dates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文