Aleksandr Levchuk Aleksandr Levchuk - 15 days ago 5x
R Question

Read lines by number from a large file

I have a file with 15 million lines (will not fit in memory). I also have a small vector of line numbers - the lines that I want to extract.

How can I read-out the lines in one pass?

I was hoping for a C function that does it on one pass.

mbq mbq

The trick is to use connection AND open it before read.table:


read.table(con,skip=5,nrow=1) #6-th line
read.table(con,skip=20,nrow=1) #27-th line

You may also try scan, it is faster and gives more control.