使用端点函数来获取起点? [英] Use endpoints function to get start points instead?

查看:27
本文介绍了使用端点函数来获取起点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Daily_Quotes 的 xts 对象,其中包含股票报价.我正在使用 endpoints 来获取我使用 getSymbols(来自 quantmod 包)检索到的每月股票报价.我注意到 endpoints 函数创建了包含特定月份最后一个交易日的行的索引,并将其分配给指定日期范围的新对象.有没有办法获得本月的第一个交易日?

I have an xts object called Daily_Quotes that contains stock quotes. I'm using endpoints to get monthly stock quotes that I retrieved using getSymbols (from the quantmod package). I noticed that the endpoints function creates an index of the row that contains the last trading day for the particular month and assigns it to the new object for the specified date range. Is there anyway to get first trading day of the month instead?

# My code    
Monthly_Quotes <- Daily_Quotes[endpoints(Daily_Quotes,'months')]

我尝试做的是:

# This gave me the next day or 1st day of the next month
# or next row for the object.
endpoints(Daily_Quotes,'months') + 1

# So I applied this and it gave me 
# Error in `[.xts`(Daily_Quotes, endpoints(Daily_Quotes, "months") + 1) :
# subscript out of bounds
Monthly_Quotes <- Daily_Quotes[endpoints(Daily_Quotes,'months') + 1]

我该如何尝试解决这个问题?

How do I attempt to solve this ?

推荐答案

你可以像这样创建一个 startpoints 函数

You can create a startpoints function like this

startpoints <- function (x, on = "months", k = 1) {
  head(endpoints(x, on, k) + 1, -1)
}

这篇关于使用端点函数来获取起点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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