<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-weekly-bulletin-vol-iv/"></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-iv/"> 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" alt="" class="wp-image-8098" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/Weekly-R-edition_volume_4.jpg?w=450&ssl=1" data-recalc-dims="1"></p>
<p>This week’s R bulletin will cover topics like removing duplicate rows, finding row number, sorting a data frame in the same order, sorting a data frame in different order, and creating two tabs in an excel workbook. We will also cover functions like Sys.time, julian, and the second &amp; minute function from the lubridate package. Hope you like this R weekly bulletin. Enjoy reading!</p>
<h3>Shortcut Keys</h3>
<p>1. To open a file in R – Ctrl+O<br>
2. To open a new R script – Ctrl+Shift+N<br>
3. To save an R script – Ctrl+S</p>
<h3>Problem Solving Ideas</h3>
<h4>Remove duplicate rows in R</h4>
<p>Consider the following data frame “df” comprising of 3 rows of a 1-minute OHC of a stock.</p>
<p><strong>Example:</strong></p>
<pre>open = c(221, 221.25, 221.25) 
high = c(221.75, 221.45, 221.45) 
close = c(221.35, 221.2, 221.2) 
df = data.frame(open, high, close) 
print(df)</pre>
<p><img sizes="(max-width: 725px) 100vw, 725px" alt="" class="wp-image-8101" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-4_pic1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>As can be seen from the output, the 2nd and the 3rd rows are duplicates. If we want to retain only the unique rows, we can use the duplicated function in the following manner:</p>
<pre>df[!duplicated(df), ]</pre>
<p><img sizes="(max-width: 725px) 100vw, 725px" alt="" class="wp-image-8103" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-4_pic2.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>This will remove the 3rd duplicate row, and retain the first 2 rows. In case we want the duplicate row, we can do so in the following way:</p>
<pre>df[duplicated(df), ]</pre>
<p><img sizes="(max-width: 725px) 100vw, 725px" alt="" class="wp-image-8105" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-4_pic3.png?w=450&ssl=1" data-recalc-dims="1"></p>
<h4>Return row number for a particular value in a column</h4>
<p>To find the row number for a particular value in a column in a vector/data frame we can use the “which” function.</p>
<p><strong>Example 1:</strong></p>
<pre>day = c(&quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thurs&quot;, &quot;Fri&quot;, &quot;Sat&quot;, &quot;Sun&quot;) 
row_number = which(day == &quot;Sat&quot;) 
print(row_number)</pre>
<p>[1] 6</p>
<p><strong>Example 2:</strong> Consider a 2 column data frame “df” comprising of the stock symbols and their respective closing price for the day. To find the row number corresponding to the HCL stock we call the “which” function on the Ticker column with its value selected as HCL.</p>
<pre>Ticker = c(&quot;INFY&quot;, &quot;TCS&quot;, &quot;HCL&quot;) 
ClosePrice = c(2021, 2294, 910) 
data = data.frame(Ticker, ClosePrice) 

row_number = which(data$Ticker == &quot;HCL&quot;) 
print(row_number)</pre>
<p>[1] 3</p>
<h4>Sorting a data frame by two columns in the same order</h4>
<p>To sort a data frame by two columns in the same order, we can use the “order” function and the “with” function. Consider a data frame comprising of stock symbols, their categorization, and the percentage change in price. We first sort the data frame based on category and then sort based on the percentage change in price.</p>
<p>The order function by default sorts in an ascending manner. Hence to sort both the columns in descending order we keep the decreasing argument as TRUE.</p>
<p><strong>Example – Sorting a data frame by two columns</strong></p>
<pre># Create a data frame 
Ticker = c(&quot;INFY&quot;, &quot;TCS&quot;, &quot;HCLTECH&quot;, &quot;SBIN&quot;) 
Category = c(1, 1, 1, 2) 
Percent_Change = c(2.3, -0.25, 0.5, 0.25) 

df = data.frame(Ticker, Category, Percent_Change) 
print(df)</pre>
<p><img sizes="(max-width: 725px) 100vw, 725px" alt="" class="wp-image-8110" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-4_pic4.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Sorting by Category column first and then the Percent_Change column: 
df_sort = df[with(df, order(Category, Percent_Change, decreasing = TRUE)), ] 
print(df_sort)</pre>
<p><img sizes="(max-width: 725px) 100vw, 725px" alt="" class="wp-image-8111" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-4_pic5.png?w=450&ssl=1" data-recalc-dims="1"></p>
<h4>Sorting a data frame by two columns in different order</h4>
<p>To sort a data frame by two columns in different order, we can use the “order” function along with the “with” function.</p>
<p>Consider a data frame comprising of stock symbols, their categorization, and the percentage change in price. Assume that we want to sort first in an ascending order by column “Category”, and then by column “Percent_Change” in a descending order.</p>
<p>The order function by default sorts in an ascending manner. Hence, to sort the “Category” column we mention it as the first variable in the order function without prepending it with any sign. To sort the “Percent_Change” column in a descending order we prepend it with a negative sign.</p>
<p><strong>Example – Sorting a data frame by two columns</strong></p>
<pre># Create a data frame 
Ticker = c(&quot;INFY&quot;, &quot;TCS&quot;, &quot;HCLTECH&quot;, &quot;SBIN&quot;) 
Category = c(1, 1, 1, 2) 
Percent_Change = c(2.3, -0.25, 0.5, 0.25) 

