Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a cumulative column to an R dataframe using dplyr?

Tags:

dataframe

r

dplyr

I have the same question as this post, but I want to use dplyr:

With an R dataframe, eg:

df <- data.frame(id = rep(1:3, each = 5)                  , hour = rep(1:5, 3)                  , value = sample(1:15)) 

how do I add a cumulative sum column that matches the id?

Without dplyr the accepted solution of the previous post is:

df$csum <- ave(df$value, df$id, FUN=cumsum) 
like image 854
Racing Tadpole Avatar asked Feb 16 '14 23:02

Racing Tadpole


People also ask

How do you make a column cumulative in R?

Reverse cumulative sum of a column is calculated using rev() and cumsum() function. cumsum() function takes up column name as argument which computes the cumulative sum of the column and it is passed to rev() function which reverses the cumulative sum as shown below.

How do you do cumulative in R?

Calculate Cumulative Sum of a Numeric Object in R Programming – cumsum() Function. The cumulative sum can be defined as the sum of a set of numbers as the sum value grows with the sequence of numbers. cumsum() function in R Language is used to calculate the cumulative sum of the vector passed as argument.


1 Answers

Like this?

df <- data.frame(id = rep(1:3, each = 5),                  hour = rep(1:5, 3),                  value = sample(1:15))  mutate(group_by(df,id), csum=cumsum(value)) 

Or if you use the dplyr's piping operator:

df %>% group_by(id) %>% mutate(csum = cumsum(value)) 

Result in both cases:

Source: local data frame [15 x 4] Groups: id     id hour value csum 1   1    1     4      4 2   1    2    14     18 3   1    3     8     26 4   1    4     2     28 5   1    5     3     31 6   2    1    10     10 7   2    2     7     17 8   2    3     5     22 9   2    4    12     34 10  2    5     9     43 11  3    1     6      6 12  3    2    15     21 13  3    3     1     22 14  3    4    13     35 15  3    5    11     46 
like image 100
Stephen Henderson Avatar answered Sep 19 '22 00:09

Stephen Henderson