{
  "id": 9394,
  "title": "My R code is too slow!",
  "url": "/competitions/acquire-valued-shoppers-challenge/discussion/9394",
  "author_name": "",
  "post_date": "2014-06-05T06:56:33.737Z",
  "votes": null,
  "comment_count": 2,
  "views": 1521,
  "content": "<p>Hi, everyone,</p>\n<p>I would like to make my data set like&nbsp;Triskelion's in another thread, but &nbsp;it is not successful because my code is too slow in R.</p>\n<p>Here is my code sample:</p>\n<p style=\"text-align: justify\">&nbsp;</p>\n<p style=\"text-align: justify\">&nbsp;</p>\n<p style=\"padding-left: 30px; text-align: justify;\">for(i in 1:nrow(dtrain))<br>{<br>&nbsp; &nbsp;&nbsp;dsub&lt;-reduced2[reduced2$id==dtrain$id[i],]<br>&nbsp; &nbsp;&nbsp;dtrain$has_bought_company[i]&lt;-nrow(dsub[dsub$company==dtrain$company[i],])</p>\n<p style=\"padding-left: 30px; text-align: justify;\">&nbsp; &nbsp; ........<br>}</p>\n<p style=\"padding-left: 30px; text-align: justify;\">&nbsp;</p>\n<p style=\"padding-left: 30px; text-align: justify;\">&nbsp;</p>\n<p>I made dtrain by combining &quot;offers&quot;&nbsp;with &quot;trainHistory&quot;.</p>\n<p>Then I count up how many times the shopper has bought the company.</p>\n<p>Is there any faster code or is it better to use another tools&nbsp;such as&nbsp;python / SQL?</p>",
  "messages": [
    {
      "id": "48689",
      "postDate": "06/05/2014 06:56:33",
      "content": "<p>Hi, everyone,</p>\n<p>I would like to make my data set like&nbsp;Triskelion's in another thread, but &nbsp;it is not successful because my code is too slow in R.</p>\n<p>Here is my code sample:</p>\n<p style=\"text-align: justify\">&nbsp;</p>\n<p style=\"text-align: justify\">&nbsp;</p>\n<p style=\"padding-left: 30px; text-align: justify;\">for(i in 1:nrow(dtrain))<br>{<br>&nbsp; &nbsp;&nbsp;dsub&lt;-reduced2[reduced2$id==dtrain$id[i],]<br>&nbsp; &nbsp;&nbsp;dtrain$has_bought_company[i]&lt;-nrow(dsub[dsub$company==dtrain$company[i],])</p>\n<p style=\"padding-left: 30px; text-align: justify;\">&nbsp; &nbsp; ........<br>}</p>\n<p style=\"padding-left: 30px; text-align: justify;\">&nbsp;</p>\n<p style=\"padding-left: 30px; text-align: justify;\">&nbsp;</p>\n<p>I made dtrain by combining &quot;offers&quot;&nbsp;with &quot;trainHistory&quot;.</p>\n<p>Then I count up how many times the shopper has bought the company.</p>\n<p>Is there any faster code or is it better to use another tools&nbsp;such as&nbsp;python / SQL?</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "48789",
      "postDate": "06/06/2014 18:16:19",
      "content": "<p>Subsetting <em>reduced2</em> iteratively is very expensive:</p>\n<p><code>system.time(length(reduced[trans$id==1,1]))<br> user system elapsed <br> 9.022 4.089 17.168 &lt;br/&gt;&lt;br/&gt;</code></p>\n<p>So say ~10sec * ~311000 users = 3110000 secs ~= 35 days +/-</p>\n<p>Consider package&nbsp;<a href=\"http://datatable.r-forge.r-project.org/\">data.table</a>&nbsp;with which the following takes a total of ~12 secs. &lt;br/&gt;&lt;br/&gt;</p>\n<p><code>&gt; system.time(t1 &lt;- trans[,sum(purchaseamount),by=list(id,category)])<br>user system elapsed <br>12.004 14.401 38.805 <br>&gt; head(t1)<br>&nbsp; &nbsp;id &nbsp;category &nbsp;V1<br>1: 86246 &nbsp;707 &nbsp;535.96<br>2: 86246 6319 &nbsp;165.62<br>3: 86246 9753 9286.86<br>4: 86246 2509 &nbsp;146.33<br>5: 86246 5555 &nbsp;134.73<br>6: 86246 9909 &nbsp;699.81&lt;br/&gt;&lt;br/&gt;</code></p>\n<p>Data tables are a subclass of data.frames. They can be confusing at first, but the performance is worth the cognitive dissonance. Also this:</p>\n<p><code>trans=fread(&quot;../data/reduced2.csv&quot;,sep=&quot;,&quot;,header=T) </code></p>\n<p>is up to five times faster than</p>\n<p><code>read.csv()</code></p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "48828",
      "postDate": "06/07/2014 13:53:41",
      "content": "<p>Thank you for your help ,&nbsp;mariobluedog.</p>\n<p>It is really good information for me.</p>\n<p>I will try this way!</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 48789,
      "author_name": "numinary",
      "author_url": "",
      "post_date": "06/06/2014 18:16:19",
      "content": "<p>Subsetting <em>reduced2</em> iteratively is very expensive:</p>\n<p><code>system.time(length(reduced[trans$id==1,1]))<br> user system elapsed <br> 9.022 4.089 17.168 &lt;br/&gt;&lt;br/&gt;</code></p>\n<p>So say ~10sec * ~311000 users = 3110000 secs ~= 35 days +/-</p>\n<p>Consider package&nbsp;<a href=\"http://datatable.r-forge.r-project.org/\">data.table</a>&nbsp;with which the following takes a total of ~12 secs. &lt;br/&gt;&lt;br/&gt;</p>\n<p><code>&gt; system.time(t1 &lt;- trans[,sum(purchaseamount),by=list(id,category)])<br>user system elapsed <br>12.004 14.401 38.805 <br>&gt; head(t1)<br>&nbsp; &nbsp;id &nbsp;category &nbsp;V1<br>1: 86246 &nbsp;707 &nbsp;535.96<br>2: 86246 6319 &nbsp;165.62<br>3: 86246 9753 9286.86<br>4: 86246 2509 &nbsp;146.33<br>5: 86246 5555 &nbsp;134.73<br>6: 86246 9909 &nbsp;699.81&lt;br/&gt;&lt;br/&gt;</code></p>\n<p>Data tables are a subclass of data.frames. They can be confusing at first, but the performance is worth the cognitive dissonance. Also this:</p>\n<p><code>trans=fread(&quot;../data/reduced2.csv&quot;,sep=&quot;,&quot;,header=T) </code></p>\n<p>is up to five times faster than</p>\n<p><code>read.csv()</code></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 48828,
      "author_name": "chanfuku18",
      "author_url": "",
      "post_date": "06/07/2014 13:53:41",
      "content": "<p>Thank you for your help ,&nbsp;mariobluedog.</p>\n<p>It is really good information for me.</p>\n<p>I will try this way!</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "48689": "",
    "48789": "",
    "48828": ""
  },
  "source": "meta"
}