Read lines by number from a large file

后端 未结 5 676
有刺的猬
有刺的猬 2020-12-13 02:44

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 l

5条回答
  •  抹茶落季
    2020-12-13 03:50

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

    con<-file('filename')
    open(con)
    
    read.table(con,skip=5,nrow=1) #6-th line
    read.table(con,skip=20,nrow=1) #27-th line
    ...
    close(con)
    

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

提交回复
热议问题