<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-ix/"></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-ix/"> 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-8821" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/Weekly-R-Bulletin_Vol-9.jpg?w=450&ssl=1" data-recalc-dims="1"></p>
<p>This week’s R bulletin will cover topics on how to list files, extracting file names, and creating a folder using R. </p>
<div></div>
<p>	<a 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-ix/" target="_blank" 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-ix/"><span><span>We will also cover functions like the select function, filter function, and the arrange 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. Run the current chunk – Ctrl+Alt+C<br>
2. Run the next chunk – Ctrl+Alt+N<br>
3. Run the current function definition – Ctrl+Alt+F</p>
<h3>Problem Solving Ideas</h3>
<h4>How to list files with a particular extension</h4>
<p>To list files with a particular extension, one can use the pattern argument in the list.files function. For example to list csv files use the following syntax.</p>
<p><strong>Example: </strong></p>
<pre>files = list.files(pattern = &quot;\\.csv$&quot;)</pre>
<p>This will list all the csv files present in the current working directory. To list files in any other folder, you need to provide the folder path.</p>
<pre> list.files(path = &quot;C:/Users/MyFolder&quot;, pattern = &quot;\\.csv$&quot;)</pre>
<p>$ at the end means that this is end of the string. Adding \. ensures that you match only files with extension .csv</p>
<h4>Extracting file name using gsub function</h4>
<p>When we download stock data from google finance, the file’s name corresponds to the stock data symbol. If we want to extract the stock data symbol from the file name, we can do it using the gsub function. The function searches for a match to the pattern argument and replaces all the matches with the replacement value given in the replacement argument. The syntax for the function is given as:</p>
<pre> gsub(pattern, replacement, x)</pre>
<p>where,</p>
<p>pattern – is a character string containing a regular expression to be matched in the given character vector.<br>
replacement – a replacement for matched pattern.<br>
x – is a character vector where matches are sought.</p>
<p>In the example given below, we extract the file name for files stored in the “Reading MFs” folder. We have downloaded the stock price data in R working directory for two companies namely, MRF and PAGEIND Ltd.</p>
<p><strong>Example:</strong></p>
<pre>folderpath = paste(getwd(), &quot;/Reading MFs&quot;, sep = &quot;&quot;)
temp = list.files(folderpath, pattern = &quot;*.csv&quot;)
print(temp)</pre>
<p>[1] “MRF.csv”  “PAGEIND.csv”</p>
<pre>gsub(&quot;*.csv$&quot;, &quot;&quot;, temp)</pre>
<p>[1] “MRF”   “PAGEIND”</p>
<h4>Create a folder using R</h4>
<p>One can create a folder via R with the help of the “dir.create” function. The function creates a folder with the name as specified in the last element of the path. Trailing path separators are discarded.</p>
<p>The syntax is given as:</p>
<pre>dir.create(path, showWarnings = FALSE, recursive = FALSE)</pre>
<p><strong>Example: </strong></p>
<pre>dir.create(&quot;D:/RCodes&quot;, showWarnings = FALSE, recursive = FALSE)</pre>
<p>This will create a folder called “RCodes” in the D drive.</p>
<h3>Functions Demystified</h3>
<h4>select function</h4>
<p>The select function comes from the dplyr package and can be used to select certain columns of a data frame which you need. Consider the data frame “df” given in the example.</p>
<p><strong>Example:</strong></p>
<pre>library(dplyr)
Ticker = c(&quot;INFY&quot;, &quot;TCS&quot;, &quot;HCL&quot;, &quot;TECHM&quot;)
OpenPrice = c(2012, 2300, 900, 520)
ClosePrice = c(2021, 2294, 910, 524)
df = data.frame(Ticker, OpenPrice, ClosePrice)
print(df)</pre>
<p><img sizes="(max-width: 571px) 100vw, 571px" alt="" class="wp-image-8810" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/IX-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Suppose we wanted to select the first 2 columns only. We can use the names of the columns in the 
# second argument to select them from the main data frame.

