{
  "id": 1274,
  "title": "Hard first day with R -",
  "url": "/competitions/WhatDoYouKnow/discussion/1274",
  "author_name": "",
  "post_date": "2012-01-15T01:22:03.510Z",
  "votes": null,
  "comment_count": 4,
  "views": 2924,
  "content": "<p>Dear Listeners,</p>\r\n<p>I had a hard first day with 'R'! (half day with importing the data and another half day with the following beginner problem:)</p>\r\n<p>Backround: The last ten year I worked with SAS (work) and the change to R is not so easy. Used to data steps I was not able to do some really easy computing and merging.</p>\r\n<p>&nbsp;What I was trying to do:</p>\r\n<p>Computing the mean question difficulty for each question_id with 'questiondiff&lt;-tapply(training$correct,training$question_id,mean)'</p>\r\n<p>So far so good, &nbsp;the result is a vector with the right numbers, but I am not able to get the second dimension for merging it (1 to many) with the original training data by question_id for further steps. Do I have to tell tapply that I still need the question_id?\r\n Or maybe I am just to stupid for merging in R and there is no dimension problem at all...and so all the Ideas in my head for the real job have to wait.... &nbsp;</p>\r\n<p>I hope somebody can give me a hint. Thanks for your time. For me it's bedtime. Good night.</p>\r\n<p>Greetings from Germany-</p>\r\n<p>Vielauge&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>",
  "messages": [
    {
      "id": "8105",
      "postDate": "01/15/2012 01:22:03",
      "content": "<p>Dear Listeners,</p>\r\n<p>I had a hard first day with 'R'! (half day with importing the data and another half day with the following beginner problem:)</p>\r\n<p>Backround: The last ten year I worked with SAS (work) and the change to R is not so easy. Used to data steps I was not able to do some really easy computing and merging.</p>\r\n<p>&nbsp;What I was trying to do:</p>\r\n<p>Computing the mean question difficulty for each question_id with 'questiondiff&lt;-tapply(training$correct,training$question_id,mean)'</p>\r\n<p>So far so good, &nbsp;the result is a vector with the right numbers, but I am not able to get the second dimension for merging it (1 to many) with the original training data by question_id for further steps. Do I have to tell tapply that I still need the question_id?\r\n Or maybe I am just to stupid for merging in R and there is no dimension problem at all...and so all the Ideas in my head for the real job have to wait.... &nbsp;</p>\r\n<p>I hope somebody can give me a hint. Thanks for your time. For me it's bedtime. Good night.</p>\r\n<p>Greetings from Germany-</p>\r\n<p>Vielauge&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "8109",
      "postDate": "01/15/2012 17:44:06",
      "content": "<p>Usually I would use ddply from the plyr package as a convenience wrapper to tapply that has some added features.</p>\r\n<p>However, it can be pretty slow, so I believe the use of tapply is justified.</p>\r\n<p>Having said that, for your specific problem, tapply implicitly transforms &quot;training$question<em>id&quot; into a factor and then computes the mean for each of the resulting levels. You should be able to recreate those levels in integer form with just a &quot;unique(training$question</em>id)&quot;.</p>\r\n<p>You can then make a matrix with two columns via cbind, or a full data frame via a data.frame call.</p>\r\n<p>Best of luck!</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "8111",
      "postDate": "01/15/2012 22:36:38",
      "content": "<p>Keep it going, mate! Learning R was my new years resolution, and I am totally hooked to it now..&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "8112",
      "postDate": "01/15/2012 23:02:33",
      "content": "<p>Hi Vielauge,<br>\r\n<br>\r\nI'm not sure I understood your question, but have you tried this:<br>\r\nas.integer(dimnames(questiondiff)[[1]])<br>\r\n<br>\r\nOr, in a data frame:<br>\r\ndata.frame(question_id=as.integer(dimnames(questiondiff)[[1]]), questiondiff)<br>\r\n<br>\r\nJames</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "8123",
      "postDate": "01/16/2012 19:07:25",
      "content": "<p>Hi everybody,</p>\r\n<p>many thanks for your responses.&nbsp;</p>\r\n<p>@Shea Parks</p>\r\n<p>Thank you. Your answer works and is definitely better than googling and searching in a lot of help documents for hours (with no result).</p>\r\n<p>@James Petterson</p>\r\n<p>I will try your answer too, but have not done it so far. I am sure I can learn something. (At first sight it looks quite similar to Shea Parks idea).</p>\r\n<p>@shaz24</p>\r\n<p>I will see. I am already addicted to kaggle, but R could be a problem. With two small childs (2 and 5) &nbsp;and regular work there is not too much time to get into R. I guess 44 days will not be enough for me to get to top twenty with such elemental programming\r\n difficulties. But maybe I can get the benchmark with a total different method. We will see.</p>\r\n<p>Have a good time. All of you. Bye.</p>\r\n<p>Vielauge</p>\r\n<p>&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 8109,
      "author_name": "biasvariance",
      "author_url": "",
      "post_date": "01/15/2012 17:44:06",
      "content": "<p>Usually I would use ddply from the plyr package as a convenience wrapper to tapply that has some added features.</p>\r\n<p>However, it can be pretty slow, so I believe the use of tapply is justified.</p>\r\n<p>Having said that, for your specific problem, tapply implicitly transforms &quot;training$question<em>id&quot; into a factor and then computes the mean for each of the resulting levels. You should be able to recreate those levels in integer form with just a &quot;unique(training$question</em>id)&quot;.</p>\r\n<p>You can then make a matrix with two columns via cbind, or a full data frame via a data.frame call.</p>\r\n<p>Best of luck!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 8111,
      "author_name": "shaz24",
      "author_url": "",
      "post_date": "01/15/2012 22:36:38",
      "content": "<p>Keep it going, mate! Learning R was my new years resolution, and I am totally hooked to it now..&nbsp;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 8112,
      "author_name": "jpetterson",
      "author_url": "",
      "post_date": "01/15/2012 23:02:33",
      "content": "<p>Hi Vielauge,<br>\r\n<br>\r\nI'm not sure I understood your question, but have you tried this:<br>\r\nas.integer(dimnames(questiondiff)[[1]])<br>\r\n<br>\r\nOr, in a data frame:<br>\r\ndata.frame(question_id=as.integer(dimnames(questiondiff)[[1]]), questiondiff)<br>\r\n<br>\r\nJames</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 8123,
      "author_name": "vielauge",
      "author_url": "",
      "post_date": "01/16/2012 19:07:25",
      "content": "<p>Hi everybody,</p>\r\n<p>many thanks for your responses.&nbsp;</p>\r\n<p>@Shea Parks</p>\r\n<p>Thank you. Your answer works and is definitely better than googling and searching in a lot of help documents for hours (with no result).</p>\r\n<p>@James Petterson</p>\r\n<p>I will try your answer too, but have not done it so far. I am sure I can learn something. (At first sight it looks quite similar to Shea Parks idea).</p>\r\n<p>@shaz24</p>\r\n<p>I will see. I am already addicted to kaggle, but R could be a problem. With two small childs (2 and 5) &nbsp;and regular work there is not too much time to get into R. I guess 44 days will not be enough for me to get to top twenty with such elemental programming\r\n difficulties. But maybe I can get the benchmark with a total different method. We will see.</p>\r\n<p>Have a good time. All of you. Bye.</p>\r\n<p>Vielauge</p>\r\n<p>&nbsp;</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "8105": "",
    "8109": "",
    "8111": "",
    "8112": "",
    "8123": ""
  },
  "source": "meta"
}