Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cumsum table with missing value

Tags:

r

cumsum

dt <- data.table(Name =c("A","A","A","A","B","B","B","B","B"), 
             Number = c(1,3,3,4, 4, 1,1,5,8))

I created cumsum table in this way.

library(matrixStats)
tbl <- round(prop.table(table(dt), 1) * 100, 3)
tbl[] <- rowCumsums(tbl)
names(dimnames(tbl)) <- NULL
tbl[] <-  paste0(sub("^([^.]+)(\\.[^0]).*", "\\1\\2", tbl), "%")
cumsumtable <-  as.data.frame.matrix(tbl)

In original dt, 2,6 and 7 were missing so it didn't reflect the table.

enter image description here

My desired cumsum table is like this. 2,6 and 7 are filled with the percentage before.

enter image description here

like image 768
joerna Avatar asked Mar 27 '26 02:03

joerna


1 Answers

We can convert the 'Number' to factor column with levels specified

dt[, Number := factor(Number, levels = min(Number):max(Number))]

and then running the OP's code

cumsumtable
#   1   2   3    4    5    6    7    8
#A 25% 25% 75% 100% 100% 100% 100% 100%
#B 40% 40% 40%  60%  80%  80%  80% 100%

This can also be done via data.table methods after the column conversion to factor

dcast(dt[, .N,.(Name, Number)][, perc := 100*N/sum(N), Name], 
     Name ~ Number, value.var = 'perc', fill = 0, drop = FALSE)[,
      (2:9) := lapply(Reduce(`+`, .SD, accumulate = TRUE),
              function(x) paste0(x, "%")), .SDcols = -1][]
#  Name   1   2   3    4    5    6    7    8
#1:    A 25% 25% 75% 100% 100% 100% 100% 100%
#2:    B 40% 40% 40%  60%  80%  80%  80% 100%
like image 108
akrun Avatar answered Mar 29 '26 15:03

akrun