<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/zoo-time-series-exercises/"></div></div><div><a data-url="http://www.r-bloggers.com/zoo-time-series-exercises/" data-counturl="http://www.r-bloggers.com/zoo-time-series-exercises/" data-count="horizontal" data-text="zoo time series exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/05/30/zoo-time-series-exercises/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://i0.wp.com/r-exercises.com/wp-content/uploads/2016/05/DallasZooEntryPlaza.jpg"><img alt="DallasZooEntryPlaza" class="wp-image-848" src="http://i0.wp.com/r-exercises.com/wp-content/uploads/2016/05/DallasZooEntryPlaza.jpg?resize=256%2C192" data-recalc-dims="1"></a>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 &amp; 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 &amp; 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.</p>
<p>The initial environment setup required to work on these exercises is as follows.</p>
<p>Install zoo package – <code>install.packages(&quot;zoo&quot;)</code><br>
Attach the package – <code>require(&quot;zoo&quot;)</code><br>
Download the Dataset – <code><a target="_blank" href="http://r-exercises.com/wp-content/uploads/2016/05/ZooData.csv">ZooData</a><br>
read.table(<strong><em>Filepath</em></strong>, header = TRUE, sep=&quot;,&quot;,stringsAsFactors = FALSE) -> inData<br>
</code></p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/05/30/zoo-time-series-solutions/">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Coerce the <code> inData </code> object as zoo object into <code> wZ</code>.<br>
Check the class of the object <code> wZ </code><br>
Observe the index of the object <code> wZ </code>.</p>
<p><strong>Exercise 2</strong><br>
Create a zoo object <code> Z </code> from the <code> inData </code> with <code> ‘Date’ </code> column as the index</p>
<p><strong>Exercise 3</strong><br>
Get the ratio of <code> Z$DeliveryVolume to Z$TotalVolume </code><br>
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<code> Z </code> with only numeric columns from <code> inData </code></p>
<p>Create a Zoo object (Z) with <code>inData[3:10]</code> with index as Date Column<br>
Extract only the data (without index) of zoo object Z<br>
Get the ratio of <code>Z$Deliverable.Qty to Z$Total.Traded.Quantity</code></p>
<p><strong>Exercise 4</strong><br>
Get the mean of <code> Z$DeliveryVolume to Z$TotalVolume </code> per quarter, by using Aggregate function.<br>
as.yearqtr – function returns the Quarter of a given date.<br>
Get the mean of <code> Z$DeliveryVolume to Z$TotalVolume </code> per month, by using Aggregate function.<br>
as.yearmon – function returns the Year Month of a given date.</p>
<p><strong>Exercise 5</strong><br>
Create <code> Z1</code> object with only price related columns (Date as index). Cols – 3:6<br>
Create <code> Z2 </code> with all other quantity related columns (Date as index). Cols – 3,8:10<br>
Merge the objects <code> Z1 &amp; Z2 to Z3 </code><br>
check if merged output <code> Z3 </code> is same as <code> Z </code></p>
<p><strong>Exercise 6</strong><br>
Extract only the rows from 2015-Feb-01 to 2015-Feb-15 from Zoo object <code> Z </code></p>
<p><strong>Exercise 7</strong><br>
<code><br>
Volume Weighted Average Price (VWAP) = Sum of (Price * Volume)/Sum of Volume<br>
myVwap &lt;- function (x) {<br>
  sum(x[,1]*x[,2])/sum(x[,2])<br>
}<br>
</code></p>
<p>Find VWAP with <code> Close </code> as Price, <code> TotalVolume </code> as Volume with data of 5 prior days. Store the result in object <code> ZT </code><br>
e.g. Find the VWAP of 2015-01-07 with 2015-Jan-01 to 2015-01-07.<br>
Find VWAP with <code> Close </code> as Price, <code> DeliveryVolume </code> as Volume with data of 5 prior days. Store the result object <code> ZD </code><br>
Merge the objects <code> Z with ZT, ZD </code> and save result in <code> R </code></p>
<p><strong>Exercise 8</strong><br>
Replace the NAs in <code> R[,&quot;ZT&quot;] </code> with the monthly mean of <code> ZT </code><br>
e.g. NAs of Jan month in <code> R[,&quot;ZT&quot;] </code> should be replaced with Jan months mean of <code> R[,&quot;ZT&quot;] </code></p>
<p><strong>Exercise 9</strong><br>
Subtract the mean of each month’s VWAP <code> ZT</code> from <code> close </code> price of each <code> R </code> row</p>
<p><strong>Exercise 10</strong><br>
Check the rownames of object <code> Z </code>.<br>
Surprised, to see NULL ?, Zoo object stores the index separately and if you want to retrieve use <code> index</code> function.Now, you know how to get index and data, make a data frame <code>DT </code> from Zoo object <code> Z </code>, with all the columns of <code> Z </code> + new column <code>dt</code>, which contains the date of each row.</p>
<p>Image by Kevin1086 (Own work) [<a target="_blank" href="http://creativecommons.org/licenses/by-sa/3.0">CC BY-SA 3.0</a>], <a target="_blank" href="https://commons.wikimedia.org/wiki/File%3ADallasZooEntryPlaza.jpg">via Wikimedia Commons</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/zoo-time-series-exercises/"></div></div><div><a data-url="http://www.r-bloggers.com/zoo-time-series-exercises/" data-counturl="http://www.r-bloggers.com/zoo-time-series-exercises/" data-count="vertical" data-text="zoo time series exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/05/30/zoo-time-series-exercises/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/fwREVAn6e4s"></a></div>