<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-xii/"></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-xii/"> 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://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/R-weekly-XII-1024x536.png?w=450&ssl=1" alt="" class="wp-image-9147" data-recalc-dims="1"></p>
<p>This week’s R bulletin will cover topics on how to resolve some common errors in R. </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-xii/" 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-xii/" target="_blank"><span><span>We will also cover functions like do.call, rename, and lapply. </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. Find and Replace – Ctrl+F<br>
2. Find Next – F3<br>
3. Find Previous – Shift+F3</p>
<h3>Problem Solving Ideas</h3>
<h4>Resolving the ‘: cannot open the connection’ Error</h4>
<p>There can be two reasons for this error to show up when we run an R script: 1) A file/connection can’t be opened because R can’t find it (mostly due to an error in the path) 2) Failure in .onLoad() because a package can’t find a system dependency</p>
<p><strong>Example:</strong></p>
<pre>symbol = &quot;AXISBANK&quot;
noDays = 1
dirPath = paste(getwd(), &quot;/&quot;, noDays, &quot; Year Historical Data&quot;, sep = &quot;&quot;)
fileName = paste(dirPath, symbol, &quot;.csv&quot;, sep = &quot;&quot;)
data = as.data.frame(read.csv(fileName))</pre>
<p>Warning in file(file, “rt”): cannot open file ‘C:/Users/Madhukar/Documents/<br>
1 Year Historical DataAXISBANK.csv’: No such file or directory<br>
Error in file(file, “rt”): cannot open the connection</p>
<p>We are getting this error because we have specified the wrong path to the “dirPath” object in the code. The right path is shown below. We missed adding a forward slash after “Year Historical Data” in the paste function. This led to the wrong path, and hence the error.</p>
<p>dirPath = paste(getwd(),”/”,noDays,” Year Historical Data/”,sep=””)</p>
<p>After adding the forward slash, we re-ran the code. Below we can see the right dirPath and fileName printed in the R console.</p>
<p><strong>Example:</strong></p>
<pre>symbol = &quot;AXISBANK&quot;
noDays = 1
dirPath = paste(getwd(), &quot;/&quot;, noDays, &quot; Year Historical Data/&quot;, sep = &quot;&quot;)
fileName = paste(dirPath, symbol, &quot;.csv&quot;, sep = &quot;&quot;)
data = as.data.frame(read.csv(fileName))
print(head(data, 3))</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/RXII-3.png?w=450&ssl=1" alt="" class="wp-image-9153" data-recalc-dims="1"></p>
<h4>Resolving the ‘could not find function’ Error</h4>
<p>This error arises when an R package is not loaded properly or due to the misspelling of the function names.</p>
<p>When we run the code shown below, we get a “could not find the function ymd” error in the console. This is because we have misspelled the “ymd” function as “ymed”. If we do not load the required packages, this will also throw up a “could not find function ymd” error.</p>
<p><strong>Example:</strong></p>
<pre># Read NIFTY price data from the csv file
df = read.csv(&quot;NIFTY.csv&quot;)

# Format date
dates = ymed(df$DATE)</pre>
<p>Error in eval(expr, envir, enclos): could not find function “ymed”</p>
<h4>Resolving the “replacement has” Error</h4>
<p>This error occurs when one tries to assign a vector of values to an existing object and the lengths do not match up.</p>
<p>In the example below, the stock price data of Axis bank has 245 rows. In the code, we created a sequence “s” of numbers from 1 to 150. When we try to add this sequence to the Axis Bank data set, it throws up a “replacement error” as the lengths of the two do not match. Thus to resolve such errors one should ensure that the lengths match.</p>
<p><strong>Example:</strong></p>
<pre>symbol = &quot;AXISBANK&quot; ; noDays = 1 ;
dirPath = paste(getwd(),&quot;/&quot;,noDays,&quot; Year Historical Data/&quot;,sep=&quot;&quot;)
fileName = paste(dirPath,symbol,&quot;.csv&quot;,sep=&quot;&quot;)
df = as.data.frame(read.csv(fileName))

