Best practices while writing R code Exercises

November 24, 2016

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

How can I write R codes that other people can understand and use?

Hand pressing Best Practice button on interface with blue background.

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.

Answers to the exercises are available here.

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.

Exercise 1
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.

NumVector = seq(1,10,by=2)

Exercise 2
The command below installs “car” package. Make changes in the command below so that all the packages get installed on which “car” is dependent.


Exercise 3
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.

y <- 0
x <- 0

if (y == 0)
} else {
y ^ x

Exercise 4
Update the below code so that it is easy for other users to read it.

NumVector <- seq(1,10,by=2)

if(length(NumVector) > 10 && debug)
message(“Length of the numeric vector is greater than 10”)

Exercise 5
Correct the indentation in the below function so that it is easy for you and other users to read and understand.


if (test==1) {
print(“Hello World!”)
print(“The value of test is 1 here”)
} else{
print(“The value of test is not 1 here”);

Exercise 6
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.

install.packages("dplyr",dependencies = T)

Exercise 7
Change the below code so that the it doesn’t print package related information while loading the plyr package.


Exercise 8
Make the changes in the below code so that it doesn’t print warnings while calculating the correlation value between two vectors.

a <- c(1,1)
b <- c(2,3)

Exercise 9
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’.

rename(head(mtcars), c(mpg = "NewName"))

Exercise 10
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.

a <- 1e-02

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)