[R] how to get how many lines there are in a file.

Thomas Lumley tlumley at u.washington.edu
Mon Dec 6 18:14:27 CET 2004


On Mon, 6 Dec 2004, Uwe Ligges wrote:

> Hu Chen wrote:
>
>> hi all
>> If I wanna get the total number of lines in a big file without reading
>> the file's content into R as matrix or data frame, any methods or
>> functions?
>
> You must read it in R, or how do you think should one determine the number of 
> lines in a file (if you don't want to use another program)?
> I'd suggest length(readLines(...)).
>

If the file is large enough that you don't want to read the whole thing at 
once you can read it in chunks using readLines(). If all the lines are the 
same length you can find the size of the file and divide by the length of 
a line.

Also, you don't say what OS you are on.  If it isn't Windows the easiest 
thing would be to use wc.

 	-thomas




More information about the R-help mailing list