Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork70
Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
License
joshuaulrich/xts
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
xts is anR package that provides an extension ofthezoo class. zoo's strength comesfrom its simplicity of use (it's very similar to base R functions), and itsoverall flexibility (you can useanything as an index). The xts extensionwas motivated by the ability to improve performance by imposing reasonableconstraints, while providing a truly time-based structure.
Available as part of the Tidelift Subscription.
The maintainers ofxts and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use.Learn more.
If you are interested in supporting the ongoing development and maintenance of xts, please considerbecoming a sponsor.
The current release is available onCRAN,which you can install via:
install.packages("xts")To install the development version, you need to clone the repository and buildfrom source, or run one of:
# lightweightremotes::install_github("joshuaulrich/xts")# ordevtools::install_github("joshuaulrich/xts")
You will need tools to compile C, C++, and Fortran code. See the relevantappendix in theR Installation and Administration manualfor your operating system:
- Windows
- MacOS (theR for Mac OS X Developer's Page might also be helpful)
- Unix-alike
You can create xts objects usingxts() andas.xts().
Note thatas.xts() currently expects the date/times to be in the row namesfor matrix and data.frame objects, or in the names for vector. You can alsouse thedateFormat argument to control whether the names should be convertedtoDate orPOSIXct. Seehelp(as.xts.methods) for details.
n<-10series<- rnorm(n)# POSIXct (date/time) indexdatetimes<- seq(as.POSIXct("2017-03-27"),length.out=n,by="days")library(xts)x<- xts(series,datetimes)
In addition to the usual ways you can subset matrix and zoo objects, you canalso subset xts objects using character strings that adhere to theISO-8601 standard, which is theinternationally recognized and accepted way to represent dates and times.Using the data from the prior code block, here are some examples:
# March, 2017x["2017-03"]# [,1]# 2017-03-27 0.25155453# 2017-03-28 -0.09379529# 2017-03-29 0.44600926# 2017-03-30 0.18095782# 2017-03-31 -1.45539421# March 30th through April 2ndx["2017-03-30/2017-04-02"]# [,1]# 2017-03-30 0.1809578# 2017-03-31 -1.4553942# 2017-04-01 -0.4012951# 2017-04-02 -0.5331497# Beginning of the series to April 1stx["/2017-04-01"]# [,1]# 2017-03-27 0.25155453# 2017-03-28 -0.09379529# 2017-03-29 0.44600926# 2017-03-30 0.18095782# 2017-03-31 -1.45539421# 2017-04-01 -0.40129513
You can aggregate a univariate series, or open-high-low-close (OHLC) data, intoa lower frequency OHLC series with theto.period() function. There are alsoconvenience functions for some frequencies (e.g.to.minutes(),to.daily(),to.yearly(), etc).
data(sample_matrix)x<- as.xts(sample_matrix)to.period(x,"months")# x.Open x.High x.Low x.Close# 2007-01-31 50.03978 50.77336 49.76308 50.22578# 2007-02-28 50.22448 51.32342 50.19101 50.77091# 2007-03-31 50.81620 50.81620 48.23648 48.97490# 2007-04-30 48.94407 50.33781 48.80962 49.33974# 2007-05-31 49.34572 49.69097 47.51796 47.73780# 2007-06-30 47.74432 47.94127 47.09144 47.76719to.monthly(x)# result has a 'yearmon' index# x.Open x.High x.Low x.Close# Jan 2007 50.03978 50.77336 49.76308 50.22578# Feb 2007 50.22448 51.32342 50.19101 50.77091# Mar 2007 50.81620 50.81620 48.23648 48.97490# Apr 2007 48.94407 50.33781 48.80962 49.33974# May 2007 49.34572 49.69097 47.51796 47.73780# Jun 2007 47.74432 47.94127 47.09144 47.76719
Theperiod.apply() function allows you apply a custom function to non-overlapping intervals. You specify the intervals using a vector similar to theoutput ofendpoints(). Liketo.period() there are convenience functions,likeapply.daily(),apply.quarterly(), etc.
# Average monthly value for each columnperiod.apply(x, endpoints(x,"months"),colMeans)# Open High Low Close# 2007-01-31 50.21140 50.31528 50.12072 50.22791# 2007-02-28 50.78427 50.88091 50.69639 50.79533# 2007-03-31 49.53185 49.61232 49.40435 49.48246# 2007-04-30 49.62687 49.71287 49.53189 49.62978# 2007-05-31 48.31942 48.41694 48.18960 48.26699# 2007-06-30 47.47717 47.57592 47.38255 47.46899# Open High Low Close# 2007-01-31 50.21140 50.31528 50.12072 50.22791# 2007-02-28 50.78427 50.88091 50.69639 50.79533# 2007-03-31 49.53185 49.61232 49.40435 49.48246# 2007-04-30 49.62687 49.71287 49.53189 49.62978# 2007-05-31 48.31942 48.41694 48.18960 48.26699# 2007-06-30 47.47717 47.57592 47.38255 47.46899
Ask your question onStack Overflowor theR-SIG-Financemailing list (you must subscribe to post).
- DataCamp course on importing and managing financial data
- DataCamp course on manipulating time series with xts & zoo
Please see theContributing Guide.
- quantmod: quantitative financial modeling framework
- TTR: functions for technical tradingrules
- zoo: class for regular and irregular time series
Jeffrey Ryan,Joshua Ulrich
About
Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.