Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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?

like image 344
user2806363 Avatar asked Jan 19 '14 20:01

user2806363


2 Answers

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.

like image 73
jlhoward Avatar answered Oct 10 '22 10:10

jlhoward


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.

like image 26
Ari B. Friedman Avatar answered Oct 10 '22 12:10

Ari B. Friedman