title: Error when making a sparse matrix
body: <p>I am facing a problem I do not understand. It's a follow-up on answers suggested <a href="https://stackoverflow.com/questions/13281303/creating-co-occurrence-matrix">here</a> and <a href="https://stackoverflow.com/questions/26244685/count-every-possible-pair-of-values-in-a-column-grouped-by-multiple-columns?lq=1">here</a></p>
  
  <p>I have two identically structured datasets. One I created as a reproducible example for which the code works, and one which is real for which the code does not work. After staring at it for hours I cannot find what is causing the error.
  The following gives an example that works</p>
  
  <pre><code>    df &lt;- data.table(cbind(rep(seq(1,25), each = 4 )), cbind(rep(seq(1,40), length.out = 100)))
      colnames(df) &lt;- c("a", "b") #ignore warning
  setkey(df, a, b)
  </code></pre>
  
  <p>This is just to create a reproducible example. When I apply the - slightly adjusted - code suggested in the mentioned SO articles I get what I am looking for: a sparse matrix that indicates when two elements in column b occur together for values of column a</p>
  
  <pre><code>library(Matrix)
  s &lt;- sparseMatrix(
    df$a,
    df$b,
      dimnames = list(
          unique(df$a),unique(df$b)), x = 1)
  v &lt;- t(s) %*% s
  </code></pre>
  
  <p>Now I am doing - in my eyes - exactly the same on my real dataset which is much longer.  </p>
  
  <p>A sample <code>dput</code> below looks like this</p>
  
  <pre><code>test &lt;- dput(dk[1:50,])
  structure(list(pid = c(204L, 204L, 207L, 254L, 254L, 258L, 258L, 
  258L, 258L, 258L, 265L, 265L, 269L, 269L, 269L, 269L, 1520L, 
  1520L, 1520L, 1520L, 1532L, 1532L, 1534L, 1534L, 1534L, 1534L, 
  1539L, 1539L, 1543L, 1543L, 1546L, 1546L, 1546L, 1546L, 1546L, 
  1546L, 1546L, 1549L, 1549L, 1549L, 1559L, 1559L, 1559L, 1559L, 
  1559L, 1559L, 1559L, 1561L, 1561L, 1561L), cid = c(11023L, 11787L, 
  14232L, 14470L, 14480L, 1290L, 1637L, 4452L, 13964L, 14590L, 
  17814L, 23453L, 6658L, 10952L, 17259L, 27549L, 11034L, 22748L, 
  23345L, 23347L, 10487L, 11162L, 15570L, 15629L, 17983L, 17999L, 
  17531L, 22497L, 14425L, 14521L, 11495L, 24948L, 24962L, 24969L, 
  24972L, 24973L, 30627L, 17886L, 18428L, 23972L, 13890L, 13936L, 
  14432L, 21230L, 21271L, 21384L, 21437L, 341L, 354L, 6302L)), .Names = c("pid", 
  "cid"), sorted = c("pid", "cid"), class = c("data.table", "data.frame"
  ), row.names = c(NA, -50L), .internal.selfref = &lt;pointer: 0x0000000000100788&gt;)
  </code></pre>
  
  <p>Then when running the same formula, I get an error</p>
  
  <pre><code>s &lt;- sparseMatrix(test$pid,test$cid,dimnames = list(unique(test$pid), unique(test$cid)),x = 1)
  </code></pre>
  
  <p>The Error (which occurs in the <code>test</code> dataset as well) reads as follows:</p>
  
  <pre><code>Error in validObject(r) : 
    invalid class “dgTMatrix” object: length(Dimnames[[1]])' must match Dim[1]
  </code></pre>
  
  <p>The problem disappears when I remove the <code>dimnames</code> but I really need these dimnames to make sense of the results. I'm sure I'm missing out on something obvious. Can someone please tell me what it is ?</p>