# Number of rows in the dataframe &quot;df&quot;
n = nrow(df); print(n);

# create a sequence of numbers from 1 to 150
s = seq(1,150,1)

# Add a new column &quot;X&quot; to the existing data frame &quot;df&quot;
df$X = s
print(head(df,3))</pre>
<p>Error in $&lt;-.data.frame(*tmp*, “X”, value = c(1, 2, 3, 4, 5, 6, 7, : replacement has 150 rows, data has 245</p>
<h3>Functions Demystified</h3>
<h4>do.call function</h4>
<p>The do.call function is used for calling other functions. The function which is to be called is provided as the first argument to the do.call function, while the second argument of the do.call function is a list of arguments of the function to be called. The syntax for the function is given as:</p>
<p>do.call (function_name, arguments)</p>
<p><strong>Example:</strong> Let us first define a simple function that we will call later in the do.call function.</p>
<pre>numbers = function(x, y) {
sqrt(x^3 + y^3)
}

# Now let us call this 'numbers' function using the do.call function. We provide the function name as # the first argument to the do.call function, and a list of the arguments as the second argument.

do.call(numbers, list(x = 3, y = 2))</pre>
<p>[1] 5.91608</p>
<h4>rename function</h4>
<p>The rename function is part of the dplyr package, and is used to rename the columns of a data frame. The syntax for the rename function is to have the new name on the left-hand side of the = sign, and the old name on the right-hand side. Consider the data frame “df” given in the example below.</p>
<p><strong>Example:</strong></p>
<pre>library(dplyr)
Tic = c(&quot;IOC&quot;, &quot;BPCL&quot;, &quot;HINDPETRO&quot;, &quot;ABAN&quot;)
OP = c(555, 570, 1242, 210)
CP = c(558, 579, 1248, 213)
df = data.frame(Tic, OP, CP)
print(df)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/RXII-1.png?w=450&ssl=1" alt="" class="wp-image-9151" data-recalc-dims="1"></p>
<pre># Renaming the columns as 'Ticker', 'OpenPrice', and 'ClosePrice'. This can be done in the following 
# manner:

renamed_df = rename(df, Ticker = Tic, OpenPrice = OP, ClosePrice = CP)
print(renamed_df)</pre>
<p><img sizes="(max-width: 665px) 100vw, 665px" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/06/RXII-2.png?w=450&ssl=1" alt="" class="wp-image-9152" data-recalc-dims="1"></p>
<h4>lapply function</h4>
<p>The lapply function is part of the R base package, and it takes a list “x” as an input, and returns a list of the same length as “x”, each element of which is the result of applying a function to the corresponding element of X. The syntax of the function is given as:</p>
<p>lapply(x, Fun)<br>
where,<br>
x is a vector (atomic or list)<br>
Fun is the function to be applied</p>
<p><strong>Example 1:</strong></p>
<p>Let us create a list with 2 elements, OpenPrice and the ClosePrice. We will compute the mean of the values in each element using the lapply function.</p>
<pre>x = list(OpenPrice = c(520, 521.35, 521.45), ClosePrice = c(521, 521.1, 522))
lapply(x, mean)</pre>
<p>$OpenPrice<br>
[1] 520.9333</p>
<p>$ClosePrice<br>
[1] 521.3667</p>
<p><strong>Example 2:</strong></p>
<pre>x = list(a = 1:10, b = 11:15, c = 1:50)
lapply(x, FUN = length)</pre>
<p>$a<br>
[1] 10</p>
<p>$b<br>
[1] 5</p>
<p>$c<br>
[1] 50</p>
<h3>Next Step</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-XII.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-xii/" target="_blank">R Weekly Bulletin Vol – XII</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-xii/"> 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/cndMcgtYtaw" height="1"></a></div>