r - Frequency table including zeros for unused values, on a data.table -
i have data set follows:
library(data.table) test <- data.table(structure(list(issue.date = structure(c(16041, 16056, 16042,15990, 15996, 16001, 15995, 15981, 15986, 15996, 15996, 16002,16015, 16020, 16025, 16032, 16023, 16084, 16077, 16102, 16104,16107, 16112, 16113, 16115, 16121, 16125, 16128, 16104, 16132,16133, 16135, 16139, 16146, 16151), class = "date"), complaint = structure(c(1l,4l, 4l, 4l, 4l, 4l, 4l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 1l,5l, 3l, 1l, 3l, 1l, 4l, 4l, 3l, 3l, 3l, 3l, 3l, 2l, 2l, 1l, 3l,3l, 3l), .label = c("a", "b", "c", "d", "e"), class = "factor"), yr = c("2013", "2013", "2013", "2013", "2013", "2013", "2013","2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013","2013", "2013", "2014", "2014", "2014", "2014", "2014", "2014","2014", "2014", "2014", "2014", "2014", "2014", "2014", "2014","2014", "2014", "2014", "2014"), month = c("2013-12", "2013-12","2013-12", "2013-10", "2013-10", "2013-10", "2013-10", "2013-10","2013-10", "2013-10", "2013-10", "2013-10", "2013-11", "2013-11","2013-11", "2013-11", "2013-11", "2014-01", "2014-01", "2014-02","2014-02", "2014-02", "2014-02", "2014-02", "2014-02", "2014-02","2014-02", "2014-02", "2014-02", "2014-03", "2014-03", "2014-03","2014-03", "2014-03", "2014-03"), da = c("02", "17", "03","12", "18", "23", "17", "03", "08", "18", "18", "24", "06","11", "16", "23", "14", "14", "07", "01", "03", "06", "11","12", "14", "20", "24", "27", "03", "03", "04", "06", "10","17", "22")), .names = c("issue.date", "complaint", "yr","month", "da"), class = c("data.table", "data.frame"), row.names = c(na,-35l)))
basically use data.table
create frequency table has complaint
, count
month
. trick need show count
of 0 if there no complaints
of type month
. know how without showing zeros, want know how include them.
test[ , count := .n, = "month,complaint"]
to directly counts each group:
setkey(test, month, complaint) # may need add allow.cartesian, depending on actual data test[cj(month, complaint, unique = true), .n, = .eachi] # month complaint n # 1: 2013-10 0 # 2: 2013-10 b 0 # 3: 2013-10 c 5 # 4: 2013-10 d 4 # 5: 2013-10 e 0 # 6: 2013-11 1 # 7: 2013-11 b 0 # 8: 2013-11 c 4 # 9: 2013-11 d 0 #10: 2013-11 e 0 #11: 2013-12 1 #12: 2013-12 b 0 #13: 2013-12 c 0 #14: 2013-12 d 2 #15: 2013-12 e 0 #16: 2014-01 0 #17: 2014-01 b 0 #18: 2014-01 c 1 #19: 2014-01 d 0 #20: 2014-01 e 1 #21: 2014-02 2 #22: 2014-02 b 0 #23: 2014-02 c 6 #24: 2014-02 d 2 #25: 2014-02 e 0 #26: 2014-03 1 #27: 2014-03 b 2 #28: 2014-03 c 3 #29: 2014-03 d 0 #30: 2014-03 e 0 # month complaint n
see first revision of answer if want have counts in full data.table
instead of summarizing.
Comments
Post a Comment