Aleksandr Levchuk Aleksandr Levchuk - 1 year ago 90
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
Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download