df = data.frame(Ticker, Category, Percent_Change) 
print(df)</pre>
<p><img sizes="(max-width: 724px) 100vw, 724px" alt="" class="wp-image-8113" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-4_pic6.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Sort by Category column first and then the Percent_Change column: 
df_sort = df[with(df, order(Category, -Percent_Change)), ] 
print(df_sort)</pre>
<p><img sizes="(max-width: 724px) 100vw, 724px" alt="" class="wp-image-8114" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-4_pic7.png?w=450&ssl=1" data-recalc-dims="1"></p>
<h4>Creating two tabs in the output excel workbook</h4>
<p>At times we want to write &amp; save the multiple results generated after running our R script in an excel workbook on separate worksheets. To do so, we can make use of the “append” argument in the write.xlsx function.To write to an excel file, one must first install and load the xlsx package in R.</p>
<p><strong>Example:</strong> In this example, we are creating two worksheets in the “Stocks.xlsx” workbook. In the worksheet named, “Top Gainers”, we save the table_1 output, while in the “Top Losers” worksheet we save the table_2 output. To create a second worksheet in the same workbook, we keep the append argument as TRUE in the second line.</p>
<pre>write.xlsx(table_1, &quot;Stocks.xlsx&quot;, sheetName = &quot;Top Gainers&quot;, append = FALSE) 
write.xlsx(table_2, &quot; Stocks.xlsx&quot;, sheetName = &quot;Top Losers&quot;, append = TRUE)</pre>
<h3>Functions Demystified</h3>
<h4>Sys.time function</h4>
<p>The Sys.time function gives the current date and time.</p>
<p><strong>Example:</strong></p>
<pre>date_time = Sys.time() 
print(date_time)</pre>
<p>[1] “2017-04-15 16:25:38 IST”</p>
<p>The function can be used to find the time required to run a code by placing this function at the start and the end of the code. The difference between the start and the end will give the time taken to execute the code.</p>
<h4>julian function</h4>
<p>The julian function is used to extract the Julian date, which is the number of days since January 1, 1970. Given a date, the syntax of the function is given as:</p>
<p>julian(date)</p>
<p><strong>Example:</strong></p>
<pre>date = as.Date(&quot;2010-03-15&quot;) 
julian(date)</pre>
<p>[1] 14683<br>
attr(,”origin”)<br>
[1] “1970-01-01”</p>
<p>Alternatively one can use the as.integer function to get the same result</p>
<h4>second and minute functions</h4>
<p>These functions are part of the lubridate package. The second function retrieves/sets the second component of a date-time object, while the minute function retrieves/sets the minute component. It allows Date-time objects like those belonging to the POSIXct, POSIXlt, Date, zoo, xts, and the timeSeries objects.</p>
<p><strong>Example:</strong></p>
<pre>library(lubridate) 
# Retrieving the seconds We have used the ymd_hms function to parse the given object. 
x = ymd_hms(&quot;2016-06-01 12:23:45&quot;) 
second(x)</pre>
<p>[1] 45</p>
<p><strong>minute function:</strong></p>
<pre>library(lubridate) 
# Retrieving the minute from a date-time object 
x = ymd_hms(&quot;2016-06-01 12:23:45&quot;) 
minute(x)</pre>
<p>[1] 23</p>
<pre># Retrieving the minute from a time object. We have used the hms function to parse the given object.
x = hms(&quot;15:29:06&quot;) 
minute(x)</pre>
<p>[1] 29</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>
<p> </p>
<div><a target="_blank" rel="nofollow" href="https://www.quantinsti.com/wp-content/uploads/2017/04/QuantInsti%E2%84%A2_R_Bulletin-IV.pdf"><i></i>Download the PDF Now!</a></div>
<p> </p>
<p> </p>
<p>The post <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-iv/">R Weekly Bulletin Vol – IV</a> appeared first on <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/"></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-iv/"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/S2zyRKfiL7w"></a></div>