user5054
user5054

Reputation: 1096

R - How to count number of columns in each row of a file before reading the file

I have a big file containing about 100K rows. Each row is supposed to have 17 columns, but since some of the columns are missing for some of the rows, those rows seem to have columns less than 17. I want to know those rows beforehand and not read them. How is this possible in R? Thanks.

Upvotes: 1

Views: 410

Answers (1)

MrFlick
MrFlick

Reputation: 206187

The count.fields() function does exactly this. It takes most of the same parameters that read.table() would use. See the ?count.fields help page for more info.

fc <- count.fields("myfile.txt")
which(fc != 17)

Upvotes: 6

Related Questions