Common R Programming Errors Faced by Beginners

Share on Facebook0Tweet about this on Twitter0Share on LinkedIn0Share on Google+0

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 Facebook0Tweet about this on Twitter0Share on LinkedIn0Share on Google+0

Leave a Reply

Your email address will not be published. Required fields are marked *