subset_df = select(df, Ticker:OpenPrice)
print(subset_df)</pre>
<p><img sizes="(max-width: 579px) 100vw, 579px" alt="" class="wp-image-8812" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-2.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Suppose we want to omit the OpenPrice column using the select function. We can do so by using
# the negative sign along with the column name as the second argument to the function.

subset_df = select(df, -OpenPrice)
print(subset_df)</pre>
<p><img sizes="(max-width: 579px) 100vw, 579px" alt="" class="wp-image-8813" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-3.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># We can also use the 'starts_with' and the 'ends_with' arguments for selecting columns from the
# data frame. The example below will select all the columns which end with the word 'Price'.

subset_df = select(df, ends_with(&quot;Price&quot;))
print(subset_df)</pre>
<p><img sizes="(max-width: 580px) 100vw, 580px" alt="" class="wp-image-8814" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-4.png?w=450&ssl=1" data-recalc-dims="1"></p>
<h4>filter function</h4>
<p>The filter function comes from the dplyr package and is used to extract subsets of rows from a data frame. This function is similar to the subset function in R.</p>
<p><strong>Example: </strong></p>
<pre>library(dplyr)
Ticker = c(&quot;INFY&quot;, &quot;TCS&quot;, &quot;HCL&quot;, &quot;TECHM&quot;)
OpenPrice = c(2012, 2300, 900, 520)
ClosePrice = c(2021, 2294, 910, 524)
df = data.frame(Ticker, OpenPrice, ClosePrice)
print(df)</pre>
<p><img sizes="(max-width: 579px) 100vw, 579px" alt="" class="wp-image-8815" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-5.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Suppose we want to select stocks with closing prices above 750, we can do so using the filter 
# function in the following manner:

subset_df = filter(df, ClosePrice > 750)
print(subset_df)</pre>
<p><img sizes="(max-width: 579px) 100vw, 579px" alt="" class="wp-image-8816" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-6.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># One can also use a combination of conditions as the second argument in filtering a data set.

subset_df = filter(df, ClosePrice > 750 &amp; OpenPrice &lt; 2000)
print(subset_df)</pre>
<p><img sizes="(max-width: 579px) 100vw, 579px" alt="" class="wp-image-8817" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-7.png?w=450&ssl=1" data-recalc-dims="1"></p>
<h4>arrange function</h4>
<p>The arrange function is part of the dplyr package, and is used to reorder rows of a data frame according to one of the columns. Columns can be arranged in descending order or ascending order by using the special desc() operator.</p>
<p><strong> Example:</strong></p>
<pre>library(dplyr)
Ticker = c(&quot;INFY&quot;, &quot;TCS&quot;, &quot;HCL&quot;, &quot;TECHM&quot;)
OpenPrice = c(2012, 2300, 900, 520)
ClosePrice = c(2021, 2294, 910, 524)
df = data.frame(Ticker, OpenPrice, ClosePrice)
print(df)</pre>
<p><img sizes="(max-width: 580px) 100vw, 580px" alt="" class="wp-image-8818" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-8.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Arrange in descending order

subset_df = arrange(df, desc(OpenPrice))
print(subset_df)</pre>
<p><img sizes="(max-width: 581px) 100vw, 581px" alt="" class="wp-image-8819" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-9.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre># Arrange in ascending order.

subset_df = arrange(df, -desc(OpenPrice))
print(subset_df)</pre>
<p><img sizes="(max-width: 581px) 100vw, 581px" alt="" class="wp-image-8820" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/ix-10.png?w=450&ssl=1" data-recalc-dims="1"></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 target="_blank" rel="nofollow" href="https://www.quantinsti.com/wp-content/uploads/2017/05/QuantInsti%E2%84%A2_R_Bulletin-IX.pdf">Download the PDF Now!</a></div>
<p>The post <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-ix/">R Weekly Bulletin Vol – IX</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-ix/"> 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/qcaerZNDU6I"></a></div>