[R] Efficiently reading random lines form a large file

Juan Pablo Lewinger lewinger at usc.edu
Wed May 16 01:02:02 CEST 2007


I need to read two different random lines at a time from a large 
ASCII file (120 x 296976) containing space delimited 0-1 entries.

The following code does the job and it's reasonable fast for my needs:

   lineNumber = sample(120, 2)
   line1 = scan(filename, what = "integer", skip=lineNumber[1]-1, nlines=1)
   line2 = scan(filename, what = "integer", skip=lineNumber[2]-1, nlines=1)

 > system.time(for (i in 50){
+   lineNumber = sample(120, 2)
+   line1 = scan(filename, what = "integer", skip=lineNumber[1]-1, nlines=1)
+   line2 = scan(filename, what = "integer", skip=lineNumber[2]-1, nlines=1)
+ })

Read 296976 items
Read 296976 items
[1] 14.24  0.12 14.51    NA    NA

However, I'm wondering if there's an even faster way to do this. Is there?

 > sessionInfo()
R version 2.4.1 (2006-12-18)
i386-pc-mingw32

Juan Pablo Lewinger
Department of Preventive Medicine
Keck School of Medicine
University of Southern California
1540 Alcazar Street, CHP-220
Los Angeles, CA 90089-9011, USA



More information about the R-help mailing list