rdplyrcolumn-sum

Add a ColSum to vector in r using dplyr


I am trying to add the sum (of all the counts in a specific vector) in my data frame in R. Specifically, I want to keep all the counts and then add a sum at the end. In excel, you would do =sum(A1:A5232). Additionally, I don't know the length of the specific vector. See below:

#sumarize by colname
  NewDepartment <- List %>%
  group_by(NewDepartment) %>%
  tally(sort=TRUE)

The above code will give me the following:

                   NewDepartment     n
                       <chr> <int>
1                           <NA>   709
2                    Collections   454
3                      Telesales   281
4  Operations Control Management    93
5                   Underwriting    92

I want a total count at the end like this:

                   NewDepartment     n
                       <chr> <int>
1                           <NA>   709
2                    Collections   454
3                      Telesales   281
4  Operations Control Management    93
5                   Underwriting    92
6                   Total Sum       1721

How do I get the row # 6 above??


Solution

  • Try this:

    NewDepartment = rbind(NewDepartment, 
        data.frame(NewDepartment = "Total Sum", n = sum(NewDepartment$n)))