title: Get data into a format for xgboost in R?
body: <p>Does someone have a very well-explained example of getting data into a format usable by xgboost in R?</p>
  
  <p>The <a href="https://github.com/dmlc/xgboost/blob/master/doc/get_started/index.md" rel="nofollow noreferrer">get started doc</a> doesn't help me. The data (<code>agaricus.train</code> and <code>agaricus.test</code>) are already in a specialized format (<code>dgCMatrix</code>):</p>
  
  <pre><code>&gt; str(agaricus.train)
  List of 2
   $ data :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
    .. ..@ i       : int [1:143286] 2 6 8 11 18 20 21 24 28 32 ...
    .. ..@ p       : int [1:127] 0 369 372 3306 5845 6489 6513 8380 8384 10991 ...
    .. ..@ Dim     : int [1:2] 6513 126
    .. ..@ Dimnames:List of 2
    .. .. ..$ : NULL
    .. .. ..$ : chr [1:126] "cap-shape=bell" "cap-shape=conical" "cap-shape=convex" "cap-shape=flat" ...
    .. ..@ x       : num [1:143286] 1 1 1 1 1 1 1 1 1 1 ...
    .. ..@ factors : list()
   $ label: num [1:6513] 1 0 0 1 0 0 0 1 0 0 ...
  </code></pre>
  
  <p>I saw <a href="https://www.snip2code.com/Snippet/482990/xgboost-example/" rel="nofollow noreferrer">this example code</a> use <a href="https://www.rdocumentation.org/packages/Matrix/versions/1.2-8/topics/sparse.model.matrix" rel="nofollow noreferrer">sparse.model.matrix</a>, but I'm still having a hard time putting together fairly plain data into the format xgboost needs.</p>
  
  <p>For example, suppose I have two data frames: <code>words</code> and <code>labels</code>.</p>
  
  <p>The <code>words</code> data frame has <code>sentence_id</code> and <code>word_id</code>, with one or more words per sentence.</p>
  
  <p>The <code>data_label</code> data frame has a sentence_id and label (say, 0 or 1 for a binary classification task).</p>
  
  <p>How do I get that data into a format to predict the label for a sentence?</p>
  
  <p>I can split train and test.</p>
  
  <p><strong>Edit</strong>: The simplest version of words and data_label:</p>
  
  <pre><code>words &lt;- data.frame(sentence_id=c(1, 1, 2, 2, 2),
                      word_id=c(1, 2, 1, 3, 4))
  data_label &lt;- data.frame(sentence_id=c(1, 2), label=c(0, 1))
  </code></pre>