Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answers so far work to a certain degree, but don't fully answer your question. In particular, they don't address the issue of a case in which there are no shops which sold a particular product. From your example input and desired output, there were no shops which sold "Product3". Indeed, "Product3" does not even appear in your source <code>data.frame</code>. Additionally, they do not address the possible situation of having more than one row for each Shop + Product combination.</p> <p>Here's a modified version of your data and the two solutions so far. I've added another row for a combination of "Shop1" and "Product1". Notice that I have converted your products to a <code>factor</code> variable that includes the levels that the variable can take, even if none of the cases actually has that level.</p> <pre><code>mydf &lt;- data.frame( Shop.Name = c("Shop1", "Shop1", "Shop2", "Shop3", "Shop3", "Shop1"), Items = c(2, 4, 3, 2, 1, 2), Product = factor( c("Product1", "Product2", "Product1", "Product1", "Product4", "Product1"), levels = c("Product1", "Product2", "Product3", "Product4"))) </code></pre> <ol> <li><p><code>dcast</code> from "reshape2"</p> <pre><code>library(reshape2) dcast(mydf, formula = Shop.Name ~ Product, value="Items", fill=0) # Using Product as value column: use value.var to override. # Aggregation function missing: defaulting to length # Error in .fun(.value[i], ...) : # 2 arguments passed to 'length' which requires 1 </code></pre> <p>Wha? Suddenly does not work. Do this instead:</p> <pre><code>dcast(mydf, formula = Shop.Name ~ Product, fill = 0, value.var = "Items", fun.aggregate = sum, drop = FALSE) # Shop.Name Product1 Product2 Product3 Product4 # 1 Shop1 4 4 0 0 # 2 Shop2 3 0 0 0 # 3 Shop3 2 0 0 1 </code></pre></li> <li><p>Let's be oldschool. <code>cast</code> from "reshape"</p> <pre><code>library(reshape) cast(mydf, formula = Shop.Name ~ Product, value="Items", fill=0) # Aggregation requires fun.aggregate: length used as default # Shop.Name Product1 Product2 Product4 # 1 Shop1 2 1 0 # 2 Shop2 1 0 0 # 3 Shop3 1 0 1 </code></pre> <p>Eh. Not what you wanted again... Try this instead:</p> <pre><code>cast(mydf, formula = Shop.Name ~ Product, value = "Items", fill = 0, add.missing = TRUE, fun.aggregate = sum) # Shop.Name Product1 Product2 Product3 Product4 # 1 Shop1 4 4 0 0 # 2 Shop2 3 0 0 0 # 3 Shop3 2 0 0 1 </code></pre></li> <li><p>Let's get back to basics. <code>xtabs</code> from base R</p> <pre><code>xtabs(Items ~ Shop.Name + Product, mydf) # Product # Shop.Name Product1 Product2 Product3 Product4 # Shop1 4 4 0 0 # Shop2 3 0 0 0 # Shop3 2 0 0 1 </code></pre> <p>Or, if you prefer a <code>data.frame</code> (note that your "Shop.Name" variable has been converted to the <code>row.names</code> of the <code>data.frame</code>):</p> <pre><code>as.data.frame.matrix(xtabs(Items ~ Shop.Name + Product, mydf)) # Product1 Product2 Product3 Product4 # Shop1 4 4 0 0 # Shop2 3 0 0 0 # Shop3 2 0 0 1 </code></pre></li> </ol>
    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