Most common errors in R

When you work with R, you are going to face few errors. Few of the most common error messages are listed here. We will talk about possible scenarios which might have caused that error to appear

could not find function

This error mostly occurs when,

  • You have misspelled function name. e.g., if you want to view your dataset and you put view() with a small v instead of capital V
  • You haven’t loaded the required package

Error in eval

eval is a function which evaluates an R expression in a given environment. Whenever you are trying to call a function it looks for arguments in R environment and if it doesn’t find it flashes the error.

See the following example,

## Error in eval(expr, envir, enclos): object 'b' not found

There is no column named ‘a’ in iris dataset but still we tried to use it in lm() function. So when you see this error, make sure that

  • Object exist
  • Object is named correctly

unexpected ‘else’ in “else”

Let see the following example,

  print("a is an even number")
  print("a is an odd number")
## Error: <text>:4:1: unexpected 'else'
## 3:   print("a is an even number")
## 4: else
##    ^

The cause of the problem is that R doesn’t know whether ‘else’ actually belongs to first if or not. Because the first if() is a complete command in itself. i.e.,

  print("a is an odd number")
## [1] "a is an odd number"

So, to tell R specifically about else being part of if, you should start else in the same line where if block ends. i.e.,

  print("a is an even number") else
  print("a is an odd number")
## [1] "a is an odd number"

See this CRAN document for details –

cannot open

This error occurs only when

  • The object you are referencing doesn’t exist

For possible solutions:

  • Check your working directory
  • Check the path your giving
  • Check the file name

not meaningful for factors

Generally when you try to apply an arithmetic function on factors, you get this error.
This error says that “Arithmetic functions are not allowed on Factor variables”.

For possible solution, try to change the class of your variable

## Error in Summary.factor(structure(1:5, .Label = c("1", "2", "3", "4", : 'sum' not meaningful for factors

Similar error happens when you attempt to get sum of a character variable

## Error in sum(a): invalid 'type' (character) of argument

subscript out of bounds

subscript out of bounds error occurs when you try to access elements out of total available elements. e.g.,

mat <- matrix(0,7,7) 
for(i in 1:7)
  for(j in 1:7)
## Error in `[<-`(`*tmp*`, i, j + 1, value = 10): subscript out of bounds

So, when j reached 7, we tried to assign value 10 to mat[1,8] which doesn’t exist.
To get rid of the error, make sure of limits.

mat <- matrix(0,7,7) 
for(i in 1:7)
  for(j in 1:6)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    0   10   10   10   10   10   10
## [2,]    0   10   10   10   10   10   10
## [3,]    0   10   10   10   10   10   10
## [4,]    0   10   10   10   10   10   10
## [5,]    0   10   10   10   10   10   10
## [6,]    0   10   10   10   10   10   10
## [7,]    0   10   10   10   10   10   10

unexpected numeric constant in

occurs mostly when you have a typo.

c(1,2,34 5,6,7)
## Error: <text>:1:10: unexpected numeric constant
## 1: c(1,2,34 5
##              ^

In the above example, I missed to put a comma between 34 and 5.

unexpected ‘=’ in

Remember, = is an assignment opeartor and not a logical operator. So when you are trying to check for an equality, use “==” and not “=”. e.g.,

  { print("a is 2") }
## Error: <text>:2:5: unexpected '='
## 1: a<-2
## 2: if(a=
##        ^

Error in file(file, “rt”) : cannot open the connection
In addition: Warning message:
In file(file, “rt”) :

cannot open file ‘file.csv’: No such file or directory

When you see this error, check for the file path whether it is correct or not.

invalid ‘type’ (closure) of argument

It means that you have function where you should have data. e.g.,

## Error in sum(mean): invalid 'type' (closure) of argument

object of type ‘closure’ is not subsettable

This error occurs when you try to subset a function, mostly happens because of typos.

## Error in mean[1]: object of type 'closure' is not subsettable
## Error in sum[[1]]: object of type 'builtin' is not subsettable

missing value where TRUE/FALSE needed

Sometimes you have written a block where you are checking for condition getting satisfied or not. But suddenly (mostly unknowingly) an NA value comes and ruins your mood.

for(i in 1:length(a))
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## Error in if (a[i] > 0) print(a[i]): missing value where TRUE/FALSE needed

So, when you face this error – Hunt for the possible NA value.

Error in $<*tmp* replacement has rows, data has

Mostly this error will occur, when you are writing a loop and forgot to add subscripts while assigning values.

Reasons behind the error is that you are trying to assign a vector to another vector of different length.


## Error in `$<`(`*tmp*`, "var1", value = 1:4): replacement has 4 rows, data has 10


The following two tabs change content below.


One thought on “Most common errors in R

Leave a Reply

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