Converting a ts (Time Series) object to a Vector in R Converting a ts (Time Series) object to a Vector in R r r

Converting a ts (Time Series) object to a Vector in R


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 itAP1 = unclass(AP)

AP1 is a vector with the same values and length as AP. The class is now numeric instead of ts, which means, in part that the indices are no longer some sort of date-time object but just ordinary sequential integers.

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

If you need to do the same thing with the indices rather than, or in addition to, the values--ie, from Date objects to numeric, you can do that like so:

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: 5640a3 = fnx(5640)# returns: "1985-06-11" (a date object)