0

In R, I am trying to only read the first four columns of a text file. Is there a way to do this? This is some example code:

example <- read.table("example.txt", header=TRUE)

When I run this, I get the following error message:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 1 did not have 9 elements

This is why I only want to read in the first four columns.

  • Can you post your first few lines of data? You can pad the missing columns, but it usually means there is a problem with the data formatting. – beroe Oct 16 '13 at 14:22
  • You may have a look at the `colClasses` argument in `?read.table`. "`("NULL" (when the column is skipped))`" – Henrik Oct 16 '13 at 14:23
  • Check [this duplicate](http://stackoverflow.com/questions/18161009/error-in-reading-in-data-set-in-r) for the `fill` parameter, but I recommend finding the source of your inconsistency or your data may be thrown off. – beroe Oct 16 '13 at 14:28
  • Also, use `read.csv` instead of `read.table` (That is my final thought on the matter...) – beroe Oct 18 '13 at 18:36

0 Answers0