 # Calculating cummunative median with conditions

Dear everyone,

Currently I have a dataset including various companies (gvkey) over multiple years (fyear).
Now I want to calculate the cummulative median per company (over de years) for variable NIBI. Can anyone help me how to do this?

Library(plyr)
gvkey <- c(1, 1, 1, 1, 2,2,2, 4, 4 )
fyear <- c(2005,2006,2007,2008, 2007,2008,2009 , 2011,2012)
nibi <- c(100, 110, 120, 130, 500, 550, 600, 50, 60)
lagAT <- c(1000,1500,1300,1200, 300,500, 800, 70, 40)

(Thesis <- data.frame(gvkey, fyear, nibi, lagAT) %>% arrange(gvkey,fyear))

Thesis <- ddply(Thesis, .(gvkey), mutate,
lagNibi = c(NA, nibi[-length(nibi)])
)

Thesis\$difNibi <- Thesis\$nibi - Thesis\$lagNibi
Thesis\$relNibi <- Thesis\$difNibi /Thesis\$lagAT

I already tried to calculate this via:

Library(cumstats)
Thesis\$medbath <- cummedian(Thesis\$relNibi[Thesis\$relNibi<0])
tmp\$csum <- ave(Thesis\$relNibi[Thesis\$relNibi<0], Thesis\$gvkey, FUN=cummedian)

But these only give NA's.

``````library(tidyverse)
gvkey <- c(1, 1, 1, 1, 2,2,2, 4, 4 )
fyear <- c(2005,2006,2007,2008, 2007,2008,2009 , 2011,2012)
nibi <- c(100, 110, 120, 130, 500, 550, 600, 50, 60)
(Thesis <- data.frame(gvkey, fyear, nibi) )

accumulate_list <- function(tbl,df_col_str){
df_col <- pull(tbl,df_col_str)
colvec <- unlist(df_col)
n <- length(colvec)
endpoints <- 1:n
map_dfr(endpoints,
~tibble(cumlistmedian=median(df_col[1:.]))) -> newout
bind_cols(tbl,newout)
}

(Thesis2 <- Thesis %>% arrange(gvkey,fyear) %>%
group_by(gvkey) %>%
group_map(~accumulate_list(.,"nibi")) )
(Thesis3 <- bind_rows(Thesis2))

gvkey fyear  nibi cumlistmedian
<dbl> <dbl> <dbl>         <dbl>
1     1  2005   100           100
2     1  2006   110           105
3     1  2007   120           110
4     1  2008   130           115
5     2  2007   500           500
6     2  2008   550           525
7     2  2009   600           550
8     4  2011    50            50
9     4  2012    60            55
``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.