Skip to content Skip to sidebar Skip to footer

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  154798712399356
             department  21770028770028
             department  31184673184673All74791513354057
 ministry  2 department  12170818341637
             department  21183373183373
             department  334154801246440All62952421771449
 ministry  3 department  10       NaN       0
             department  256801023400509
             department  32165118330235All75329633730744All204428138856250

Update: correction.

Post a Comment for "R: Output A Pivot-like Table With Subtotals"