'Forward' cumulative sum in dplyr

When examining datasets from longitudinal studies, I commonly get results like this from a

dplyr

analysis chain from the raw data:

df = data.frame(n_sessions=c(1,2,3,4,5), n_people=c(59,89,30,23,4))

i.e. a count of how many participants have completed a certain number of assessments at this point in time.

Although it is useful to know how many people have completed exactly n sessions, we more often need to know how many have completed at least n sessions. As per the table below, a standard cumulative sum isn't appropriate, What we want are the values in the

n_total

column, which is a sort of "forwards cumulative sum" of the values in the

n_people

column. i.e. the value in each row should be the sum of the values of itself and all values above it, rather than the standard cumulative sum, which is the sum of all values up to and including itself: