<p><img sizes="(max-width: 1372px) 100vw, 1372px" alt="R Coding" class="wp-image-7666" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/03/Picture2-1.png?w=450&ssl=1" data-recalc-dims="1">This week’s R bulletin will cover functions calls, sorting data frame, creating time series object, and functions like is.na, na.omit, paste, help, rep, and seq function. Hope you like this R weekly bulletin. Enjoy reading!</p>
<h3>Shortcut Keys</h3>
<p>1. To show files – Ctrl+5<br>
2. To show plots – Ctrl+6<br>
3. To show packages – Ctrl+7</p>
<h3><strong>Problem Solving Ideas</strong></h3>
<h4>Calling a function in an R script</h4>
<p>If you want to call a custom-built function in your R script from another script, one can use the “exists” function along with the “source” function. See the example below:</p>
<p><strong>Example:</strong></p>
<pre>if(exists(&quot;daily_price_data&quot;, mode=&quot;function&quot;)) source(&quot;Stock price data.R&quot;)</pre>
<p>
In this case, the expression will check whether a function called “daily_price_data” exists in the “Stock price data.R” script, and if it does, it will load the function in the current script. We can then use the function any number of times in our script by providing the relevant arguments.</p>
<h4>Convert dates from Google finance to a time series object</h4>
<p>When we download stock price data from Google finance, the “DATE” column shows a date in the yyymmdd format. This format is not considered as a time series object in R. To convert the dates from Google Finance into a time series object, one can use the ymd function from the lubridate package. The ymd function accepts dates in the form year, month, day. In the case of dates in other formats, the lubridate package has functions like ydm, mdy, myd, dmy, and dym, which can be used to convert it into a time series object.</p>
<p><strong>Example:</strong></p>
<pre>library(lubridate)
dt = ymd(20160523)
print(dt)</pre>
<p>
[1] “2016-05-23”</p>
<h4>Sorting a data frame in an ascending or descending order</h4>
<p>The arrange function from the dplyr package can be used to sort a data frame. The first argument is the data.frame and the next argument is the variable to sort by, either in an ascending or in a descending order.</p>
<p>In the example below, we create a two column data frame comprising of stock symbols and their respective percentage price change. We then sort the Percent change column first in an ascending order, and in the second instance in a descending order.</p>
<p><strong>Example:</strong></p>
<pre>library(dplyr)
# Create a dataframe
Ticker = c(&quot;UNITECH&quot;, &quot;RCOM&quot;, &quot;VEDL&quot;, &quot;CANBK&quot;)
Percent_Change = c(2.3, -0.25, 0.5, 1.24)
df = data.frame(Ticker, Percent_Change)
print(df)</pre>
<p>
Ticker          Percent_Change<br>
1  UNITECH    2.30<br>
2      RCOM   -0.25<br>
3        VEDL    0.50<br>
4     CANBK    1.24</p>
<pre># Sort in an ascending order
df_descending = arrange(df, Percent_Change)
print(df_descending)</pre>
<p>
Ticker     Percent_Change<br>
1     RCOM    -0.25<br>
2       VEDL    0.50<br>
3    CANBK    1.24<br>
4 UNITECH    2.30</p>
<pre># Sort in a descending order
df_descending = arrange(df, desc(Percent_Change))
print(df_descending)</pre>
<p>
Ticker         Percent_Change<br>
1 UNITECH   2.30<br>
2    CANBK   1.24<br>
3       VEDL   0.50<br>
4     RCOM   -0.25</p>
<h3>Functions Demystified</h3>
<h4>paste function</h4>
<p>The paste is a very useful function in R and is used to concatenate (join) the arguments supplied to it. To include or remove the space between the arguments use the “sep” argument.</p>
<p><strong>Example 1:</strong> Combining a string of words and a function using paste</p>
<pre>x = c(20:45)
paste(&quot;Mean of x is&quot;, mean(x), sep = &quot; &quot;)</pre>
<p>
[1] “Mean of x is 32.5”</p>
<p><strong>Example 2:</strong> Creating a filename using the dirPath, symbol, and the file extension name as the arguments to the paste function.</p>
<pre>dirPath = &quot;C:/Users/MyFolder/&quot;
symbol = &quot;INFY&quot;
filename = paste(dirPath, symbol, &quot;.csv&quot;, sep = &quot;&quot;)
print(filename)</pre>
<p>
[1] “C:/Users/MyFolder/INFY.csv”</p>
<h4>is.na and na.omit function</h4>
<p>The is.na functions checks whether there are any NA values in the given data set, whereas, the na.omit function will remove all the NA values from the given data set.</p>
<p><strong>Example:</strong> Consider a data frame comprising of open and close prices for a stock corresponding to each date.</p>
<pre>date = c(20160501, 20160502, 20160503, 20160504)
open = c(234, NA, 236.85, 237.45)
close = c(236, 237, NA, 238)
df = data.frame(date, open, close)
print(df)</pre>
<p>
date           open        close<br>
1  20160501  234.00     236<br>
2  20160502        NA     237<br>
3  20160503  236.85     NA<br>
4  20160504  237.45     238</p>
<p>Let us check whether the data frame has any NA values using the is.na function.</p>
<pre>is.na(df)</pre>
<p>
date      open      close<br>
[1,]  FALSE  FALSE   FALSE<br>
[2,]  FALSE  TRUE     FALSE<br>
[3,]  FALSE  FALSE   TRUE<br>
[4,]  FALSE  FALSE   FALSE</p>
<p>As you can see from the result, it has two NA values. Let us now use the na.omit function, and view the results.</p>
<pre>na.omit(df)</pre>
<p>
date           open         close<br>
1  20160501  234.00      236<br>
4  20160504  237.45      238</p>
<p>As can be seen from the result, the rows having NA values got omitted, and the resultant data frame now comprises of non-NA values only.</p>
<p>These functions can be used to check for any NA values in large data sets on which we wish to apply some computations. The presence of NA values can cause the computations to give unwanted results, and hence such NA values need to be either removed or replaced by relevant values.</p>
<h4>rep and seq function</h4>
<p>The rep function repeats the arguments for the specified number of times, while the sequence function is used to form a required sequence of numbers. Note that in the sequence function we use a comma and not a colon.</p>
<p><strong>Example 1:</strong></p>
<pre>rep(&quot;Strategy&quot;, times = 3)</pre>
<p>
[1] “Strategy” “Strategy” “Strategy”</p>
<pre>rep(1:3, 2)</pre>
<p>
[1] 1 2 3 1 2 3</p>
<p><strong>Example 2:</strong></p>
<pre>seq(1, 5)</pre>
<p>
[1] 1 2 3 4 5</p>
<pre>seq(1, 5, 0.5)</pre>
<p>
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0</p>
<h4>help and example function</h4>
<p>The help function provides information on the topic sort, while the example function provides examples on the given topic.</p>
<p>help(sum)<br>
example(sum)</p>
<p>To access the R help files associated with specific functions within a particular package, include the function name as the first argument to the help function along with the package name mentioned in the second argument.</p>
<p><strong>Example:</strong></p>
<pre>help(barplot, package=&quot;graphics&quot;)</pre>
<p>
Alternatively one can also type a question mark followed by the function name (e.g. ?barplot) and execute the command to know more about the function.</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>The post <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-ii/">R Weekly Bulletin Vol – II</a> appeared first on <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/"></a>.</p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/p9Xm1FFEzCQ">