Convert ts (Time Series) object to vector in R

I need to use a function on a vector that does not accept a ts object. I am trying to convert it to a plain old vector, but I just can't figure it out. I googled around, but mostly people try to convert data types to ts object. I want to go the other way. Any help would be greatly appreciated.

+7
data-structures r time-series
source share
1 answer
data(AirPassengers) # already in your R installation, via package "datasets" AP = AirPassengers class(AP) # returns "ts" AP1 = as.numeric(AP) # returns "numeric" # another way to do it AP1 = unclass(AP) 

AP1 is a vector with the same values and length as AP. The class is now numeric, not ts, which means, in particular, that indexes are no longer a kind of date object, but simply ordinary consecutive integers.

So w / r / t is a specific question in OP, either of the two snippets above will "convert [ts object] to a plain old vector"

If you need to do the same with indexes, and not, or in addition to values, that is, from Date objects to numeric, you can do it like this:

 fnx = function(num_days_since_origin, origin="1970-01-01") { as.Date(num_days_since_origin, origin="1970-01-01") } a = as.Date("1985-06-11") a2 = as.numeric(a) # returns: 5640 a3 = fnx(5640) # returns: "1985-06-11" (a date object) 
+14
source share

All Articles