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

R

R Problem Overview


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?

R Solutions


Solution 1 - R

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 - R

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2806363View Question on Stackoverflow
Solution 1 - RjlhowardView Answer on Stackoverflow
Solution 2 - RAri B. FriedmanView Answer on Stackoverflow