Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are many ways to do this. This answer starts with my favorite ways, but also collects various ways from answers to similar questions scattered around this site.</p> <pre><code>tmp &lt;- data.frame(x=gl(2,3, labels=letters[24:25]), y=gl(3,1,6, labels=letters[1:3]), z=c(1,2,3,3,3,2)) </code></pre> <p>Using reshape2:</p> <pre><code>library(reshape2) acast(tmp, x~y, value.var="z") </code></pre> <p>Using matrix indexing:</p> <pre><code>with(tmp, { out &lt;- matrix(nrow=nlevels(x), ncol=nlevels(y), dimnames=list(levels(x), levels(y))) out[cbind(x, y)] &lt;- z out }) </code></pre> <p>Using <code>xtabs</code>:</p> <pre><code>xtabs(z~x+y, data=tmp) </code></pre> <p>You can also use <code>reshape</code>, as suggested here: <a href="https://stackoverflow.com/q/2126108/210673">Convert table into matrix by column names</a>, though you have to do a little manipulation afterwards to remove an extra columns and get the names right (not shown).</p> <pre><code>&gt; reshape(tmp, idvar="x", timevar="y", direction="wide") x z.a z.b z.c 1 x 1 2 3 4 y 3 3 2 </code></pre> <p>There's also <code>sparseMatrix</code> within the <code>Matrix</code> package, as seen here: <a href="https://stackoverflow.com/q/9833052/210673">R - convert BIG table into matrix by column names</a></p> <pre><code>&gt; with(tmp, sparseMatrix(i = as.numeric(x), j=as.numeric(y), x=z, + dimnames=list(levels(x), levels(y)))) 2 x 3 sparse Matrix of class "dgCMatrix" a b c x 1 2 3 y 3 3 2 </code></pre> <p>The <code>daply</code> function from the <code>plyr</code> library could also be used, as here: <a href="https://stackoverflow.com/a/7020101/210673">https://stackoverflow.com/a/7020101/210673</a></p> <pre><code>&gt; library(plyr) &gt; daply(tmp, .(x, y), function(x) x$z) y x a b c x 1 2 3 y 3 3 2 </code></pre> <p><code>dcast</code> from reshape2 also works, as here: <a href="https://stackoverflow.com/q/8093839/210673">Reshape data for values in one column</a>, but you get a data.frame with a column for the <code>x</code> value.</p> <pre><code>&gt; dcast(tmp, x~y, value.var="z") x a b c 1 x 1 2 3 2 y 3 3 2 </code></pre> <p>Similarly, <code>spread</code> from "tidyr" would also work for such a transformation:</p> <pre><code>library(tidyr) spread(tmp, y, z) # x a b c # 1 x 1 2 3 # 2 y 3 3 2 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload