xts時間序列物件操作 及 繪圖

require(xts)

data(“sample_matrix”) #load data

matrix_xts<-as.xts(sample_matrix,dateFormat = ‘Date’) 將資料轉成xts / dateFormat=’Date’ 轉換成xts以'Date’為index

xts(1:10,Sys.Date()+1:10) 時間指定索引

head(matrix_xts[‘2007–03’],5) 取2007/03前5筆

first(last(matrix_xts,’1 week’),’3 days’) 取最後一週的前3天

繪圖

plot(matrix_xts[,4],
 major.ticks=’month’,
 minor.ticks=FALSE,
 main=NULL,
 col=1)

data {utils}

Data Sets

Description

Loads specified data sets, or list the available data sets.

as.xts.methods {xts}
Convert Object To And From Class xts

Description

Conversion S3 methods to coerce data objects of arbitrary classes to class xts and back, without losing any attributes of the original format.

Usage

## S3 method for class ‘xts’
as.xts(x,…,.RECLASS=FALSE)

## S3 method for class ‘timeSeries’
as.xts(x, dateFormat=”POSIXct”, FinCenter, recordIDs,
 title, documentation, …, .RECLASS=FALSE)

## S3 method for class ‘its’
as.xts(x,…,.RECLASS=FALSE)

## S3 method for class ‘zoo’
as.xts(x, order.by=index(x), frequency=NULL, …, .RECLASS=FALSE)

## S3 method for class ‘ts’
as.xts(x, dateFormat,…,.RECLASS=FALSE)

## S3 method for class ‘data.frame’
as.xts(x, order.by, dateFormat=”POSIXct”,
 frequency=NULL, …,.RECLASS=FALSE)

## S3 method for class ‘matrix’
as.xts(x, order.by, dateFormat=”POSIXct”,
 frequency=NULL, …, .RECLASS=FALSE)

as.fts.xts(x)
Arguments


data object to convert. See details for supported types
dateFormat 
what format should the dates be converted to
FinCenter 
see timeSeries help
recordIDs 
see timeSeries help
title 
see timeSeries help
documentation 
see timeSeries help
order.by 
see zoo help
frequency 
see zoo help
… 
additional parameters or attributes
.RECLASS 
should conversion be reversible?
Details

A simple and reliable way to convert many different objects into a uniform format for use within R.

It is possible with a call to as.xts to convert objects of class timeSeries, ts, its, matrix, data.frame, and zoo.

Additional name=value pairs may be passed to the function to be added to the new object. A special print.xts method will assure that the attributes are hidden from view, but will be available via R’s standard attr function.

If .RECLASS=TRUE, the returned object will preserve all relevant attribute/slot data within itself, allowing for temporary conversion to use zoo and xts compatible methods. A call to reclass returns the object to its original class, with all original attributes intact — unless otherwise changed. This is the default behavior when try.xts is used for conversion, and should not be altered by the user; i.e. don’t touch it unless you are aware of the consequences.

It should be obvious, but any attributes added via the … argument will not be carried back to the original data object, as there would be no available storage slot/attribute.

Value

An S3 object of class xts.

Examples

## Not run: 
 # timeSeries
 library(timeSeries)
 x <- timeSeries(1:10, 1:10)

str( as.xts(x) )
 str( reclass(as.xts(x)) )
 str( try.xts(x) )
 str( reclass(try.xts(x)) )
 
## End(Not run)

Like what you read? Give Steven Wang a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.