<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-i/"></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/"> 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: 869px) 100vw, 869px" alt="" class="wp-image-7554" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/03/Weekly-R-Edition.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>We are starting with R weekly bulletins which will contain some interesting ways and methods to write codes in R and solve bugging problems. We will also cover R functions and shortcut keys for beginners. We understand that there can be more than one way of writing a code in R, and the solutions listed in the bulletins may not be the sole reference point for you. Nevertheless, we believe that the solutions listed will be helpful to many of our readers. Hope you like our R weekly bulletins. Enjoy reading them!</p>
<h3><strong>Shortcut Keys</strong></h3>
<ol>
<li>To move cursor to R Source Editor – Ctrl+1</li>
<li>To move cursor to R Console – Ctrl+2</li>
<li>To clear the R console – Ctrl+L</li>
</ol>
<h3><strong>Problem Solving Ideas</strong></h3>
<h4><strong>Creating user input functionality </strong></h4>
<p>To create the user input functionality in R, we can make use of the readline function. This gives us the flexibility to set the input values for variables for our choice in the code is run.</p>
<p>Example: Suppose that we have coded a backtesting strategy. We want to have the flexibility to choose the backtest period. To do so, we can create a user input “n”, signifying the backtest period in years, and add the line shown below at the start of the code.</p>
<p>When the code is run, it will prompt the user to enter the value for “n”. Upon entering the value, the R code will get executed for the set period and produce the desired output.</p>
<pre>n = readline(prompt = &quot;Enter the backtest period in years: &quot;)</pre>
<p><h4><strong> Refresh a code in every x seconds</strong></h4>
<p>To refresh a code in every x seconds we can use the while loop and the Sys.sleep function. The “while loop” keeps executing the enclosed block of commands until the condition remains satisfied. We enclose the code in the while statement and keep the condition as TRUE. By keeping the condition as TRUE, it will keep looping. At the end of the code, we add the Sys.sleep function and specify the delay time in seconds. This way the code will get refreshed after every “x” seconds.</p>
<p><strong>Example:</strong> In this example, we initialize the x value to zero. The code is refreshed every 1 second, and it will keep printing the value of x. One can hit the escape button on the keyboard to terminate the code.</p>
<pre>x = 0 
while (TRUE) { 
x = x + 1 print(x) 
Sys.sleep(1) 
}</pre>
<p><h4><strong>Running multiple R scripts sequentially</strong></h4>
<p>To run multiple R scripts, one can have the main script which will contain the names of the scripts to be run. Running the main script will lead to the execution of the other R scripts. Assume the name of the main script is “NSE Stocks.R”. In this script, we will mention the names of the scripts we wish to run within the source function. In this example, we wish to run the “Top gainers.R” and the “Top losers.R” script. These will be the part of the “NSE Stocks.R” as shown below and we run the main script to run these 2 scripts.</p>
<pre>source(&quot;Top gainers.R&quot;)
source(&quot;Top losers.R&quot;)</pre>
<p>
Enclosing the R script name within the “source” function causes R to accept its input from the named file. Input is read and parsed from that file until the end of the file is reached, then the parsed expressions are evaluated sequentially in the chosen environment. Alternatively, one can also place the R script names in a vector, and use the sapply function.</p>
<p><strong>Example:</strong></p>
<pre>filenames = c(&quot;Top gainers.R&quot;, &quot;Top losers.R&quot;) 
sapply(filenames, source)</pre>
<p><h4><strong>Converting a date in the American format to Standard date format </strong></h4>
<p>The American date format is of the type mm/dd/yyyy, whereas the ISO 8601 standard format is yyyy-mm-dd. To convert a date from American format to the Standard date format we will use the as.Date function along with the format function. The example below illustrates the method.</p>
<p><strong>Example:</strong></p>
<pre># date in American format dt = &quot;07/24/2016&quot; # If we call the as.Date function on the date, it will  
# throw up an error, as the default format assumed by the as.Date function is yyyy-mmm-dd.
as.Date(dt)</pre>
<p>
Error in charToDate(x): character string is not in a standard unambiguous format</p>
<pre># Correct way of formatting the date 
as.Date(dt, format = &quot;%m/%d/%Y&quot;)</pre>
<p>
[1] “2016-07-24”</p>
<h4><strong>How to remove all the existing files from a folder</strong></h4>
<p>To remove all the files from a particular folder, one can use the unlink function. Specify the path of the folder as the argument to the function. A forward slash with an asterisk is added at the end of the path. The syntax is given below.</p>
<p><strong>unlink(“path/*”)</strong></p>
<p><strong>Example:</strong></p>
<pre>unlink(&quot;C:/Users/Documents/NSE Stocks/*&quot;)</pre>
<p>
This will remove all the files present in the “NSE Stocks” folder.</p>
<h3><strong>Functions Demystified</strong></h3>
<h4><strong>write.csv function</strong></h4>
<p>If you want to save a data frame or matrix in a csv file, R provides for the write.csv function. The syntax for the write.csv function is given as:</p>
<p><strong>write.csv(x, file=”filename”, row.names=FALSE)</strong></p>
<p>If we specify row.names=TRUE, the function prepends each row with a label taken from the row.names attribute of your data. If your data doesn’t have row names then the function just uses the row numbers. Column header line is written by default. If you do not want the column headers, set col.names=FALSE.</p>
<p><strong>Example:</strong></p>
<pre># Create a data frame 
Ticker = c(&quot;PNB&quot;,&quot;CANBK&quot;,&quot;KTKBANK&quot;,&quot;IOB&quot;) 
Percent_Change = c(2.30,-0.25,0.50,1.24) 
df = data.frame(Ticker,Percent_Change) 
write.csv(df, file=&quot;Banking Stocks.csv&quot;, row.names=FALSE)</pre>
<p>
This will write the data contained in the “df” dataframe to the “Banking Stocks.csv” file. The file gets saved in the R working directory.</p>
<h4><strong>fix function</strong></h4>
<p>The fix function shows the underlying code of a function provided as an argument to it.</p>
<p><strong>Example:</strong></p>
<pre>fix(sd)</pre>
<p>
The underlying code for the standard deviation function is as shown below. This is displayed when we executed the fix function with “sd” as the argument.</p>
<pre>function (x, na.rm = FALSE) 
sqrt(var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm))</pre>
<p><h4><strong> download.file function</strong></h4>
<p>The download.file function helps download a file from a website. This could be a webpage, a csv file, an R file, etc. The syntax for the function is given as:</p>
<p><strong>download.file(</strong>url<strong>, </strong>destfile<strong>)</strong></p>
<p>where,<br>
url – the Uniform Resource Locator (URL) of the file to be downloaded<br>
destfile – the location to save the downloaded file, i.e. path with a file name</p>
<p><strong>Example:</strong> In this example, the function will download the file from the path given in the “url” argument, and saved it in the D drive within the “Skills” folder with the name “betawacc.xls”.</p>
<pre>url = &quot;http://www.exinfm.com/excel%20files/betawacc.xls&quot;
destfile = &quot;D:/Skills/wacc.xls&quot;
download.file(url, destfile)</pre>
<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><span></span></p>
<div><a target="_blank" rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol/"><i></i>Download the PDF Now!</a></div>
<p>The post <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol/">R Weekly Bulletin Vol – I</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/"> 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/yG50r5ZiW8w"></a></div>