How to Limit the Number of Columns Read in R
Practise yous e'er need to read a text file while not knowing what's the maximum number of columns it can take? Well, I exercise 🙂 Turns out read.tabular array function only looks at get-go five columns to determine the number of columns every bit ?read.table assist page says:
The number of data columns is adamant past looking at the get-go five lines of input (or the whole input if information technology has less than v lines), or from the length of
col.names
if information technology is specified and is longer. This could feasibly be incorrect iffill
orblank.lines.skip
are truthful, then specifycol.names
if necessary (equally in the 'Examples').
And so what's the workaround? Detect out the maximum in advance!
no_col <- max(count.fields(file, sep = "\t")) dataI <- read.table(file,sep="\t",fill up=TRUE,header = F,col.names=c("chr", "start", "end", "length",1:no_col))
Since nosotros want to fill the blanks, we volition use make full=TRUE function and we may as well decide to name our columns numerically as col.names does here. Thus, the columns volition be named chr, start, terminate, length and ane, 2, 3 … maxColumn.
abadwhorknotho1940.blogspot.com
Source: https://sites.psu.edu/biomonika/2017/09/08/reading-text-files-with-variable-number-of-columns-in-r/
0 Response to "How to Limit the Number of Columns Read in R"
Postar um comentário