R Errors Explained: incomplete final line found by readtableheader

Error messages are the dread of every programmer. Even when you know how to fix a particular problem it is still annoying when they show up. Here, we have an error message that results from a problem in loading a file. This is where most of your errors are likely to occur because new files can introduce unexpected issues into a program.

The circumstances of this error.

This error message can occur when using the read.table() function. It is particularly frequent when also using the file.choose() function to load in a data file.

> X = read.table(file.csv,header=TRUE,sep=”,”)

> X = read.table(file.choose(),header=TRUE,sep=”,”)

The problem is not with your code but with the file you are loading. The result is that you could load several files without any problem and then suddenly this error message shows up. It a particularly annoying error message because it is not intuitive. The irony is it the cause of this error message turned out to be a ridiculously simple flaw in the formatting of the file.

What is causing this error?

The key to understanding the cause of this error message is “incomplete final line.” It indicates that the problem is at the end of the file you are trying to load and that it does not end as expected. The read.table() function is looking for an end of line character. When the function reads the file and comes to the end without this character, it thinks there is something wrong with the file and produces the error message. Fortunately, this problem can be solved both in the file and in your code.

How to fix this error.

There are two ways of fixing this problem. The first is by fixing the file you want to load. The second is a small adjustment to the code that prevents the problem from occurring.

Fixing the file you are trying to load.

The first solution is to edit the file you are working with. This solution works well if you only using one file. In that case, you would probably have its location as a part of your code.

  • Open the file you wish to use in a text editor.
  • Scroll to the bottom of the file.
  • Put the cursor at the end of the last line.
  • Hit Enter.
  • Save your file.

While this is a viable solution when you are dealing with a single file, it is impractical when you are using the file.choose() function so that you can use many files.

Fixing the problem from within your code.

The second solution involves adjusting your code to avoid the problem. This is done by inserting the readLines() into the code as shown below.

> X = read.table(readLines(file.csv, warn = FALSE),header=TRUE,sep=”,”)

> X = read.table(readLines(file.choose(), warn = FALSE),header=TRUE,sep=”,”)

This is the solution that speaks of true programming and not just a stopgap measure. It is the one you can proudly tell other programmers about.

Scroll to top
Privacy Policy