Common R Programming Errors Faced by Beginners

[This article was first published on R programming, 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.

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+

Common R Programming Errors Faced by Beginners

There are two ways to write error-free programs; only the third one works. – Alan Perlis

Error messages can be intimidating for novice coders, and sometimes even for the experienced ones. Trying to decipher the error can be a time-consuming task. In this issue we look at some of the common errors that you can encounter while working in R.

‘could not find function’ Error

This error arises when an R package is not loaded properly or due to misspelling of the functions.

As you can see in the screenshot below, when we run the code, we get a could not find function “ymd” error in the console. This is because we have not loaded the package “lubridate” to which the ymd function belongs.

We need to include the line – library(lubridate) at the start of  the code to run it error-free.

If we misspell the ymd() function, this will also throw up a could not find function “ymd” error.

could not find function Error

‘Error in if’

“Error in if” generally means the logical statement in “if (xxx) { …” is not yielding a logical value. Most of these have missing value where TRUE/FALSE is needed, meaning that the variable in xxx has NA in it.

As you can see in the screenshot below, we have assigned NA to the variable “c”. When we use “c” in the logical statement, (c > 12) this also gives NA, and as a result the if() expression cannot be executed.

Error in if

‘object not found’ error

This error occurs when the particular object used in the code is empty.

In the example below we are trying to compute the market capitalization of Tata Motors Limited. As you can see, we get an ‘Object not found’ error as the “price” object is missing in the code. We have only entered the number of shares outstanding in the code, and not the price.

object not found error

‘: cannot open the connection’ Error

There can be two reasons for this error to show up when running 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

cannot open the connection Error

We are getting this error because we have specified the wrong path to the “dirPath” object in the code. The right path is shown in the screenshot below. We missed adding a forward slash after getwd() in the paste function. This led to the wrong path, and hence the error.

Reading the downloaded stock price data

After adding the forward slash, we re-ran the code. Now in screenshot below we can see the right dirPath and fileName printed in the R console.

Reading the downloaded stock price data 2

“subscript out of bounds” Error

This error is likely to occur when one is using nested loops incorrectly in the code.

subscript out of bounds Error


In the above example, the “letters.mat” matrix has 5 rows. Since the first loop size is the same as that of the number of rows in the “letters.mat” matrix, the code runs successfully. However, if we increase the first loop size beyond 5, we get “subscript out of bonds” error.subscript out of bounds Error 2

This is because when we increase the loop size to 10, we have not increased the size of underlying object (letters.mat) in kind.

‘non-numeric argument to a binary operator’ Error

This is a simple error to decipher. Since today_close is a character, the price_change computation results in the error.

non-numeric argument to a binary operator Error

“replacement has” Error

This error occurs when one tries to assign a vector of values to a subset of an existing object and the lengths do not match up.

In the example below, the stock price data of Axis bank has 246 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.

replacement has Error

Next Step

If you’re new at R programming, you may like to take a look at a few basic articles like, designing a quant trading strategy in R or example of trading strategy coded in R. You can also learn how to start with quantmod package in R. Once you have learned the basics, you can test your skills at our interactive R course.

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+

The post Common R Programming Errors Faced by Beginners appeared first on .

To leave a comment for the author, please follow the link and comment on their blog: R programming. 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.

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)