rtime-seriesr-colnamesr-rownames

How to access row and column names (year and month) from a (monthly) time series, when rownames and colnames fail?


In monthly time series (frequency = 12), R prints out the years and months automatically, arranging them to look like row names and column names. But they are not accessible through rownames or colnames. How can one access them?

Using the AirPassengers time series:

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201

It appears that we have row names of years: 1949, 1950, 1951, ... and column names of the month names Jan, Feb, Mar, ...

I'd like to access those row and column name values, but the usual expected functions fail, probably because this is a time series.

> rownames(AirPassengers)
NULL
> colnames(AirPassengers)
NULL
> names(AirPassengers)
NULL

I know that the time function is intended for time series, but it returns the same dataframe-like format.

> time(AirPassengers)
          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug      Sep      Oct      Nov      Dec
1949 1949.000 1949.083 1949.167 1949.250 1949.333 1949.417 1949.500 1949.583 1949.667 1949.750 1949.833 1949.917
1950 1950.000 1950.083 1950.167 1950.250 1950.333 1950.417 1950.500 1950.583 1950.667 1950.750 1950.833 1950.917
1951 1951.000 1951.083 1951.167 1951.250 1951.333 1951.417 1951.500 1951.583 1951.667 1951.750 1951.833 1951.917
1952 1952.000 1952.083 1952.167 1952.250 1952.333 1952.417 1952.500 1952.583 1952.667 1952.750 1952.833 1952.917
1953 1953.000 1953.083 1953.167 1953.250 1953.333 1953.417 1953.500 1953.583 1953.667 1953.750 1953.833 1953.917

> rownames(time(AirPassengers))
NULL
> colnames(time(AirPassengers))
NULL
> names(time(AirPassengers))
NULL

I'm sure I could convert 1949.000 into Jan 1949 with some clever programming, but I'd much rather be able to access the column and row name of AirPassengers[1] directly. Since R prints them in a way that looks like row and column names...

Is there a function that can access those Year and Month values that are displayed like row and column names for time series?

Like so: 1949, 1950, 1951, ... and Jan, Feb, Mar, Apr, ...

This format is also acceptable: Jan 1949, Feb 1949, ...

Edit: thank you everyone for the quick responses! Please note the ideal method should also work for time series with different frequencies and less helpful column names, such as UKgas, with "Qtr1" "Qtr2" "Qtr3" "Qtr4" as the column names.


Solution

  • "rownames(time(AirPassengers))" --you're close. Combine rownames()/colnames() with .preformat.ts():

    strtoi(rownames(.preformat.ts(AirPassengers)))
    #>  [1] 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
    colnames(.preformat.ts(AirPassengers))
    #>  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"