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?
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.
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.
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.
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.
Use coredata
:
reg.curve.num <- coredata(Reg.curve)
# or, if you want a vector:
reg.curve.num <- drop(coredata(Reg.curve))
To extract the numerical values of any xts
, ts
, or zoo
object use:
as.numeric(Reg.curve)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With