7.8 Converting a Time Series Object to Times and Values

7.8.1 Problem

You have a time series object that you wish to convert to numeric vectors representing the time and values at each time.

7.8.2 Solution

Use the time() function to get the time for each observation, then convert the times and values to numeric vectors with as.numeric():

# Look at nhtemp Time Series object
nhtemp
#> Time Series:
#> Start = 1912 
#> End = 1971 
#>  ...
#> [43] 52.0 52.0 50.9 52.6 50.2 52.6 51.6 51.9 50.5 50.9 51.7 51.4 51.7 50.8
#> [57] 51.9 51.8 51.9 53.0

# Get times for each observation
as.numeric(time(nhtemp))
#>  [1] 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
#> [15] 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
#> [29] 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
#> [43] 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
#> [57] 1968 1969 1970 1971

# Get value of each observation
as.numeric(nhtemp)
#>  [1] 49.9 52.3 49.4 51.1 49.4 47.9 49.8 50.9 49.3 51.9 50.8 49.6 49.3 50.6
#> [15] 48.4 50.7 50.9 50.6 51.5 52.8 51.8 51.1 49.8 50.2 50.4 51.6 51.8 50.9
#> [29] 48.8 51.7 51.0 50.6 51.7 51.5 52.1 51.3 51.0 54.0 51.4 52.7 53.1 54.6
#> [43] 52.0 52.0 50.9 52.6 50.2 52.6 51.6 51.9 50.5 50.9 51.7 51.4 51.7 50.8
#> [57] 51.9 51.8 51.9 53.0
# Put them in a data frame
nht <- data.frame(year = as.numeric(time(nhtemp)), temp = as.numeric(nhtemp))
nht
#>    year temp
#> 1  1912 49.9
#> 2  1913 52.3
#>  ...<56 more rows>...
#> 59 1970 51.9
#> 60 1971 53.0

7.8.3 Discussion

Time series objects efficiently store information when there are observations at regular time intervals, but for use with ggplot, they need to be converted to a format that separately represents times and values for each observation.

Some time series objects are cyclical. The presidents data set, for example, contains four observations per year, one for each quarter:

presidents
#>      Qtr1 Qtr2 Qtr3 Qtr4
#> 1945   NA   87   82   75
#> 1946   63   50   43   32
#>  ...
#> 1973   68   44   40   27
#> 1974   28   25   24   24

To convert it to a two-column data frame with one column representing the year with fractional values, we can do the same as before:

pres_rating <- data.frame(
  year = as.numeric(time(presidents)),
  rating = as.numeric(presidents)
)
pres_rating
#>        year rating
#> 1   1945.00     NA
#> 2   1945.25     87
#>  ...<116 more rows>...
#> 119 1974.50     24
#> 120 1974.75     24

It is also possible to store the year and quarter in separate columns, which may be useful in some visualizations:

pres_rating2 <- data.frame(
  year = as.numeric(floor(time(presidents))),
  quarter = as.numeric(cycle(presidents)),
  rating = as.numeric(presidents)
)
pres_rating2
#>     year quarter rating
#> 1   1945       1     NA
#> 2   1945       2     87
#>  ...<116 more rows>...
#> 119 1974       3     24
#> 120 1974       4     24

7.8.4 See Also

The zoo package is also useful for working with time series objects.