Home > Software design >  Sum values incrementally for panel data
Sum values incrementally for panel data

Time:10-16

I have a very basic question as I am relatively new to R. I was wondering how to add a value in a particular column to the previous one for each cross-sectional unit in my data separately. My data looks like this:

firm date value 
A    1    10
A    2    15
A    3    20 
A    4    0
B    1    0
B    2    1
B    3    5
B    4    10
C    1    3
C    2    2
C    3    10
C    4    1
D    1    7
D    2    3
D    3    6
D    4    9

And I want to achieve the data below. So I want to sum values for each cross-sectional unit incrementally.

firm date value cumulative value
A    1    10    10
A    2    15    25
A    3    20    45
A    4    0     45
B    1    0     0
B    2    1     1
B    3    5     6
B    4    10    16
C    1    3     3
C    2    2     5
C    3    10    15
C    4    1     16
D    1    7     7
D    2    3     10
D    3    6     16
D    4    9     25

Below is a reproducible example code. I tried lag() but couldn't figure out how to repeat it for each firm.

firm <- c("A","A","A","A","B","B","B","B","C","C","C", "C","D","D","D","D")
date <- c("1","2","3","4","1","2","3","4","1","2","3","4", "1", "2", "3", "4")
value <- c("10", "15", "20", "0", "0", "1", "5", "10", "3", "2", "10", "1", "7", "3", "6", "9")
data = data.frame(firm=firm, date=date, value=value)

Thanks!

CodePudding user response:

Does this work:

library(dplyr)

df %>% group_by(firm) %>% mutate(cumulative_value = cumsum(value))
# A tibble: 16 x 4
# Groups:   firm [4]
   firm   date value cumulative_value
   <chr> <int> <int>            <int>
 1 A         1    10               10
 2 A         2    15               25
 3 A         3    20               45
 4 A         4     0               45
 5 B         1     0                0
 6 B         2     1                1
 7 B         3     5                6
 8 B         4    10               16
 9 C         1     3                3
10 C         2     2                5
11 C         3    10               15
12 C         4     1               16
13 D         1     7                7
14 D         2     3               10
15 D         3     6               16
16 D         4     9               25

CodePudding user response:

Using base R with ave

data$cumulative_value <- with(data, ave(value, firm, FUN = cumsum))

-output

> data
   firm date value cumulative_value
1     A    1    10               10
2     A    2    15               25
3     A    3    20               45
4     A    4     0               45
5     B    1     0                0
6     B    2     1                1
7     B    3     5                6
8     B    4    10               16
9     C    1     3                3
10    C    2     2                5
11    C    3    10               15
12    C    4     1               16
13    D    1     7                7
14    D    2     3               10
15    D    3     6               16
16    D    4     9               25
  • Related