Use endpoints function to get start points instead?

我怕爱的太早我们不能终老 提交于 2019-12-18 05:02:32

问题


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

What I tried doing was:

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


回答1:


You can create a startpoints function like this

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


来源:https://stackoverflow.com/questions/26982057/use-endpoints-function-to-get-start-points-instead

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!