R: Output A Pivot-like Table With Subtotals
I'm trying to make a cross tabulation in R, and having its output resemble as much as possible what I'd get in an Excel pivot table. The objective is to replace a report made manua
Solution 1:
Replace the left hand side with:
ministry * (department + 1) + 1
That is, try this:
tabular(ministry * (department + 1) + 1 ~
((Count = budget) + (Avg = (mean * budget)) + (Total = (sum * budget))),
data = df)
giving:
Avg Total
ministry department Count budget budget
ministry 1 department 1 5 479871 2399356
department 2 1 770028 770028
department 3 1 184673 184673
All 7 479151 3354057
ministry 2 department 1 2 170818 341637
department 2 1 183373 183373
department 3 3 415480 1246440
All 6 295242 1771449
ministry 3 department 1 0 NaN 0
department 2 5 680102 3400509
department 3 2 165118 330235
All 7 532963 3730744
All 20 442813 8856250
Update: correction.
Post a Comment for "R: Output A Pivot-like Table With Subtotals"