{
  "id": 8298,
  "title": "Trouble loading 'transactions.csv' into R",
  "url": "/competitions/acquire-valued-shoppers-challenge/discussion/8298",
  "author_name": "",
  "post_date": "2014-05-28T07:39:32.230Z",
  "votes": null,
  "comment_count": 5,
  "views": 2573,
  "content": "<p>Anyone know a way to load 'transactions.csv' into R on a 32-bit 3GB RAM machine?</p>\n<p>Is it even possible? If not, would a 64-bit 4GB or a 64-bit 8GB RAM machines be able to handle it?</p>\n\n<p>thanks</p>",
  "messages": [
    {
      "id": "47165",
      "postDate": "05/28/2014 07:39:32",
      "content": "<p>Anyone know a way to load 'transactions.csv' into R on a 32-bit 3GB RAM machine?</p>\n<p>Is it even possible? If not, would a 64-bit 4GB or a 64-bit 8GB RAM machines be able to handle it?</p>\n\n<p>thanks</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "47177",
      "postDate": "05/28/2014 12:19:13",
      "content": "<p>Am facing the same issue . I'm running 32bit OS and using RODBC to connect to transaction file.</p>\n<p>However, I'm able to work on only 200,000 Rows at a time . I get memory limitation error if i use more than that.</p>\n<p>I think we just can't beat the benchmark without utilizing the transaction file.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "47218",
      "postDate": "05/29/2014 03:30:50",
      "content": "<p>Check this thread:&nbsp;https://www.kaggle.com/c/acquire-valued-shoppers-challenge/forums/t/8258/fread-function-in-r</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "47300",
      "postDate": "05/30/2014 02:04:16",
      "content": "<p>On your question about 64 bit machine - the unpacked file is about 20Gb. One of my machines is a 64bit running Win7Pro64 bit with 16Gb of RAM. R runs out of RAM loading the transactions.csv directly. You have to use other methods, some of them were mentioned on this forum already.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "48546",
      "postDate": "06/03/2014 03:30:32",
      "content": "<p>You cannot load a 20g file in 4 or 8g memory. You could put the file in a database or read the files chunk by chunk and filter the no so needed transactions. However, the methods mentioned in this forum take some possibly useful temporal transactions out.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "48577",
      "postDate": "06/03/2014 22:17:19",
      "content": "<p>You can load, filter and save the big transaction file in Chunks similar to the python code posted on the other thread with very small foot print. (I did it on a 4G Win 32bit testing&nbsp;machine, with R footprint less than 100M memory, I think. But whether you can do any meaningful modeling, is another story.) However, R is not good at these tasks, R code can be significantly slower than python for doing this kind of things.</p>\n<p>Here is my sample code to load and filter and save the transaction files from and into gzip'ed files:</p>\n<p>options(stringsAsFactors=F)<br>offers=read.csv(&quot;Data/offers.csv.gz&quot;)<br>allcats=unique(offers$category)</p>\n<p>trainHistory=read.csv(&quot;Data/trainHistory.csv.gz&quot;)<br>train_ids=unique(trainHistory$id)<br>testHistory=read.csv(&quot;Data/testHistory.csv.gz&quot;)<br>test_ids=unique(testHistory$id)<br>all_ids=unique(c(train_ids, test_ids))</p>\n<p>gzf = gzfile(&quot;Data/reduced_df_quicksave.csv.gz&quot;,&quot;w&quot;)<br>con=file(&quot;Data/transactions.csv.gz&quot;,open='r')<br>blocksize=500000<br>rownum=blocksize<br>readnum=0<br>totalrows=0<br>keptrows=0<br>begTime=Sys.time()<br>headers=read.csv(con, nrows=1, header=F)<br>while(rownum &gt;= blocksize) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tempdf=read.csv(con, nrows=blocksize, header=F)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rownum=nrow(tempdf)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totalrows=totalrows+rownum<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; colnames(tempdf)=headers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tempdf=tempdf[tempdf$category %in% allcats &amp; tempdf$id %in% all_ids,]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (nrow(tempdf) &gt; 0) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (keptrows == 0) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write.table(tempdf, row.names=F, col.names=TRUE, sep=&quot;,&quot;, file=gzf)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write.table(tempdf, row.names=F, col.names=FALSE, sep=&quot;,&quot;, file=gzf)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keptrows=keptrows+nrow(tempdf)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rm(tempdf);gc()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; readnum=readnum+1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cat(paste(&quot;Read&quot;,readnum,&quot;blocks, total rows read:&quot;,format(totalrows,scientific=F),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;, total rows kept:&quot;, format(keptrows,scientific=F),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;, time used:&quot;,format(Sys.time()-begTime),&quot;\\n&quot;))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flush.console()<br>}<br>close(con)</p>\n<p>close(gzf)<br>cat(paste(&quot;&nbsp;&nbsp; time used:&quot;,format(Sys.time()-begTime),&quot;\\n&quot;))</p>\n<p>It took 40+ minutes on my windows machine, with the file located on a local hard drive.</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 47177,
      "author_name": "namesj",
      "author_url": "",
      "post_date": "05/28/2014 12:19:13",
      "content": "<p>Am facing the same issue . I'm running 32bit OS and using RODBC to connect to transaction file.</p>\n<p>However, I'm able to work on only 200,000 Rows at a time . I get memory limitation error if i use more than that.</p>\n<p>I think we just can't beat the benchmark without utilizing the transaction file.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 47218,
      "author_name": "omoekan09",
      "author_url": "",
      "post_date": "05/29/2014 03:30:50",
      "content": "<p>Check this thread:&nbsp;https://www.kaggle.com/c/acquire-valued-shoppers-challenge/forums/t/8258/fread-function-in-r</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 47300,
      "author_name": "suboperator",
      "author_url": "",
      "post_date": "05/30/2014 02:04:16",
      "content": "<p>On your question about 64 bit machine - the unpacked file is about 20Gb. One of my machines is a 64bit running Win7Pro64 bit with 16Gb of RAM. R runs out of RAM loading the transactions.csv directly. You have to use other methods, some of them were mentioned on this forum already.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 48546,
      "author_name": "fenec8",
      "author_url": "",
      "post_date": "06/03/2014 03:30:32",
      "content": "<p>You cannot load a 20g file in 4 or 8g memory. You could put the file in a database or read the files chunk by chunk and filter the no so needed transactions. However, the methods mentioned in this forum take some possibly useful temporal transactions out.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 48577,
      "author_name": "weiwunyc",
      "author_url": "",
      "post_date": "06/03/2014 22:17:19",
      "content": "<p>You can load, filter and save the big transaction file in Chunks similar to the python code posted on the other thread with very small foot print. (I did it on a 4G Win 32bit testing&nbsp;machine, with R footprint less than 100M memory, I think. But whether you can do any meaningful modeling, is another story.) However, R is not good at these tasks, R code can be significantly slower than python for doing this kind of things.</p>\n<p>Here is my sample code to load and filter and save the transaction files from and into gzip'ed files:</p>\n<p>options(stringsAsFactors=F)<br>offers=read.csv(&quot;Data/offers.csv.gz&quot;)<br>allcats=unique(offers$category)</p>\n<p>trainHistory=read.csv(&quot;Data/trainHistory.csv.gz&quot;)<br>train_ids=unique(trainHistory$id)<br>testHistory=read.csv(&quot;Data/testHistory.csv.gz&quot;)<br>test_ids=unique(testHistory$id)<br>all_ids=unique(c(train_ids, test_ids))</p>\n<p>gzf = gzfile(&quot;Data/reduced_df_quicksave.csv.gz&quot;,&quot;w&quot;)<br>con=file(&quot;Data/transactions.csv.gz&quot;,open='r')<br>blocksize=500000<br>rownum=blocksize<br>readnum=0<br>totalrows=0<br>keptrows=0<br>begTime=Sys.time()<br>headers=read.csv(con, nrows=1, header=F)<br>while(rownum &gt;= blocksize) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tempdf=read.csv(con, nrows=blocksize, header=F)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rownum=nrow(tempdf)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totalrows=totalrows+rownum<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; colnames(tempdf)=headers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tempdf=tempdf[tempdf$category %in% allcats &amp; tempdf$id %in% all_ids,]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (nrow(tempdf) &gt; 0) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (keptrows == 0) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write.table(tempdf, row.names=F, col.names=TRUE, sep=&quot;,&quot;, file=gzf)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write.table(tempdf, row.names=F, col.names=FALSE, sep=&quot;,&quot;, file=gzf)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keptrows=keptrows+nrow(tempdf)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rm(tempdf);gc()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; readnum=readnum+1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cat(paste(&quot;Read&quot;,readnum,&quot;blocks, total rows read:&quot;,format(totalrows,scientific=F),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;, total rows kept:&quot;, format(keptrows,scientific=F),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;, time used:&quot;,format(Sys.time()-begTime),&quot;\\n&quot;))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flush.console()<br>}<br>close(con)</p>\n<p>close(gzf)<br>cat(paste(&quot;&nbsp;&nbsp; time used:&quot;,format(Sys.time()-begTime),&quot;\\n&quot;))</p>\n<p>It took 40+ minutes on my windows machine, with the file located on a local hard drive.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "47165": "",
    "47177": "",
    "47218": "",
    "47300": "",
    "48546": "",
    "48577": ""
  },
  "source": "meta"
}