<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r-weekly-bulletin-vol-xiii/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-xiii/"> R programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 1200px) 100vw, 1200px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/07/R-weekly-XIII.png?w=450&ssl=1" alt="" class="wp-image-9463" data-recalc-dims="1"></p>
<p>This week’s R bulletin covers some interesting plotting methods. </p>
<div></div>
<p>	<a rel="nofollow" href="https://twitter.com/share?text=Learn+some+interesting+R+programming+functions.+%40QuantInsti&url=https://www.quantinsti.com/blog/r-weekly-bulletin-vol-xiii/" data-link="https://twitter.com/share?text=Learn+some+interesting+R+programming+functions.+%40QuantInsti&amp;url=https://www.quantinsti.com/blog/r-weekly-bulletin-vol-xiii/" target="_blank"><span><span>We will also cover functions like sapply, vapply rollMean, and group_by. </span><span>Click To Tweet<i></i></span></span></a>Hope you like this R weekly bulletin. Enjoy reading!</p>
<h3>Shortcut Keys</h3>
<p>1. Source a file – Ctrl+Shift+O<br>
2. Source the current document – Ctrl+Shift+S<br>
3. Find in Files – Ctrl+Shift+F</p>
<h3>Problem Solving Ideas</h3>
<h4>Using Multiple Colors for Plotting a Variable</h4>
<p>Plotting a variable with multiple colors allows one to discern the pattern in the plotted data easily, and can be done using the “col” argument. Remaining arguments to the plot function remains mostly the same. Let us consider an example to illustrate multi-color plotting.</p>
<p>Example: Here, we are sourcing the one-year data for the NSE listed MRF stock. Using the dailyReturn function from the quantmod package we compute the daily returns based on the daily closing price for the stock.</p>
<p>We intend to have the positive returns plotted in darkgreen color, and the negative returns in red. To do so, we create a vector called “colors” as shown below using the ifelse statement. The ifelse statement works on a vector, and we call it on the “returns” vector.</p>
<p>This “colors” vector is used as the value for the “col” argument in the plot function. The “type” argument in the plot function has to be kept as “h”. Remaining parameters used in the plot function are self-explanatory.</p>
<pre>library(quantmod)
mrf = getSymbols(&quot;MRF.NS&quot;, src=&quot;yahoo&quot;, from=&quot;2017-01-01&quot;, to=&quot;2017-06-01&quot;, auto.assign=FALSE)
returns = dailyReturn(Cl(mrf))*100
date = index(mrf)
colors = ifelse(returns >= 0, &quot;darkgreen&quot;, &quot;red&quot;)
plot(date,returns, type=&quot;h&quot;,lwd=2, col=colors, xlab=&quot;Period&quot;, ylab=&quot;Daily returns(%)&quot;, main=&quot;MRF Daily Returns for 1H-2017&quot;)</pre>
<p><img sizes="(max-width: 544px) 100vw, 544px" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/07/R-XIII-5.png?w=450&ssl=1" alt="" class="wp-image-9470" data-recalc-dims="1"></p>
<h4>Displaying Multiple Plotting on a Single Page</h4>
<p>For displaying multiple plots on a single page we divide the graphics window into a matrix using the par(mfrow) function. We specify the number of rows and columns in the mfrow argument of the par function. For example, mfrow = c(2,2) will divide the space into four parts.</p>
<p>We then call the high-level function to plot our datasets. If we want to plot four datasets and display them separately, we will call them four times using the high-level function. The example given below illustrates the same.</p>
<p><strong>Example:</strong></p>
<pre>library(quantmod)
ktkbank=getSymbols(&quot;KTKBANK.NS&quot;, src=&quot;yahoo&quot;, from=&quot;2016-01-01&quot;, to=&quot;2016-12-31&quot;,auto.assign=FALSE)
unionbank=getSymbols(&quot;UNIONBANK.NS&quot;,src=&quot;yahoo&quot;,from=&quot;2016-01-01&quot;,to=&quot;2016-12-31&quot;,auto.assign=FALSE)

