I've the following dataset
Pet Shop | Item | Price |
---|---|---|
A | dog | 300 |
A | fish | 20 |
A | turtle | 50 |
A | dog | 250 |
A | cat | 280 |
A | rabbit | 180 |
A | cat | 270 |
B | dog | 350 |
B | fish | 70 |
B | cat | 220 |
B | turtle | 80 |
B | fish | 55 |
B | fish | 75 |
C | dog | 280 |
C | cat | 260 |
C | fish | 65 |
The code for the data is as follows
Pet_Shop = c(rep("A",7), rep("B",6), rep("C",3))
Item = c("Dog","Fish","Turtle","Dog","Cat","Rabbit","Cat","Dog","Fish","Cat","Turtle","Fish","Fish","Dog","Cat","Fish")
Price = c(300,20,50,250,280,180,270,350,70,220,80,55,75,280,260,65)
Data = data.frame(Pet_Shop, Item, Price)
I'm trying to change the data into the following table
Does anyone know how to do this? I tried doing the following method using spread but it returns an error
Test = spread(Data, Item, Price)
Error: Each row of output must be identified by a unique combination of keys. Keys are shared for 7 rows: * 5, 7 * 1, 4 * 9, 12, 13
Thank you!