Solved

How to speed up reading data from a txt file in R?

Posted on 2014-11-30
13
270 Views
Last Modified: 2014-12-10
Hi,

I have a very large file (800 MB) to be read in R from a txt files and it takes several minutes to do so. I need it to be much quicker.

 Is there any functions, packages, or tricks to read in data much quicker than what I am doing?

I have included my code and the dataset.
I use a HP laptop with intel i7 and windows8.

Thank you for you help.


### time the first way
start.time.data1<-Sys.time()

data1<-read.table("XYZ_EE.txt",sep=",",stringsAsFactor=F,header=F)

time.data1<-Sys.time()-start.time.data1
time.data1  ## Time difference of 1.523134 secs


### time the second way
start.time.data2<-Sys.time()

data2<-read.table("XYZ_EE.txt",sep=",",stringsAsFactor=F,header=F,
                  colClasses=c("character","character","numeric","numeric"))

time.data2<-Sys.time()-start.time.data2
time.data2  ## Time difference of 1.497055 secs

Open in new window

XYZ-EE.txt
0
Comment
Question by:pgmerLA
13 Comments
 
LVL 41

Assisted Solution

by:pcelba
pcelba earned 110 total points
ID: 40473446
Notepad reads your file 8 secs independently on the drive type used. And Notepad is very bad at this field... MS Excel is faster. The R speed is derived by the R itself.

It seems the only way is parallelism in your task...

I suppose you don't need all detail data in one program instance. Simply split your file into several parts and execute these parts separately. This will be limited by your disk speed so I would recommend to use SSD drive.

If you need to process all data at once (and no way to speed the R up exists) then you should look for faster processing in e.g. C++. Or you may use C++ to preprocess/reduce your data.

For more advanced R solution you may look here: http://cran.r-project.org/web/views/HighPerformanceComputing.html
http://dirk.eddelbuettel.com/papers/useR2009hpcTutorial.pdf
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 25 total points
ID: 40473472
Do you have 800MB+ free RAM (physical memory) when you begin reading?

The reason I used the plus sign is that R will need additional memory to do statistics.
0
 
LVL 16

Assisted Solution

by:dhsindy
dhsindy earned 65 total points
ID: 40473625
Are you using a 32 or 64 bit system?  I am assuming a 64-bit since i7 processor.  Disk speed and memory comes to mind.  Verify the file is loaded all to the memory and not virtually to the disk.  Do you have ample free space (30%) on your hard disk.

Also, you didn't mention where the file is stored, hard disk, cloud, etc.  Other things to look for  are other processes that might be taking priority - like a security program, or other - I use Malwarebytes and I noticed it periodically kicks in and uses up to 30% of my RAM. I frequently stop my security program when loading large files and then restart it after the file is loaded.

I would start "Task Manager"/or other process manager and watch the memory processes as it loads, maybe increase the priority of that process.  And, look for other processes hogging resources.
0
 
LVL 34

Accepted Solution

by:
Gary Patterson earned 300 total points
ID: 40473679
How about fread() instead of read.table()?

http://davetang.org/muse/2013/09/03/handling-big-data-in-r/

Here's a nice comparison of some basic techniques to load big data into memory:

http://yanran.li/2013/10/loading-big-data-in-R/
0
 

Author Comment

by:pgmerLA
ID: 40492825
Thank you all for your answers.

I have used GaryPatterson advice on fread() and I was able to reduce the time considerably

> system.time(data1<-fread("XYZ_EE.txt",sep=",",header=F))
   user  system elapsed 
   0.59    0.00    0.61 
> system.time(data2<-read.table("XYZ_EE.txt",sep=",",header=F))
   user  system elapsed 
   1.64    0.03    1.67 

Open in new window

0
 

Author Comment

by:pgmerLA
ID: 40492829
The system I use:

i7 4700MQ @2.4 Ghz
16 GB RAM DDR3
64bit Windows8
1TB hard drive
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 41

Expert Comment

by:pcelba
ID: 40492834
At least some speeding is good.
0
 

Author Comment

by:pgmerLA
ID: 40492843
Hi pcelba,

How can I follow your advice in R?  "I would recommend to use SSD drive"
0
 
LVL 41

Expert Comment

by:pcelba
ID: 40492852
It was just a brainstorming... or hardware solution.

Any HDD swap for SSD drive will speed your computer up. I am using notebooks with SSD C: drive and it is much faster than any older desktop.

The notebook uses just i7-3610QM but its experience index is 7.
0
 

Author Comment

by:pgmerLA
ID: 40492873
Are you using an external SSD C:drive?

How much faster do you think my program will get?

What do you mean by "The notebook uses just i7-3610QM but its experience index is 7"?
0
 
LVL 41

Expert Comment

by:pcelba
ID: 40492883
My SSD is internal. External would need USB 3 connection and it also does not speed the OS up.

Sorry I cannot predict the speed improvement.

 i7-3610QM is slower than 4700MQ: http://ark.intel.com/compare/75117,64899
but thanks to SSD the notebook speed index is 7.
0
 

Author Comment

by:pgmerLA
ID: 40492910
Thank you pcelba. I will keep that really good advise in mind!
0
 
LVL 41

Expert Comment

by:pcelba
ID: 40492920
You are welcome.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

We are taking giant steps in technological advances in the field of wireless telephony. At just 10 years since the advent of smartphones, it is crucial to examine the benefits and disadvantages that have been report to us.
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now