How to read first 1000 lines of .csv file into R? [closed]

I have very big .csv file, it's around a few GB.
I want to read first few thousand lines of it. Is there any method to do this efficiently?


Solution 1:

Use the nrows argument in read.csv(...)

df <- read.csv(file="my.large.file.csv",nrows=2000)

There is also a skip= parameter that tells read.csv(...) how many lines to skip before you start reading.

If your file is that large you might be better off using fread(...) in the data.table package. Same arguments.

Solution 2:

If you're on UNIX or OS/X, you can use the command line:

head -n 1000 myfile.csv > myfile.head.csv

Then just read it in R like normal.