Reading text files using read.table Reading text files using read.table r r

Reading text files using read.table


From ?read.table: The number of data columns is determined by looking at the first five lines of input (or the whole file if it has less than five lines), or from the length of col.names if it is specified and is longer. This could conceivably be wrong if fill or blank.lines.skip are true, so specify col.names if necessary.

So, perhaps your data file isn't clean. Being more specific will help the data import:

d = read.table("foobar.txt",                sep="\t",                col.names=c("id", "name"),                fill=FALSE,                strip.white=TRUE)

will specify exact columns and fill=FALSE will force a two column data frame.