Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. The zoo package consists of the methods for totally ordered indexed observations. It aims at performing calculations containing irregular time series of numeric vectors, matrices & factors. The zoo package interfaces to all other time series packages on CRAN. This makes it easy to pass the time series objects between zoo & other time series packages. The zoo package is an infrastructure that tries to do all basic things well, but it doesn’t provide modeling functionality. The below set of exercises makes you understand some of zoo concepts.

The initial environment setup required to work on these exercises is as follows.

Install zoo package – `install.packages("zoo")`
Attach the package – `require("zoo")`
Download the Dataset – ```ZooData read.table(Filepath, header = TRUE, sep=",",stringsAsFactors = FALSE) -> inData ```

Answers to the exercises are available here.

Exercise 1
Coerce the ` inData ` object as zoo object into ` wZ`.
Check the class of the object ` wZ `
Observe the index of the object ` wZ `.

Exercise 2
Create a zoo object ` Z ` from the ` inData ` with ` ‘Date’ ` column as the index

Exercise 3
Get the ratio of ` Z\$DeliveryVolume to Z\$TotalVolume `
Did you get the non-numeric operation error ? There is a small catch to remember, Zoo objects need to be a matrix. If there is a character string in at least one of the values, the complete Zoo object is considered as matrix of ‘character’ type. Now make a numeric zoo object, create Zoo object` Z ` with only numeric columns from ` inData `

Create a Zoo object (Z) with `inData[3:10]` with index as Date Column
Extract only the data (without index) of zoo object Z
Get the ratio of `Z\$Deliverable.Qty to Z\$Total.Traded.Quantity`

Exercise 4
Get the mean of ` Z\$DeliveryVolume to Z\$TotalVolume ` per quarter, by using Aggregate function.
as.yearqtr – function returns the Quarter of a given date.
Get the mean of ` Z\$DeliveryVolume to Z\$TotalVolume ` per month, by using Aggregate function.
as.yearmon – function returns the Year Month of a given date.

Exercise 5
Create ` Z1` object with only price related columns (Date as index). Cols – 3:6
Create ` Z2 ` with all other quantity related columns (Date as index). Cols – 3,8:10
Merge the objects ` Z1 & Z2 to Z3 `
check if merged output ` Z3 ` is same as ` Z `

Exercise 6
Extract only the rows from 2015-Feb-01 to 2015-Feb-15 from Zoo object ` Z `

Exercise 7
``` Volume Weighted Average Price (VWAP) = Sum of (Price * Volume)/Sum of Volume myVwap <- function (x) { sum(x[,1]*x[,2])/sum(x[,2]) } ```

Find VWAP with ` Close ` as Price, ` TotalVolume ` as Volume with data of 5 prior days. Store the result in object ` ZT `
e.g. Find the VWAP of 2015-01-07 with 2015-Jan-01 to 2015-01-07.
Find VWAP with ` Close ` as Price, ` DeliveryVolume ` as Volume with data of 5 prior days. Store the result object ` ZD `
Merge the objects ` Z with ZT, ZD ` and save result in ` R `

Exercise 8
Replace the NAs in ` R[,"ZT"] ` with the monthly mean of ` ZT `
e.g. NAs of Jan month in ` R[,"ZT"] ` should be replaced with Jan months mean of ` R[,"ZT"] `

Exercise 9
Subtract the mean of each month’s VWAP ` ZT` from ` close ` price of each ` R ` row

Exercise 10
Check the rownames of object ` Z `.
Surprised, to see NULL ?, Zoo object stores the index separately and if you want to retrieve use ` index` function.Now, you know how to get index and data, make a data frame `DT ` from Zoo object ` Z `, with all the columns of ` Z ` + new column `dt`, which contains the date of each row.

Image by Kevin1086 (Own work) [CC BY-SA 3.0], via Wikimedia Commons.