在 pivottabler::qpvt 中提前进行小计

Bringing subtotals upfront in pivottabler::qpvt

想知道如何在 pivottabler::qpvt 中预先显示小计(第一行 and/or 第一列)。

library(pivottabler)

qpvt(
      dataFrame     = bhmtrains
    , rows          = c("=", "TOC")
    , columns       = c("TrainCategory", "PowerType")
    , calculations  = c(
                          "Number of Trains"="n()"
                        , "Maximum Speed"="max(SchedSpeedMPH, na.rm=TRUE)"
                        )
    )
#>                                        Express Passenger         Ordinary Passenger  Total  
#>                                        DMU    EMU    HST  Total  DMU   EMU    Total         
#> Number of Trains  Arriva Trains Wales   3079               3079   830           830   3909  
#>                   CrossCountry         22133         732  22865    63            63  22928  
#>                   London Midland        5638   8849       14487  5591  28201  33792  48279  
#>                   Virgin Trains         2137   6457        8594                       8594  
#>                   Total                32987  15306  732  49025  6484  28201  34685  83710  
#> Maximum Speed     Arriva Trains Wales     90                 90    90            90     90  
#>                   CrossCountry           125         125    125   100           100    125  
#>                   London Midland         100    110         110   100    100    100    110  
#>                   Virgin Trains          125    125         125                        125  
#>                   Total                  125    125  125    125   100    100    100    125

您可以随时将 table R6 对象转换为 data.frame 对象 并使用 dplyr 或索引重新排序列和行

my_table =qpvt(
      dataFrame     = bhmtrains
    , rows          = c("=", "TOC")
    , columns       = c("TrainCategory", "PowerType")
    , calculations  = c(
                          "Number of Trains"="n()"
                        , "Maximum Speed"="max(SchedSpeedMPH, na.rm=TRUE)"
                        )
    )
my_df=my_table$asDataFrame()%>%relocate(8,4,7)
my_df[c(5,10,1:4,6:9),]

                                     Total  Express Passenger Total Ordinary Passenger Total Express Passenger DMU Express Passenger EMU Express Passenger HST Ordinary Passenger DMU Ordinary Passenger EMU
Number of Trains Total                83710                   49025                    34685                 32987                 15306                   732                   6484                  28201
Maximum Speed Total                     125                     125                      100                   125                   125                   125                    100                    100
Number of Trains Arriva Trains Wales   3909                    3079                      830                  3079                    NA                    NA                    830                     NA
Number of Trains CrossCountry         22928                   22865                       63                 22133                    NA                   732                     63                     NA
Number of Trains London Midland       48279                   14487                    33792                  5638                  8849                    NA                   5591                  28201
Number of Trains Virgin Trains         8594                    8594                       NA                  2137                  6457                    NA                     NA                     NA
Maximum Speed Arriva Trains Wales        90                      90                       90                    90                    NA                    NA                     90                     NA
Maximum Speed CrossCountry              125                     125                      100                   125                    NA                   125                    100                     NA
Maximum Speed London Midland            110                     110                      100                   100                   110                    NA                    100                    100
Maximum Speed Virgin Trains             125                     125                       NA                   125                   125                    NA                     NA                     NA