0% found this document useful (0 votes)
12 views15 pages

Chapter 1

This document introduces xts and zoo objects for manipulating time series data in R. It discusses that xts objects combine a matrix containing the observations with an index containing the timestamps. It provides an example of creating an xts object from a matrix and date index. It also describes how to extract the data and index components from an xts object and discusses converting between classes, importing/exporting xts objects from external files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Chapter 1

This document introduces xts and zoo objects for manipulating time series data in R. It discusses that xts objects combine a matrix containing the observations with an index containing the timestamps. It provides an example of creating an xts object from a matrix and date index. It also describes how to extract the data and index components from an xts object and discusses converting between classes, importing/exporting xts objects from external files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Introducing xts and

zoo objects
M A N I P U L AT I N G T I M E S E R I E S D ATA W I T H X T S A N D Z O O I N R

Je rey Ryan
Creator of xts and quantmod
What is xts?
eXtensible Time Series

An extended zoo object

Matrix + Index

Observations + Times

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


An xts example
# XTS = MATRIX + INDEX
x <- matrix(1:4, ncos = 2, nrow = 2)
x

[,1] [,2]
[1,] 1 3
[2,] 2 4

idx <- as.Date(c("2015-01-01", "2015-02-01"))


idx

"2015-01-01" "2015-02-01"

Class: Date , POSIX times , timeDate , chron ,…

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


An xts example
# XTS = MATRIX + INDEX
X <- xts(x, order.by = idx)
X

[,1] [,2]
2015-01-01 1 3
2015-02-01 2 4

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


The xts constructor
xts(x = NULL,
order.by = index(x),
frequency = NULL,
unique = NULL,
tzone = Sys.getenv("TZ"))

tzone: time zone of your series

unique: forces times to be unique

index is in increasing order of time

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


An xts example
# XTS = MATRIX + INDEX
X <- xts(x, order.by = idx)
X

[,1] [,2]
2015-01-01 1 3
2015-02-01 2 4

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


Special xts behavior
xts is a matrix with associated times for each observation

Subsets preserve matrix form

A ributes are preserved


i.e. a time-stamp that was acquired

xts is a subclass of zoo

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


Deconstructing xts
Use internal components

coredata(x) is used to extract the data component

coredata(x, fmt = FALSE)

index(x) to extract the index a.k.a. times

index(x)

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


Let's practice!
M A N I P U L AT I N G T I M E S E R I E S D ATA W I T H X T S A N D Z O O I N R
Importing, exporting
and converting time
series
M A N I P U L AT I N G T I M E S E R I E S D ATA W I T H X T S A N D Z O O I N R

Je rey Ryan
Creator of xts and quantmod
Reality check
Data usually already exists, and needs wrangling
O en data isn’t in your preferred class

Data needs to be imported into R and converted to xts

You will convert, read and export xts objects

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


Converting using as.xts()
# Load data from R datasets head(sunspots_xts)
data(sunspots)
class(sunspots)
[,1]
Jan 1749 58.0
"ts" Feb 1749 62.6
Mar 1749 70.0
Apr 1749 55.7
sunspots_xts <- as.xts(sunspots)
May 1749 85.0
class(sunspots_xts)
Jun 1749 83.5

"xts" "zoo"

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


Importing external data to xts
Read data into R using built in (or external) functions
i.e. read.table() , read.csv() , and read.zoo()

Coerce data to xts using

as.xts(read.table("file"))
as.xts(read.zoo("file"))

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


Exporting xts from R
Sometimes you will need your data outside of R

Use write.zoo() for external use (i.e. text les)

write.zoo(x, "file")

Use saveRDS for R use

saveRDS(x, "file")

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R


Let's practice!
M A N I P U L AT I N G T I M E S E R I E S D ATA W I T H X T S A N D Z O O I N R

You might also like