Subsetting “zoo” objects¶
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
as.yearqtr("1945 Q2")
[1] "1945 Q2"
The file “zpresidents.RData” was created in an earlier example.
load("zpresidents.RData")
zpresidents[as.yearqtr("1945 Q2")]
1945 Q2
87
qtrs3 <- as.yearqtr(paste(1960:1969,"Q3"))
zpresidents[qtrs3]
1960 Q3 1961 Q3 1962 Q3 1963 Q3 1964 Q3 1965 Q3 1966 Q3 1967 Q3 1968 Q3 1969 Q3
61 71 62 62 69 69 56 38 35 65
qtrs <- paste(rep(1960:1964,each=4),rep(4:1,4),sep="-")
qtrs
[1] "1960-4" "1960-3" "1960-2" "1960-1" "1961-4" "1961-3" "1961-2" "1961-1"
[9] "1962-4" "1962-3" "1962-2" "1962-1" "1963-4" "1963-3" "1963-2" "1963-1"
[17] "1964-4" "1964-3" "1964-2" "1964-1"
zpresidents[as.yearqtr(qtrs)]
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
load("unemployment-z.RData")
unemployment.z[as.Date("1997-12-31")]
Germany France Italy Netherlands Belgium Luxembourg
1997-12-31 11.412 12.438 12.251 5.59 12.691 3.616
window(zpresidents,
start = as.yearqtr("1969-1"),
end = as.yearqtr("1974-2"))
1969 Q1 1969 Q2 1969 Q3 1969 Q4 1970 Q1 1970 Q2 1970 Q3 1970 Q4 1971 Q1 1971 Q2
59 65 65 56 66 53 61 52 51 48
1971 Q3 1971 Q4 1972 Q1 1972 Q2 1972 Q3 1972 Q4 1973 Q1 1973 Q2 1973 Q3 1973 Q4
54 49 49 61 NA NA 68 44 40 27
1974 Q1 1974 Q2
28 25
window(unemployment.z,
start = as.Date("1980-12-31"),
end = as.Date("1989-12-31"))
Germany France Italy Netherlands Belgium Luxembourg
1980-12-31 3.190 6.246 5.574 4.015 8.029 0.721
1981-12-31 4.505 7.396 6.269 5.818 10.279 1.042
1982-12-31 6.441 8.041 6.918 8.519 12.030 1.302
1983-12-31 7.921 8.253 7.694 10.987 13.319 1.630
1984-12-31 7.932 9.660 8.504 10.604 13.363 1.750
1985-12-31 8.002 10.234 8.611 9.191 12.442 1.688
1986-12-31 7.661 10.373 9.896 8.394 11.792 1.478
1987-12-31 7.611 10.479 10.248 7.982 11.461 1.710
1988-12-31 7.598 9.975 10.451 7.785 10.422 1.564
1989-12-31 6.863 9.348 10.214 6.917 9.377 1.420
- R file: subsetting-zoo-objects.R
- Rmarkdown file: subsetting-zoo-objects.Rmd
- Jupyter notebook file: subsetting-zoo-objects.ipynb
- Interactive version of the Jupyter notebook (shuts down after 60s):
- Interactive version of the Jupyter notebook (sign in required):