Do you ever need to read a text file while not knowing what’s the maximum number of columns it can have? Well, I do 🙂 Turns out read.table function only looks at first five columns to determine the number of columns as ?read.table help page says:
The number of data columns is determined by looking at the first five lines of input (or the whole input 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 iffill
orblank.lines.skip
are true, so specifycol.names
if necessary (as in the ‘Examples’).
So what’s the workaround? Find out the maximum in advance!
 no_col <- max(count.fields(file, sep = "\t"))  dataI <- read.table(file,sep="\t",fill=TRUE,header = F,col.names=c("chr", "start", "end", "length",1:no_col))
Since we want to fill the blanks, we will use fill=TRUE function and we may also decide to name our columns numerically as col.names does here. Thus, the columns will be named chr, start, end, length and 1, 2, 3 … maxColumn.
Awesome example! Thank you so much for this tutorial! https://stantyan.com