获得R中每年11月的第四个星期三 [英] Get the 4th Wednesday of each November in R

查看:245
本文介绍了获得R中每年11月的第四个星期三的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间索引的基质(XTS对象),我只希望每年十一月的第四个星期三。

I have a time-indexed matrix (xts object) and I want only the fourth Wednesday of every November.

require(quantmod)
getSymbols("^GSPC", from="1900-01-01")   #returns GSPC
GSPC$WED  <- weekdays(time(GSPC)) == "Wednesday"
GSPC$NOV  <- months(time(GSPC)) == "November"
G         <- GSPC[GSPC$WED==1 & GSPC$NOV==1]

这是据我得到了R.为了解决我的问题,我踢了抨击。

That's as far as I got in R. To solve my problem I punted up to bash.

write.zoo(G, "wen_in_nov")

我做了下面的技巧:

I did the following hack:

cat wen_in_nov |
grep -v IND |
cut -c 1-10 | 
sed 's/-/ /g' | 
awk '{if($3 >= 22 && $3 < 29) print $1, $2, $3, "winner"}' |
sed 's/ /-/g' > fourth_wen

fourth_wen 文件需要分开 - 从字符串'赢家',所以我只是做了,在六。导入到回R:

The fourth_wen file needs to separate the - from the string 'winner' so I just did that in vi. Importing into back to R:

fourth_wen <- read.zoo("fourth_wen", format="%Y-%m-%d")

和,基本上是第四个星期三在十一月自1950年以来有没有办法用更少的code做这一切中的R?

And that essentially is the fourth Wednesday in November since 1950. Is there a way to do it all in R with less code?

推荐答案

使用的 .indexmon 的等直接访问POSIXlt值

Use .indexmon etc to access the POSIXlt values directly

GSPC[.indexmon(GSPC)==10 & .indexmday(GSPC) > 22 & .indexmday(GSPC) < 29
       &.indexwday(GSPC) == 3]

           GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted
2007-11-28   1432.95   1471.62  1432.95    1469.02  4508020000       1469.02
2008-11-26    852.90    887.68   841.37     887.68  5793260000        887.68
2009-11-25   1106.49   1111.18  1104.75    1110.63  3036350000       1110.63
2010-11-24   1183.70   1198.62  1183.70    1198.35  3384250000       1198.35
2011-11-23   1187.48   1187.48  1161.79    1161.79  3798940000       1161.79

这篇关于获得R中每年11月的第四个星期三的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