Want to get a pivot table - like output, with two row levels in R -


i trying create grouped tables make excel pivot table. far, i've used ddply sum data groups, can't figure out how create subtotals other making them each individually , piecing them table.

here sample of data after grouped ddply:

species<-c("bigeye","bigeye","bigeye","bigeye","yellowfin","yellowfin", "yellowfin") country<-c("japan", "canada", "hongkong", "southkorea", "japan", "canada",  "malaysia") pounds<-c(445274,152467,9768,2406,216323,19689,108) dollars<-c(4298063, 2420140, 58596, 14432,3682677,212323, 5309) unitvalue<-c(9.65, 15.87, 6, 6, 17.02,10.78, 49.16) volumeshare <-c(73, 25, 1.6, .39, 91.62, 8.34, .05) valueshare <-c(63.29, 35.64, .86, .21, 94.42, 5.44, .14) fish<-data.frame(species, country, pounds, dollars, unitvalue, volumeshare, valueshare) 

it creates table this:

species country pounds dollars unitvalue volumeshare valueshare bigeye japan 445274 4298063 9.65 73.00 63.29 bigeye canada 152467 2420140 15.87 25.00 35.64 bigeye hongkong 9768 58596 6.00 1.60 0.86 bigeye southkorea 2406 14432 6.00 0.39 0.21 yellowfin japan 216323 3682677 17.02 91.62 94.42 yellowfin canada 19689 212323 10.78 8.34 5.44 yellowfin malaysia 108 5309 49.16 0.05 0.14

i'd tabulate data species, , include total row each species group (in case below total after species) except unitvalue column instead of including group summation in total row, i'd include quotient of dollars/pounds species group. how i'd turn out:

species/country pounds dollars unitvalue .... valueshare bigeye 609,975 6,791,231 37.52 59.44 canada 152,467 2,420,140 15.87..........21.81 hongkong . . japan . . southkorea . . yellowfin 236,120.................................... canada 19,689 japan malaysia

i've tried using function "tabular" following advice question "r: output pivot-like table subtotals", haven't been able work. when include dataframe, output includes countries , species dataframe not have, , ends being long. if manually create dataframe 1 above, works, when used output ddply using large dataframe, tabulate seems ignore small dataframe reference , includes every single country , species.

any or tips on appreciated.

thank you!


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -