I have data.frame like this
x <- data.frame(Category=factor(c("One", "One", "Four", "Two","Two", "Three", "Two", "Four","Three")), City=factor(c("D","A","B","B","A","D","A","C","C")), Frequency=c(10,1,5,2,14,8,20,3,5)) Category City Frequency 1 One D 10 2 One A 1 3 Four B 5 4 Two B 2 5 Two A 14 6 Three D 8 7 Two A 20 8 Four C 3 9 Three C 5
I want to make a pivot table with the sum (Frequency) and use the ddply function as follows:
ddply(x,.(Category,City),summarize,Total=sum(Frequency)) Category City Total 1 Four B 5 2 Four C 3 3 One A 1 4 One D 10 5 Three C 5 6 Three D 8 7 Two A 34 8 Two B 2
But I need these results, sorted by the sum in each group of categories. Something like that:
Category City Frequency 1 Two A 34 2 Two B 2 3 Three D 14 4 Three C 5 5 One D 10 6 One A 1 7 Four B 5 8 Four C 3
I looked and tried to sort, organize, organize, but nothing similar does what I need. How can I do this in R?