Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting the numerical values of a xts object

Tags:

r

xts

I want to extract the numerical values of a xts object. Let's look at an example

data <- new.env()
starting.date <- as.Date("2006-01-01")
nlookback <- 20
getSymbols("UBS", env = data, src = "yahoo", from = starting.date)
Reg.curve <- rollapply(Cl(data$UBS), nlookback, mean, align="right")

The Reg.cuve is still a xts object but actually I'm just interested in the running means. How can I modify Reg.curve to get a numerical vector?

like image 235
math Avatar asked Apr 05 '14 13:04

math


People also ask

What is an XTS object in R?

xts objects are simple. Think of them as a matrix of observations combined with an index of corresponding dates and times. xts = matrix + times. The main xts constructor takes a number of arguments, but the two most important are x for the data and order.by for the index.

How do I convert data to XTS objects in R?

Example: Converting Data Frame to xts / zoo Object frame class to a time series object (i.e. xts or zoo). First, we have to convert our character string variable to the Date class. As you can see, we switched the class from data. frame to xts / zoo.

What is a zoo object in R?

zoo is an R package providing an S3 class with methods for indexed totally ordered observations, such as discrete irregular time series. Its key design goals are independence of a particular index/time/date class and consistency with base R and the "ts" class for regular time series.

What is a time index in R?

Time Index A time series is a series of data points indexed in time order. In R, all data types for which an order is defined can be used to index a time series. If the operator. < is defined for a data type, then the data type can be used to index a time series.


2 Answers

Use coredata:

reg.curve.num <- coredata(Reg.curve)
# or, if you want a vector:
reg.curve.num <- drop(coredata(Reg.curve))
like image 160
Joshua Ulrich Avatar answered Nov 01 '22 21:11

Joshua Ulrich


To extract the numerical values of any xts, ts, or zoo object use:

as.numeric(Reg.curve)
like image 1
mpetric Avatar answered Nov 01 '22 19:11

mpetric