Dr. Mark Gardener


Providing training for:

  • Ecology
  • Data analysis
  • Statistics
  • R The statistical programming language
  • Data management
  • Data mining

Tips and Tricks - for R and Excel

On these pages you can find tips, tricks and hints for using both R and Excel. At the end of each tip there are links forwards and backwards as appropriate. There is also an index of R tips and an index of Excel tips.

For most analytical purposes the combination of Excel and R is unbeatable! Excel is great as a data management tool and for preparing data for analysis. You can also use it to get an overview of your data or to make simple (and not so simple) graphs. R is an analytical "swiss army knife" and can carry out a mind-boggling array of analytical routines as well as producing great graphics.

Tips & Tricks for R | Tips & Tricks for Excel | An Introduction to R | MonogRaphs | Writer's Bloc

"Incomplete final line" error when importing CSV files. Edit file in Text editor and add extra line return.


Incomplete final line error on CSV import

Sometimes you can get an error message when trying to import a CSV file, most likely with the read.csv() or read.table() command. The "incomplete final line" error message arises when there is a "missing" return in the last data row of your CSV file. You need to add an extra line-feed at the end of the file.

There are two ways you can set about this:

  • Open the file in a text editor and add an extra line. Then import the file to R.
  • Send an additional line-feed directly to the file from R itself. Then import the file to R.

Whichever method you choose it is a good idea to make a backup of the file, just in case. If you open your "faulty" file in a spreadsheet it will look absolutely fine; it is only in a text editor or word processor that you can see the end-of-line characters (not all editors display hidden characters).

Use a text editor to add a LF to the last line of a CSV file.


Edit file in text editor

The OS-based method of fixing this problem is to open the file in any kind of text editor. WordPad is a good choice in Windows as it can handle line-breaks more effectively (Windows likes end-of-line characters to be CR & LF, Mac uses LF and Linux CR, WordPad displays all correctly, Word does not). Go to the bottom of the file and if you cannot get the cursor past the end of the final line there is a missing linefeed. Simply press the Enter key on the keyboard to add the final linefeed and save the file.

Now when you try to import the file you should not get the error.

Use cat() to send a newline character to a file.


Send extra linefeed from R

You can send a linefeed to a file directly from R using the cat() command. Use a command like so:

cat("\n", file = file.choose(), append = TRUE)

The "\n" is a "newline" character (you do need the quotes). Note that you need to use append = TRUE otherwise you will overwrite the file with nothing except your newline character. If you are using Linux then you'll need to specify the filename explicilty but in Windows or Mac the file.choose() part will allow you to choose the file.

Top << Previous tip: Ordering items in boxplot() >> Next tip: Use locator() to place labels interactively
Follow me...
Facebook Twitter Google+ Linkedin Amazon
Top Tips & Tricks Home Index of R Tips Index of Excel Tips  
More links:

An introduction to R

See my Publications about Excel, R, statistics and data analysis Courses in R, data analysis, data management and statistics Visit the R Project website

See my Publications about statistics and data analysis.

MonogRaphs: random topics in R

Writer's Bloc – my latest writing project includes R scripts

Courses in data analysis, data management and statistics.

Top Home
Data Analysis
Contact GardenersOwn Homepage