{
  "id": 13847,
  "title": "Help with R",
  "url": "/competitions/facebook-recruiting-iv-human-or-bot/discussion/13847",
  "author_name": "",
  "post_date": "2015-04-29T06:22:09.060Z",
  "votes": null,
  "comment_count": 12,
  "views": 3011,
  "content": "<p>Hi all!</p>\n<p>I am trying to learn R for this competition. Can someone tell me how can I merge bids and train files by bidder_id ? I have tried merge command so far. is there anything else for this?</p>\n\n<p>Here is the command I use.</p>\n<p>bids &lt;- data.frame(read.csv(file=&quot;data/bids.csv&quot;, head=TRUE, sep=&quot;,&quot;))<br>train &lt;- data.frame(read.csv(file=&quot;data/train.csv&quot;,head=TRUE, sep=&quot;,&quot;))</p>\n<p>m &lt;- merge(bids, train, by.x=&quot;bidder_id&quot;, by.y=&quot;bidder_id&quot;)</p>\n\n<p>Thanks.</p>",
  "messages": [
    {
      "id": "75366",
      "postDate": "04/29/2015 06:22:09",
      "content": "<p>Hi all!</p>\n<p>I am trying to learn R for this competition. Can someone tell me how can I merge bids and train files by bidder_id ? I have tried merge command so far. is there anything else for this?</p>\n\n<p>Here is the command I use.</p>\n<p>bids &lt;- data.frame(read.csv(file=&quot;data/bids.csv&quot;, head=TRUE, sep=&quot;,&quot;))<br>train &lt;- data.frame(read.csv(file=&quot;data/train.csv&quot;,head=TRUE, sep=&quot;,&quot;))</p>\n<p>m &lt;- merge(bids, train, by.x=&quot;bidder_id&quot;, by.y=&quot;bidder_id&quot;)</p>\n\n<p>Thanks.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "75399",
      "postDate": "04/29/2015 12:05:32",
      "content": "<p>@K0stla: I would expect that to work. Are you getting an error message?</p>\n<p>using merge() can be a little memory-heavy at times. If you are short on RAM (&lt;= 2GB, perhaps) then a workaround might start with</p>\n<p><span style=\"font-family: monospace, serif\">bidTrainIndex &lt;- match(bids$bidder_id,train$bidder_id)</span></p>\n<p><span style=\"font-family: monospace, serif\">## note, don't assign this into bids$trainIndex as that uses a lot more memory</span></p>\n<p>Other alternatives on a limited-memory system are only keeping those rows of bids that correspond to entries in the training data:</p>\n<p><span style=\"font-family: monospace, serif\">bids &lt;- bids[bids$bidder_id %in% train$bidder_id,]</span></p>\n<p><span style=\"font-family: monospace, serif\">gc() ## return the freed memory to the system</span></p>\n<p>But perhaps memory is not your problem?</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "75409",
      "postDate": "04/29/2015 12:57:47",
      "content": "<p>it works for me, it as you pointed out consumes a lot of memory sometimes. However, particulary in this case it works, I was wondering if there is something else, more efficient for merging two data frames.</p>\n<p>Thanks for your reply.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "75416",
      "postDate": "04/29/2015 14:17:59",
      "content": "<p>This is what I use most of the time, hope it helps;</p>\n<p>library('dplyr')</p>\n<p>left_join(train, bids) # detects similar named field names and joins on them</p>\n<p># OR</p>\n<p>left_join(train, bids, by = &quot;bidder_id&quot;) &nbsp;# to select a specific field to join on</p>\n<p>However, I do believe that left_join uses MERGE under the hood. could be wrong about that.&nbsp;</p>\n\n<p>If you are very used to SQL, you might want to look at the 'sqldf' package. Then you can write something like &quot;SELECT * FROM df.a INNER JOIN df.b ON id=id &quot;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "75417",
      "postDate": "04/29/2015 14:29:18",
      "content": "<p>I think you'll find that the data.table package is easier on the memory and much faster for this sort of thing.</p>\n<p><code>install.packages(&quot;data.table&quot;)</code></p>\n<p><code>library(data.table)</code></p>\n<p><code>bids&lt;-fread(&quot;data/bids.csv&quot;)</code></p>\n<p><code>train&lt;-fread(&quot;data/train.csv&quot;)</code></p>\n<p><code>m&lt;-merge(bids,train,by=&quot;bidder_id&quot;)</code></p>\n<p><code>#cast to data.frame if needed<br></code><code></code></p>\n<p><code>m&lt;-as.data.frame(m)</code><code></code></p>\n<p>Good luck</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "75466",
      "postDate": "04/29/2015 18:17:24",
      "content": "<p>One more question. Loading of bids csv file takes me some time. Is there a way to speed up this?</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "75522",
      "postDate": "04/29/2015 22:39:23",
      "content": "<p>You can use the package data.table and the function fread as described above. It speeds up a lot.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "75830",
      "postDate": "05/01/2015 01:44:17",
      "content": "<p>The bids table is a transactional table (potentially multiple rows per bidder) I suspect that a good amount of aggregation and date preprocessing is necessary before it can be meaningfully joined to the training and test sets.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "76009",
      "postDate": "05/02/2015 01:18:21",
      "content": "<p>I am also trying to learn R&nbsp; and I am doing the MITx class. I also decided to enter this competition just to see if I can solve some real world problems. However beyond merging my files and trying a failed&nbsp; CART model I am totally clueless as to what&nbsp; tools I might consider. Can someone give me a small hint? I was thinking text mining but that will not work because I believe that I cannot stem the IDs</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "76032",
      "postDate": "05/02/2015 05:16:46",
      "content": "<p>Take a look at this list:&nbsp;http://www.infoworld.com/article/2853707/machine-learning/11-open-source-tools-machine-learning.html . In particular, scikit-learn is a great place to start:&nbsp;http://scikit-learn.org/stable/ . This is a more generic answer than you are looking for, but scanning through the features offered by&nbsp;scikit-learn might provide some inspiration (that's what I'm going to do :).</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "79884",
      "postDate": "05/24/2015 03:09:25",
      "content": "<p>I just start learning R and I need some help.</p>\n<p>I have a data frame</p>\n<p>column1 column2<br>1 89<br>2 45<br>3 82<br><br>and I want to add a new column with its value is the difference between column2 value from present row and previous row. <br><br></p>\n<p>column1 column2 column3</p>\n<p>1 89 0<br>2 45 -44<br>3 82 37<br><br>Could anyone give me some clue how to archive it? I try to search from search engine but I couldn't find it.<br><br>Thanks.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "79885",
      "postDate": "05/24/2015 03:15:57",
      "content": "<p>With basic functions you could do:<br><br>column3 = t(c(0,diff(t(column2),1)))<br><br>where, <br>t(): transpose,<br>diff(): lagged difference.<br><br>PS.: Probably there is a direct way to do it - as usually in R :)</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "79934",
      "postDate": "05/24/2015 23:01:25",
      "content": "<p>you don't need to transpose the data. just use something like this</p>\n\n<p><code>df$column3 &lt;- diff(df$column2)</code></p>\n\n<p>You may have to add or remove a value at the beginning depending on how you want to&nbsp;correlate the results. I rarely get it right the first time.</p>\n\n<p><code>df$column3 &lt;- c(0,diff(df$column2)) &nbsp;#Add a 0 in the first row, shift down</code></p>\n<p><code></code><code>df$column3 &lt;- diff(df$column2)[-1] &nbsp; #Remove the first&nbsp;row, shift up</code></p>\n\n<p>Good luck,</p>\n<p>Jeff</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 75399,
      "author_name": "jamesdlawrence",
      "author_url": "",
      "post_date": "04/29/2015 12:05:32",
      "content": "<p>@K0stla: I would expect that to work. Are you getting an error message?</p>\n<p>using merge() can be a little memory-heavy at times. If you are short on RAM (&lt;= 2GB, perhaps) then a workaround might start with</p>\n<p><span style=\"font-family: monospace, serif\">bidTrainIndex &lt;- match(bids$bidder_id,train$bidder_id)</span></p>\n<p><span style=\"font-family: monospace, serif\">## note, don't assign this into bids$trainIndex as that uses a lot more memory</span></p>\n<p>Other alternatives on a limited-memory system are only keeping those rows of bids that correspond to entries in the training data:</p>\n<p><span style=\"font-family: monospace, serif\">bids &lt;- bids[bids$bidder_id %in% train$bidder_id,]</span></p>\n<p><span style=\"font-family: monospace, serif\">gc() ## return the freed memory to the system</span></p>\n<p>But perhaps memory is not your problem?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 75409,
      "author_name": "",
      "author_url": "",
      "post_date": "04/29/2015 12:57:47",
      "content": "<p>it works for me, it as you pointed out consumes a lot of memory sometimes. However, particulary in this case it works, I was wondering if there is something else, more efficient for merging two data frames.</p>\n<p>Thanks for your reply.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 75416,
      "author_name": "michelblaauw",
      "author_url": "",
      "post_date": "04/29/2015 14:17:59",
      "content": "<p>This is what I use most of the time, hope it helps;</p>\n<p>library('dplyr')</p>\n<p>left_join(train, bids) # detects similar named field names and joins on them</p>\n<p># OR</p>\n<p>left_join(train, bids, by = &quot;bidder_id&quot;) &nbsp;# to select a specific field to join on</p>\n<p>However, I do believe that left_join uses MERGE under the hood. could be wrong about that.&nbsp;</p>\n\n<p>If you are very used to SQL, you might want to look at the 'sqldf' package. Then you can write something like &quot;SELECT * FROM df.a INNER JOIN df.b ON id=id &quot;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 75417,
      "author_name": "speculation",
      "author_url": "",
      "post_date": "04/29/2015 14:29:18",
      "content": "<p>I think you'll find that the data.table package is easier on the memory and much faster for this sort of thing.</p>\n<p><code>install.packages(&quot;data.table&quot;)</code></p>\n<p><code>library(data.table)</code></p>\n<p><code>bids&lt;-fread(&quot;data/bids.csv&quot;)</code></p>\n<p><code>train&lt;-fread(&quot;data/train.csv&quot;)</code></p>\n<p><code>m&lt;-merge(bids,train,by=&quot;bidder_id&quot;)</code></p>\n<p><code>#cast to data.frame if needed<br></code><code></code></p>\n<p><code>m&lt;-as.data.frame(m)</code><code></code></p>\n<p>Good luck</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 75466,
      "author_name": "",
      "author_url": "",
      "post_date": "04/29/2015 18:17:24",
      "content": "<p>One more question. Loading of bids csv file takes me some time. Is there a way to speed up this?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 75522,
      "author_name": "asahin",
      "author_url": "",
      "post_date": "04/29/2015 22:39:23",
      "content": "<p>You can use the package data.table and the function fread as described above. It speeds up a lot.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 75830,
      "author_name": "rocelot",
      "author_url": "",
      "post_date": "05/01/2015 01:44:17",
      "content": "<p>The bids table is a transactional table (potentially multiple rows per bidder) I suspect that a good amount of aggregation and date preprocessing is necessary before it can be meaningfully joined to the training and test sets.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 76009,
      "author_name": "awitong",
      "author_url": "",
      "post_date": "05/02/2015 01:18:21",
      "content": "<p>I am also trying to learn R&nbsp; and I am doing the MITx class. I also decided to enter this competition just to see if I can solve some real world problems. However beyond merging my files and trying a failed&nbsp; CART model I am totally clueless as to what&nbsp; tools I might consider. Can someone give me a small hint? I was thinking text mining but that will not work because I believe that I cannot stem the IDs</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 76032,
      "author_name": "wgillett",
      "author_url": "",
      "post_date": "05/02/2015 05:16:46",
      "content": "<p>Take a look at this list:&nbsp;http://www.infoworld.com/article/2853707/machine-learning/11-open-source-tools-machine-learning.html . In particular, scikit-learn is a great place to start:&nbsp;http://scikit-learn.org/stable/ . This is a more generic answer than you are looking for, but scanning through the features offered by&nbsp;scikit-learn might provide some inspiration (that's what I'm going to do :).</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 79884,
      "author_name": "winsxx",
      "author_url": "",
      "post_date": "05/24/2015 03:09:25",
      "content": "<p>I just start learning R and I need some help.</p>\n<p>I have a data frame</p>\n<p>column1 column2<br>1 89<br>2 45<br>3 82<br><br>and I want to add a new column with its value is the difference between column2 value from present row and previous row. <br><br></p>\n<p>column1 column2 column3</p>\n<p>1 89 0<br>2 45 -44<br>3 82 37<br><br>Could anyone give me some clue how to archive it? I try to search from search engine but I couldn't find it.<br><br>Thanks.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 79885,
      "author_name": "",
      "author_url": "",
      "post_date": "05/24/2015 03:15:57",
      "content": "<p>With basic functions you could do:<br><br>column3 = t(c(0,diff(t(column2),1)))<br><br>where, <br>t(): transpose,<br>diff(): lagged difference.<br><br>PS.: Probably there is a direct way to do it - as usually in R :)</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 79934,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "05/24/2015 23:01:25",
      "content": "<p>you don't need to transpose the data. just use something like this</p>\n\n<p><code>df$column3 &lt;- diff(df$column2)</code></p>\n\n<p>You may have to add or remove a value at the beginning depending on how you want to&nbsp;correlate the results. I rarely get it right the first time.</p>\n\n<p><code>df$column3 &lt;- c(0,diff(df$column2)) &nbsp;#Add a 0 in the first row, shift down</code></p>\n<p><code></code><code>df$column3 &lt;- diff(df$column2)[-1] &nbsp; #Remove the first&nbsp;row, shift up</code></p>\n\n<p>Good luck,</p>\n<p>Jeff</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "75366": "",
    "75399": "",
    "75409": "",
    "75416": "",
    "75417": "",
    "75466": "",
    "75522": "",
    "75830": "",
    "76009": "",
    "76032": "",
    "79884": "",
    "79885": "",
    "79934": ""
  },
  "source": "meta"
}