I have a problem with two data frames when I try to calculate percentages. In the first data frame, I have cumulative sums for different operations that a person does. This is a data frame, the first is an initial data frame with information for each person:
z=data.frame(ID=c("0001","0002","0002","0001","0003","0003","0004","0004","0001","0003"),Amount=c(10,20,10,30,50,10,40,10,10,30),Place=c("KFC","Marcys","Ezone","Ezone","Italocafe","Italocafe","KFC","Walmart","KFC","KFC"))
After I aggregated this, I:
ID Final.Amount 1 0001 50 2 0002 30 3 0003 90 4 0004 50
I want to calculate the percentage for each identifier associated with the Place variable, I tried with plyr, but I did not get the result. I am looking for someone like this:
ID Final.Amount Perct.KFC Perct.Macys Perct.Ezonne Perct.Italocafe Percent.Walmart 1 0001 50 40% 0% 60% 0% 0% 2 0002 30 0% 67% 33% 0% 0% 3 0003 90 33% 0% 0% 67% 0% 4 0004 50 80% 0% 0% 0% 20%
I tried with plyr, but I do not get the correct structure, I do not know if I need sqldf or another package.