<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/best-practices-while-writing-r-code-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/best-practices-while-writing-r-code-exercises/" data-counturl="https://www.r-bloggers.com/best-practices-while-writing-r-code-exercises/" data-count="horizontal" data-text="Best practices while writing R code Exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/11/24/best-practices-while-writing-r/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>How can I write R codes that other people can understand and use?</p>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/11/best-practices.jpg?x61433"><img sizes="(max-width: 300px) 100vw, 300px" alt="Hand pressing Best Practice button on interface with blue background." class="wp-image-2869" src="https://i1.wp.com/r-exercises.com/wp-content/uploads/2016/11/best-practices-300x213.jpg?resize=300%2C213" data-recalc-dims="1"></a></p>
<p>In the exercises below we cover some of the best practices while writing a small piece of R code or a full automated script. These are some of the practices which should be kept in mind while coding, trust me it will make your life a lot easier.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2016/11/24/best-practices-solutions/">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p><strong>Exercise 1</strong><br>
We want to create a numeric vector. The values of this vector should be between 1 and 10, starting from 1 with a difference of 2. Below is the code to generate a numeric vector.Make the suitable changes so that it follows standard practice for assignments.<br>
<code><br>
NumVector = seq(1,10,by=2) </code></p>
<p><strong>Exercise 2</strong><br>
The command below installs “car” package. Make changes in the command below so that all the packages get installed on which “car” is dependent.<br>
<code><br>
install.packages(&quot;car&quot;) </code></p>
<p><strong>Exercise 3</strong><br>
Make the changes in the below code so that it is easy for other users to read and it follows the standard practice while writing an if/else statement in R.<br>
<code><br>
y &lt;- 0<br>
x &lt;- 0</code></p>
<p>if (y == 0)<br>
{<br>
log(x)<br>
} else {<br>
y ^ x<br>
}</p>
<p><strong>Exercise 4</strong><br>
Update the below code so that it is easy for other users to read it.<br>
<code><br>
NumVector &lt;- seq(1,10,by=2)</code></p>
<p>if(length(NumVector) > 10 &amp;&amp; debug)<br>
message(“Length of the numeric vector is greater than 10”)</p>
<p><strong>Exercise 5</strong><br>
Correct the indentation in the below function so that it is easy for you and other users to read and understand.<br>
<code><br>
test&lt;-1</code></p>
<p>if (test==1) {<br>
print(“Hello World!”)<br>
print(“The value of test is 1 here”)<br>
} else{<br>
print(“The value of test is not 1 here”);<br>
}<br>
print(test*test+1);</p>
<p><strong>Exercise 6</strong><br>
Update the below code such that it first checks if the “dplyr” package is present. if it is already present, don’t install it just load the package.If the package is not present, install it and then load it.</p>
<p><code> install.packages(&quot;dplyr&quot;,dependencies = T) </code></p>
<p><strong>Exercise 7</strong><br>
Change the below code so that the it doesn’t print package related information while loading the plyr package.</p>
<p><code> library(plyr) </code></p>
<p><strong>Exercise 8</strong><br>
Make the changes in the below code so that it doesn’t print warnings while calculating the correlation value between two vectors.<br>
<code><br>
a &lt;- c(1,1)<br>
b &lt;- c(2,3)<br>
cor(a,b)<br>
</code></p>
<p><strong>Exercise 9</strong><br>
Update the below command so that it calls the ‘rename’ function from ‘plyr’ package. The same function is present in both the packages- ‘plyr’ and ‘rename’.</p>
<p><code> rename(head(mtcars), c(mpg = &quot;NewName&quot;)) </code></p>
<p><strong>Exercise 10</strong><br>
Create a scalar vector ‘a’ with a value of 10e-02 (1/100). Below code prints the same vector in scientific format. Make changes to print in a numeric format.<br>
<code><br>
a &lt;- 1e-02<br>
print(a)<br>
</code><span></span></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/best-practices-while-writing-r-code-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/best-practices-while-writing-r-code-exercises/" data-counturl="https://www.r-bloggers.com/best-practices-while-writing-r-code-exercises/" data-count="vertical" data-text="Best practices while writing R code Exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/11/24/best-practices-while-writing-r/"> R-exercises</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/YiMbCf9_Z_8"></a></div>