Creating a “zoo” object from the “presidents” time series

presidents is already available as part of a standard R installation.

npresidents <- as.numeric(presidents)

The following makes use of the zoo package. You may need to install it from CRAN using the code install.packages("zoo") if you want to run this on your computer. (The package is already installed on the notebook container, however.)

library(zoo)
Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
years <- 1945:1974
quarters <- 1:4
presi.times <- yearqtr(
    rep(years,each=4) +  # each year is repeated 4 times
    rep((quarters-1)/4,30) # the quarters are repeated 30 times
)
zpresidents <- zoo(npresidents,order.by=presi.times)
zpresidents
1945 Q1 1945 Q2 1945 Q3 1945 Q4 1946 Q1 1946 Q2 1946 Q3 1946 Q4 1947 Q1 1947 Q2 
     NA      87      82      75      63      50      43      32      35      60 
1947 Q3 1947 Q4 1948 Q1 1948 Q2 1948 Q3 1948 Q4 1949 Q1 1949 Q2 1949 Q3 1949 Q4 
     54      55      36      39      NA      NA      69      57      57      51 
1950 Q1 1950 Q2 1950 Q3 1950 Q4 1951 Q1 1951 Q2 1951 Q3 1951 Q4 1952 Q1 1952 Q2 
     45      37      46      39      36      24      32      23      25      32 
1952 Q3 1952 Q4 1953 Q1 1953 Q2 1953 Q3 1953 Q4 1954 Q1 1954 Q2 1954 Q3 1954 Q4 
     NA      32      59      74      75      60      71      61      71      57 
1955 Q1 1955 Q2 1955 Q3 1955 Q4 1956 Q1 1956 Q2 1956 Q3 1956 Q4 1957 Q1 1957 Q2 
     71      68      79      73      76      71      67      75      79      62 
1957 Q3 1957 Q4 1958 Q1 1958 Q2 1958 Q3 1958 Q4 1959 Q1 1959 Q2 1959 Q3 1959 Q4 
     63      57      60      49      48      52      57      62      61      66 
1960 Q1 1960 Q2 1960 Q3 1960 Q4 1961 Q1 1961 Q2 1961 Q3 1961 Q4 1962 Q1 1962 Q2 
     71      62      61      57      72      83      71      78      79      71 
1962 Q3 1962 Q4 1963 Q1 1963 Q2 1963 Q3 1963 Q4 1964 Q1 1964 Q2 1964 Q3 1964 Q4 
     62      74      76      64      62      57      80      73      69      69 
1965 Q1 1965 Q2 1965 Q3 1965 Q4 1966 Q1 1966 Q2 1966 Q3 1966 Q4 1967 Q1 1967 Q2 
     71      64      69      62      63      46      56      44      44      52 
1967 Q3 1967 Q4 1968 Q1 1968 Q2 1968 Q3 1968 Q4 1969 Q1 1969 Q2 1969 Q3 1969 Q4 
     38      46      36      49      35      44      59      65      65      56 
1970 Q1 1970 Q2 1970 Q3 1970 Q4 1971 Q1 1971 Q2 1971 Q3 1971 Q4 1972 Q1 1972 Q2 
     66      53      61      52      51      48      54      49      49      61 
1972 Q3 1972 Q4 1973 Q1 1973 Q2 1973 Q3 1973 Q4 1974 Q1 1974 Q2 1974 Q3 1974 Q4 
     NA      NA      68      44      40      27      28      25      24      24 
str(zpresidents)
'zoo' series from 1945 Q1 to 1974 Q4
  Data: num [1:120] NA 87 82 75 63 50 43 32 35 60 ...
  Index:  'yearqtr' num [1:120] 1945 Q1 1945 Q2 1945 Q3 1945 Q4 ...
coredata(zpresidents)[1:15] # To save space we only look at the
 [1] NA 87 82 75 63 50 43 32 35 60 54 55 36 39 NA
index(zpresidents)[1:15]    # first 15 elements.
 [1] "1945 Q1" "1945 Q2" "1945 Q3" "1945 Q4" "1946 Q1" "1946 Q2" "1946 Q3"
 [8] "1946 Q4" "1947 Q1" "1947 Q2" "1947 Q3" "1947 Q4" "1948 Q1" "1948 Q2"
[15] "1948 Q3"
time(zpresidents)[1:15]
 [1] "1945 Q1" "1945 Q2" "1945 Q3" "1945 Q4" "1946 Q1" "1946 Q2" "1946 Q3"
 [8] "1946 Q4" "1947 Q1" "1947 Q2" "1947 Q3" "1947 Q4" "1948 Q1" "1948 Q2"
[15] "1948 Q3"
zpresidents[1:8]
1945 Q1 1945 Q2 1945 Q3 1945 Q4 1946 Q1 1946 Q2 1946 Q3 1946 Q4 
     NA      87      82      75      63      50      43      32 
# Saved for later use:
save(zpresidents,file="zpresidents.RData")