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

Kjetil Brinchmann Halvorsen kjetil at acelerate.com
Mon Dec 6 21:18:05 CET 2004


Liaw, Andy wrote:

>>From: Marc Schwartz
>>
>>On Mon, 2004-12-06 at 22:12 +0800, 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?
>>>thanks in advance.
>>>Regards
>>>      
>>>
>>See ?readLines
>>
>>You can use:
>>
>>length(readLines("FileName"))
>>
>>to get the number of lines read.
>>
>>HTH,
>>
>>Marc Schwartz
>>    
>>
>
>
>On a system equipped with `wc' (*nix or Windows with such utilities
>installed and on PATH) I would use that.  Otherwise length(count.fields())
>might be a good choice.
>
>Cheers,
>Andy
>
>  
>
For instance on a windows machine (XP) with the development tools installed:

 > list.files()
 [1] "afm"             "AUTHORS"         "bin"             "CHANGES"       
 [5] "CHANGES1"        "COPYING"         "COPYING.LIB"     "COPYRIGHTS"    
 [9] "doc"             "etc"             "FAQ"             "include"       
[13] "lib"             "library"         "MD5"             "modules"       
[17] "NEWS"            "ONEWS"           "README"          "README.packages"
[21] "README.Rterm"    "README.rw2001"   "RESOURCES"       "rw-FAQ"        
[25] "share"           "src"             "Tcl"             "THANKS"        
[29] "unins000.dat"    "unins000.exe"    "Y2K"           
 > shell("wc --help")
Usage: wc [OPTION]... [FILE]...
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

Report bugs to <bug-textutils at gnu.org>.
 > shell("wc --lines THANKS")
     71 THANKS
 >

Kjetil

-- 

Kjetil Halvorsen.

Peace is the most effective weapon of mass construction.
               --  Mahdi Elmandjra




More information about the R-help mailing list