使用 quantmod 仅选择每月子集 [英] Choosing only a monthly subset using quantmod
问题描述
我正在使用最近 5 年的苹果和谷歌使用 quant 模式命令
I'm using the latest 5 years of apple and google using the quant mode command
loadSymbols(c("AAPL", "GOOG"))
AAPL['2016::']
这是数据集.
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2016-01-04 25.6525 26.3425 25.5000 26.3375 270597600 24.36454
2016-01-05 26.4375 26.4625 25.6025 25.6775 223164000 23.75398
2016-01-06 25.1400 25.5925 24.9675 25.1750 273829600 23.28912
2016-01-07 24.6700 25.0325 24.1075 24.1125 324377600 22.30621
2016-01-08 24.6375 24.7775 24.1900 24.2400 283192000 22.42415
你能建议一个命令我可以用来为每年的每个月的特定日期选择 AAPL.Volume 吗?
Can you please suggest a command I can use to choose the AAPL.Volume for a particular date of every month of every year?
谢谢.
推荐答案
根据评论,我们希望从每年的 4 月开始,每个月的 15 日的音量.Vo(AAPL) 给出体积.请注意,POSIXlt 表示从 0 开始的月份,因此 4 月是第 3 个月.请注意,有些月份可能没有 15 日的任何数据.
Based on the comment we want the volume on the 15th of the month for every month from April onwards in each year. Vo(AAPL) gives the volumes. Note that POSIXlt represents months starting from 0 so April is month 3. Note that some months may not have any data on the 15th.
Vo(AAPL)[with(as.POSIXlt(time(AAPL)), mon >= 3 & mday == 15)]
如果实际想要的是在 15 日或之后有数据的第一个日期,则该年的 4 月以后,则首先提取 15 日及 4 月或之后的每个月的卷,v.ok,然后选出年月相同的元素中的第一个元素.
If what is actually wanted is the first date that has data on or after the 15th for April onwards in the year then first extract the volumes on the 15 and after in each month on or after April, v.ok, and then pick out the first element of those having the same year and month.
第一行与上面相同,只是上面的 == 被替换为 >= .
The first line is the same as above except the == above is replaced with >= .
v.ok <- Vo(AAPL)[with(as.POSIXlt(time(AAPL)), mon >= 3 & mday >= 15)]
v.ok[ !duplicated(as.yearmon(time(v.ok))) ]
更新
固定和简化.
这篇关于使用 quantmod 仅选择每月子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!