ktkbank_close = coredata(Cl(ktkbank))
unionbank_close = coredata(Cl(unionbank))
date = index(ktkbank)

par(mfrow = c(1, 2))
plot(date, ktkbank_close, type = &quot;l&quot;, lty = 1, pch = 19, col = &quot;red&quot;, xlab = &quot;Period&quot;,
ylab = &quot;Price&quot;, main = &quot;KTKBANK&quot;)
plot(date, unionbank_close, type = &quot;l&quot;, lty = 1, pch = 19, col = &quot;blue&quot;, xlab = &quot;Period&quot;,ylab = &quot;Price&quot;, main = &quot;UNIONBANK&quot;)</pre>
<p><img sizes="(max-width: 485px) 100vw, 485px" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/07/R-XIII-6.png?w=450&ssl=1" alt="" class="wp-image-9471" data-recalc-dims="1"></p>
<h4>Changing Global Parameters for Plotting</h4>
<p>Many functions take multiple arguments. For some of these arguments, R has set default values. These default values help save time since we do not have to set a value for each and every argument of the function.</p>
<p>If we do not want to use a default value and set our own value for any particular argument of a function, this can be done using the par function. One can check the current default value of an argument by calling the par function with the argument name.</p>
<p><strong>Example:</strong></p>
<pre>par(&quot;col&quot;)</pre>
<p>[1] “black</p>
<p>In order to set a new value of one’s choice, we can set the same by calling the par function and assign the new value to the argument.</p>
<p><strong>Example:</strong></p>
<pre>par(col=&quot;blue&quot;)</pre>
<p>There is a long list of such arguments whose default value can be changed using the par function. Refer to the help page for the par function which lists down such arguments. Note that when you change the default value of a global parameter it will affect all your plots, not just the current one. To reset the original value, assign the argument its original value after you have done with the plotting. The example below illustrates the same:</p>
<pre>library(quantmod)
# make a copy of current settings
original_value = par(&quot;lty&quot;)
print(original_value)</pre>
<p>[1] “solid”</p>
<pre># set the global parameter using the par function
par(lty =&quot;dashed&quot;)
# create a plot with the new setting
idbi=getSymbols(&quot;IDBI.NS&quot;, src=&quot;yahoo&quot;, from=&quot;2016-01-01&quot;, to=&quot;2016-12-01&quot;,auto.assign=FALSE)
high = coredata(Hi(idbi))
low = coredata(Lo(idbi))
date = index(idbi)
main = &quot;IDBI Daily Price Chart for 2016&quot;
plot(date, high, type = &quot;l&quot;, pch = 19, col = &quot;red&quot;, xlab = &quot;Date&quot;, ylab = &quot;Price&quot;, main = main)</pre>
<p><img sizes="(max-width: 503px) 100vw, 503px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/07/R-XIII-7.png?w=450&ssl=1" alt="" class="wp-image-9472" data-recalc-dims="1"></p>
<pre># Restore the global parameter to its default value
par(lty = original_value)</pre>
<h3>Functions Demystified</h3>
<h4>sapply and vapply functions</h4>
<p>The sapply function is a user-friendly version and wrapper of lapply, it takes a list as the input and returns a vector. If the “simplify” argument is entered as “array”, the sapply function will return an array.</p>
<pre>sapply(x, f, simplify = FALSE, USE.NAMES = FALSE) is the same as lapply(x, f)</pre>
<p><strong>Example:</strong></p>
<pre>x = list(a = 1:10, b = 11:15, c = 1:50)
sapply(x, FUN = length)</pre>
<p>a b c<br>
10 5 50</p>
<p>The vapply function is similar to sapply, but has a pre-specified type of return value, so it can be safer and sometimes faster to use.</p>
<p><strong>Example:</strong></p>
<pre># We are specifying the return value to be an integer using the FUN.VALUE argument
x = list(a = 1:10, b = 11:15, c = 1:50)
vapply(x, FUN = length, FUN.VALUE = 0L)</pre>
<p>a b c<br>
10 5 50</p>
<h4>group_by function</h4>
<p>This function is part of the dplyr package, and is used to group a given dataset. One can perform different operations on such grouped data. The syntax of the function is given as:</p>
<pre>group_by(data, variables, add = FALSE)</pre>
<p>where,<br>
data – is the given data set<br>
variables – the name of the variables to group by.<br>
add – By default, when add = FALSE, group_by will override existing groups. To instead add to the existing groups, use add = TRUE</p>
<p><strong>Example:</strong> In this example, the NIFTY file contains 3 days of 1-minute intraday data. The NSE trading session starts at 9:15 am and ends at 3:30 pm IST. We call the group_by function using the “Time” variable and then count the number of observations for each time period (minute) mentioned in the data file. For this, we call the summarise function on the grouped data and use the n() function to count the number of observations for each time period. As can be seen from the output, we have 3 observations for each time period from 916 to 1530.</p>
<pre>library(dplyr)
df = read.csv(&quot;NIFTY_3days_intraday.csv&quot;)
colnames(df) = c(&quot;Date&quot;, &quot;Time&quot;, &quot;Close&quot;, &quot;High&quot;, &quot;Low&quot;, &quot;Open&quot;, &quot;Volume&quot;)
dt = group_by(df, Time)
sm = summarise(dt, n = n())
print(sm)</pre>
<p><img sizes="(max-width: 666px) 100vw, 666px" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/07/R-XIII-1.png?w=450&ssl=1" alt="" class="wp-image-9465" data-recalc-dims="1"></p>
<p>rollMean rollMin, and rollMax functions</p>
<p>These functions are part of the timeSeries package. They compute rolling mean, min, and max for a time Series object. The syntax for the rollMean is given as:</p>
<pre>rollMean(x, k, na.pad = FALSE)</pre>
<p>where,<br>
x is a uni or multivariate ‘timeSeries’ object.<br>
k is an integer width of the rolling window.<br>
na.pad is a logical flag for padding. By default it is FALSE.</p>
<p><strong>Example:</strong></p>
<pre>library(timeSeries); library(quantmod);
data = getSymbols(&quot;SBIN.NS&quot;, src=&quot;yahoo&quot;, from=&quot;2017-01-01&quot;, to=&quot;2017-01-15&quot;, auto.assign=FALSE)
open = timeSeries(data$SBIN.NS.Open)
print(head(open, 5))</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/07/R-XIII-2.png?w=450&ssl=1" alt="" class="wp-image-9466" data-recalc-dims="1"></p>
<pre>rollMean(open, k = 4, na.pad = FALSE)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/07/R-XIII-3.png?w=450&ssl=1" alt="" class="wp-image-9467" data-recalc-dims="1"></p>
<p>The other two functions work in a similar manner as rollMean.</p>
<p><strong>Example:</strong></p>
<pre>library(timeSeries); library(quantmod);
data = getSymbols(&quot;IOC.NS&quot;, src=&quot;yahoo&quot;, from=&quot;2017-01-01&quot;, to=&quot;2017-01-15&quot;, auto.assign=FALSE)
close = timeSeries(data$IOC.NS.Close)
rollMax(close, k = 5, na.pad = FALSE)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/07/R-XIII-4.png?w=450&ssl=1" alt="" class="wp-image-9468" data-recalc-dims="1"></p>
<h3>Next Step</h3>
<p>We hope you liked this bulletin. In the next weekly bulletin, we will list more interesting ways and methods plus R functions for our readers.</p>
<div><a rel="nofollow" href="https://www.quantinsti.com/wp-content/uploads/2017/07/QuantInsti_R_Bulletin-XIII%E2%84%A2.pdf" target="_blank">Download the PDF Now!</a></div>
<p>The post <a rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-xiii/" target="_blank">R Weekly Bulletin Vol – XIII</a> appeared first on <a rel="nofollow" href="https://www.quantinsti.com/" target="_blank"></a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-xiii/"> R programming</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/6w7ObAZ4jxE" height="1"></a></div>