<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-xi/"></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-xi/"> 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-XI-1024x536.png?w=450&ssl=1" alt="" class="wp-image-9005" data-recalc-dims="1"></p>
<p>This week’s R bulletin will cover topics on how to round to the nearest desired number, converting and comparing dates and how to remove last x characters from an element. </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-xi/" 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-xi/" target="_blank"><span><span>We will also cover functions like rank, mutate, transmute, and set.seed. </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. Comment/uncomment current line/selection – Ctrl+Shift+C<br>
2. Move Lines Up/Down – Alt+Up/Down<br>
3. Delete Line – Ctrl+D</p>
<h3>Problem Solving Ideas</h3>
<h4>Rounding to the nearest desired number</h4>
<p>Consider a case where you want to round a given number to the nearest 25. This can be done in the following manner:</p>
<pre>round(145/25) * 25</pre>
<p>[1] 150</p>
<pre>floor(145/25) * 25</pre>
<p>[1] 125</p>
<pre>ceiling(145/25) * 25</pre>
<p>[1] 150</p>
<p>Usage:<br>
Assume if you are calculating a stop loss or take profit for an NSE stock in which the minimum tick is 5 paisa. In such case, we will divide and multiply by 0.05 to achieve the desired outcome.</p>
<p><strong>Example:</strong></p>
<pre>Price = 566
Stop_loss = 1/100

# without rounding
SL = Price * Stop_loss
print(SL)</pre>
<p>[1] 5.66</p>
<pre># with rounding to the nearest 0.05
SL1 = floor((Price * Stop_loss)/0.05) * 0.05
print(SL1)</pre>
<p>[1] 5.65</p>
<h4>How to remove last n characters from every element</h4>
<p>To remove the last n characters we will use the substr function along with the nchr function. The example below illustrates the way to do it.</p>
<p><strong>Example:</strong></p>
<pre># In this case, we just want to retain the ticker name which is &quot;TECHM&quot;
symbol = &quot;TECHM.EQ-NSE&quot;
s = substr(symbol,1,nchar(symbol)-7)
print(s)</pre>
<p>[1] “TECHM”</p>
<h4>Converting and Comparing dates in different formats</h4>
<p>When we pull stock data from Google finance the date appears as “YYYYMMDD”, which is not recognized as a date-time object. To convert it into a date-time object we can use the “ymd” function from the lubridate package.</p>
<p><strong>Example:</strong></p>
<pre>library(lubridate)
x = ymd(20160724)
print(x)</pre>
<p>[1] “2016-07-24”</p>
<p>Another data provider gives stock data which has the date-time object in the American format (mm/dd/yyyy). When we read the file, the date-time column is read as a character. We need to convert this into a date-time object. We can convert it using the as.Date function and by specifying the format.</p>
<pre>dt = &quot;07/24/2016&quot;
y = as.Date(dt, format = &quot;%m/%d/%Y&quot;)
print(y)</pre>
<p>[1] “2016-07-24”</p>
<pre># Comparing the two date-time objects (from Google Finance and the data provider) after conversion
identical(x, y)</pre>
<p>[1] TRUE</p>
<h3>Functions Demystified</h3>
<p><strong>rank function</strong></p>
<p>The rank function returns the sample ranks of the values in a vector. Ties (i.e., equal values) and<br>
missing values can be handled in several ways.</p>
<p><strong>rank(x, </strong>na<strong>.last = TRUE, ties.method = c(“average”, “first”, “random”, “max”, “min”))</strong></p>
<p>where,<br>
<strong>x:</strong> numeric, complex, character or logical vector<br>
na<strong>.last:</strong> for controlling the treatment of NAs. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed; if “keep” they are kept with rank NA<br>
<strong>ties.method:</strong> a character string specifying how ties are treated</p>
<p><strong>Examples:</strong></p>
<pre>x &lt;- c(3, 5, 1, -4, NA, Inf, 90, 43)
rank(x)</pre>
<p>[1] 3 4 2 1 8 7 6 5</p>
<pre>rank(x, na.last = FALSE)</pre>
<p>[1] 4 5 3 2 1 8 7 6</p>
<h4>mutate and transmute functions</h4>
<p>The mutate and transmute functions are part of the dplyr package. The mutate function computes new variables using the existing variables of a given data frame. The new variables are added to the existing data frame. On the other hand, the transmute function creates these new variables as a separate data frame.</p>
<p>Consider the data frame “df” given in the example below. Suppose we have 5 observations of 1-minute price data for a stock, and we want to create a new variable by subtracting the mean from the 1-minute closing prices. It can be done in the following manner using the mutate function.</p>
<p><strong>Example:</strong></p>
<pre>library(dplyr)
OpenPrice = c(520, 521.35, 521.45, 522.1, 522)
ClosePrice = c(521, 521.1, 522, 522.25, 522.4)
Volume = c(2000, 3500, 1750, 2050, 1300)
df = data.frame(OpenPrice, ClosePrice, Volume)
print(df)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/R-XI-1.png?w=450&ssl=1" alt="" class="wp-image-9008" data-recalc-dims="1"></p>
<pre>df_new = mutate(df, cpmean_diff = ClosePrice - mean(ClosePrice, na.rm = TRUE))
print(df_new)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/R-XI-2.png?w=450&ssl=1" alt="" class="wp-image-9009" data-recalc-dims="1"></p>
<pre># If we want the new variable as a separate data frame, we can use the transmute function instead.
df_new = transmute(df, cpmean_diff = ClosePrice - mean(ClosePrice, na.rm = TRUE))
print(df_new)</pre>
<p><img sizes="(max-width: 666px) 100vw, 666px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/R-XI-3.png?w=450&ssl=1" alt="" class="wp-image-9010" data-recalc-dims="1"></p>
<h4>set.seed function</h4>
<p>The set.seed function helps generate the same sequence of random numbers every time the program runs. It sets the random number generator to a known state. The function takes a single argument which is an integer. One needs to use the same positive integer in order to get the same initial state.</p>
<p><strong>Example:</strong></p>
<pre># Initialize the random number generator to a known state and generate five random numbers
set.seed(100)
runif(5)</pre>
<p>[1] 0.30776611 0.25767250 0.55232243 0.05638315 0.46854928</p>
<pre># Reinitialize to the same known state and generate the same five 'random' numbers
set.seed(100)
runif(5)</pre>
<p>[1] 0.30776611 0.25767250 0.55232243 0.05638315 0.46854928</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-XI.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-xi/" target="_blank">R Weekly Bulletin Vol – XI</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-xi/"> 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/zL58BCy5ma0" height="1"></a></div>