This question already has an answer here:
I am currently learning to work with data.frame and quite confused on how to reorder them.
At the moment, I have a data.frame that shows :
- column 1: a shop name
- column 2: a product
- column 3: the number of purchase for this product by this shop
or visually something like this:
+---+-----------+-------+----------+--+
| | Shop.Name | Items | Product | |
+---+-----------+-------+----------+--+
| 1 | Shop1 | 2 | Product1 | |
| 2 | Shop1 | 4 | Product2 | |
| 3 | Shop2 | 3 | Product1 | |
| 4 | Shop3 | 2 | Product1 | |
| 5 | Shop3 | 1 | Product4 | |
+---+-----------+-------+----------+--+
What I would like to achieve is the following "shop-centric" structure:
- column 1: a shop name
- column 2: Items sold for product1
- column 3: Items sold for product2
- column 4: Items sold for product3 ...
When there is no line for a specific shop/product (because of no sales), I would like to create a 0.
or
+---+-------+-------+-------+-------+-------+-----+--+--+
| | Shop | Prod1 | Prod2 | Prod3 | Prod4 | ... | | |
+---+-------+-------+-------+-------+-------+-----+--+--+
| 1 | Shop1 | 2 | 4 | 0 | 0 | ... | | |
| 2 | Shop2 | 3 | 0 | 0 | 0 | ... | | |
| 3 | Shop3 | 2 | 0 | 0 | 1 | ... | | |
+---+-------+-------+-------+-------+-------+-----+--+--+
Use
dcast
from thereshape2
library:If you want to use the original reshape package for any reason:
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
data.frame
. Additionally, they do not address the possible situation of having more than one row for each Shop + Product combination.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
factor
variable that includes the levels that the variable can take, even if none of the cases actually has that level.dcast
from "reshape2"Wha? Suddenly does not work. Do this instead:
Let's be oldschool.
cast
from "reshape"Eh. Not what you wanted again... Try this instead:
Let's get back to basics.
xtabs
from base ROr, if you prefer a
data.frame
(note that your "Shop.Name" variable has been converted to therow.names
of thedata.frame
):