<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-x/"></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-x/"> 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: 900px) 100vw, 900px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/R-weekly-X-1024x536.png?w=450&ssl=1" alt="" class="wp-image-8984" data-recalc-dims="1"></p>
<p>This week’s R bulletin will cover topics on grouping data using ntile function, how to open files automatically, and formatting an Excel sheet using R. </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-x/" 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-x/" target="_blank"><span><span>We will also cover functions like the choose function, sample function, runif and rnorm function. </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. Fold selected chunk – Alt+L<br>
2. Unfold selected chunk – Shift+Alt+L<br>
3. Fold all – Alt+0</p>
<h3>Problem Solving Ideas</h3>
<h4>Grouping data using ntile function</h4>
<p>The ntile function is part of the dplyr package, and is used for grouping data. The syntax for the function is given by:</p>
<p>ntile(x, n)</p>
<p>Where,<br>
“x” is the vector of values and<br>
“n” is the number of buckets/groups to divide the data into.</p>
<p><strong>Example:</strong></p>
<p>In this example, we first create a data frame from two vectors, one comprising of Stock symbols, and the other comprising of their respective prices. We then group the values in Price column in 2 groups, and the ranks are populated in a new column called “Ntile”. In the last line we are selecting only those values which fall in the 2nd bucket using the subset function.</p>
<pre>library(dplyr)
Ticker = c(&quot;PAGEIND&quot;, &quot;MRF&quot;, &quot;BOSCHLTD&quot;, &quot;EICHERMOT&quot;, &quot;TIDEWATER&quot;)
Price = c(14742, 33922, 24450, 21800, 5519)

data = data.frame(Ticker, Price)

data$Ntile = ntile(data$Price, 2)
print(data)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/Rweekly-pic1.png?w=450&ssl=1" alt="" class="wp-image-8987" data-recalc-dims="1"></p>
<pre>ranked_data = subset(data, subset = (Ntile == 2))
print(ranked_data)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/Rweekly-pic2.png?w=450&ssl=1" alt="" class="wp-image-8988" data-recalc-dims="1"></p>
<h4>Automatically open the saved files</h4>
<p>If you are saving the output returned upon executing an R script, and also want to open the file post running the code, one can you use the shell.exec function. This function opens the specified file using the application specified in the Windows file associations.</p>
<p>A file association associates a file with an application capable of opening that file. More commonly, a file association associates a class of files (usually determined by their filename extension, such as .txt) with a corresponding application (such as a text editor).</p>
<p>The example below illustrates the usage of the function.<br>
shell.exec(filename)</p>
<p><strong>Example:</strong></p>
<pre>df = data.frame(Symbols=c(&quot;ABAN&quot;,&quot;BPCL&quot;,&quot;IOC&quot;),Price=c(212,579,538))
write.csv(df,&quot;Stocks List.csv&quot;)
shell.exec(&quot;Stocks List.csv&quot;)</pre>
<h4>Quick format of the excel sheet for column width</h4>
<p>We can format the excel sheets for column width using the command lines given below. In the example, the first line will load the excel workbook specified by the file name. In the third &amp; the fourth line, the autoSizeColumn function adjusts the width of the columns, which are specified in the “colIndex”, for each of the worksheets. The last line will save the workbook again after making the necessary formatting changes.</p>
<p><strong>Example:</strong></p>
<pre>wb = loadWorkbook(file_name)
sheets = getSheets(wb)
autoSizeColumn(sheets[[1]], colIndex=1:7)
autoSizeColumn(sheets[[2]], colIndex=1:5)
saveWorkbook(wb,file_name)</pre>
<h3>Functions Demystified</h3>
<h4>choose function</h4>
<p>The choose function computes the combination nCr. The syntax for the function is given as:</p>
<p>choose(n,r)</p>
<p>where,<br>
n is the number of elements<br>
r is the number of subset elements</p>
<p>nCr = n!/(r! * (n-r)!)</p>
<p><strong>Examples:</strong></p>
<pre>choose(5, 2)
</pre>
<p>[1] 10</p>
<pre>choose(2, 1)
</pre>
<p>[1] 2</p>
<h4>sample function</h4>
<p>The sample function randomly selects n items from a given vector. The samples are selected without replacement, which means that the function will not select the same item twice. The syntax for the function is given as:</p>
<p>sample(vector, n)</p>
<p><strong>Example:</strong> Consider a vector consisting of yearly revenue growth data for a stock. We select 5 years revenue growth at random using the sample function.</p>
<pre>Revenue = c(12, 10.5, 11, 9, 10.75, 11.25, 12.1, 10.5, 9.5, 11.45)
sample(Revenue, 5)</pre>
<p>[1] 11.45 12.00 9.50 12.10 10.50</p>
<p>Some statistical processes require sampling with replacement, in such cases you can specify replace= TRUE to the sample function.</p>
<p><strong>Example:</strong></p>
<pre>x = c(1, 3, 5, 7)
sample(x, 7, replace = TRUE)</pre>
<p>[1] 7 1 5 3 7 3 5</p>
<h4>runif and rnorm functions</h4>
<p>The runif function generates a uniform random number between 0 and 1. The argument of runif function is the number of random values to be generated.</p>
<p><strong>Example:</strong></p>
<pre># This will generate 7 uniform random number between 0 and 1.
runif(7)</pre>
<p>[1] 0.6989614 0.5750565 0.6918520 0.3442109 0.5469400 0.7955652 0.5258890</p>
<pre># This will generate 5 uniform random number between 2 and 4.
runif(5, min = 2, max = 4)</pre>
<p>[1] 2.899836 2.418774 2.906082 3.728974 2.720633</p>
<p>The rnorm function generates random numbers from normal distribution. The function rnorm stands for the Normal distribution’s random number generator. The syntax for the function is given as:</p>
<p>rnorm(n, mean, sd)</p>
<p><strong>Example:</strong></p>
<pre># generates 6 numbers from a normal distribution with a mean of 3 and standard deviation of 0.25
rnorm(6, 3, 0.25)</pre>
<p>[1] 3.588193 3.095924 3.240684 3.061176 2.905392 2.891183</p>
<h3><strong>Next Step</strong></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/06/QuantInsti%E2%84%A2_R_Bulletin-X.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-x/" target="_blank">R Weekly Bulletin Vol – X</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-x/"> 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/ymOrk9gIyqs" height="1"></a></div>