Add totals

If you want to include a row in the end of your data frame either by column or by row, you can use the adorn_totals function.

Package

Install and load package janitor.

Create dummy data set

some_df <- data.frame( drug = rep(c('A','B', 'C'), 2),
                                       year =  rep(c(2016, 2017), 3),
                                        patients = sample(5:100, 6))

adorn_totals to all rows

some_df %>%
   adorn_totals("row")
##   drug  year patients
##      A  2016       17
##      B  2017       85
##      C  2016       95
##      A  2017       92
##      B  2016       58
##      C  2017       80
##  Total 12099      427

adorn_totals to all columns

some_df %>%
adorn_totals("col")   
##  drug year patients Total
##     A 2016       17  2033
##     B 2017       85  2102
##     C 2016       95  2111
##     A 2017       92  2109
##     B 2016       58  2074
##     C 2017       80  2097

adorn_totals to certain columns

some_df %>%
  adorn_totals("row",,,, -year) # Exempts year column
##   drug year patients
##      A 2016       17
##      B 2017       85
##      C 2016       95
##      A 2017       92
##      B 2016       58
##      C 2017       80
##  Total    -      427

another example of adorn_totals to certain columns

some_df2 <- data.frame( locationA = sample(1:100, 5), locationB = sample(1:100, 5),
locationC = sample(1:100,5))

some_df2 %>%
  adorn_totals(,,,,contains("location"))
## Because the first column was specified to be totaled, it does not contain the label 'Total' (or user-specified name) in the totals row
##  locationA locationB locationC
##         67        46        61
##         84         2         2
##         96         1        23
##         37        73        54
##         68         3         6
##        352       125       146