I have this data frame and I have calculated the cumulative count for ID
for each Date
, which is cumcount
df<-
structure(list(Date = structure(c(18632, 18632, 18632, 18632,
18632, 18632, 18632, 18632, 18632, 18632, 18633, 18633, 18633,
18633, 18633, 18633, 18633, 18642, 18642, 18642, 18642, 18642,
18642, 18642, 18642, 18642, 18642, 18642, 18642, 18642, 18642,
18642, 18642, 18642, 18649, 18649, 18649, 18649, 18649, 18649,
18649, 18649, 18649, 18649, 18649, 18654, 18654, 18654, 18654,
18654, 18654, 18654, 18654, 18654, 18654, 18654, 18654, 18654,
18654, 18654, 18654, 18654, 18654, 18654, 18654, 18654, 18654,
18654, 18654, 18654, 18654, 18654, 18654, 18654, 18654, 18654,
18655, 18655, 18655, 18655, 18655, 18655, 18655, 18655, 18655,
18655, 18655, 18655, 18656, 18656, 18656, 18656, 18656, 18656,
18656, 18656, 18656, 18656, 18656, 18656, 18656, 18656, 18656,
18656, 18656, 18656, 18656, 18656, 18656, 18656, 18656, 18661,
18661, 18661, 18661, 18661, 18661, 18661, 18661, 18661, 18661,
18661, 18661, 18661, 18661, 18661, 18662, 18662, 18662, 18662,
18662, 18663, 18663, 18663, 18663, 18663, 18667, 18667, 18667,
18669, 18669, 18669, 18669, 18669, 18669, 18669, 18669, 18675,
18675, 18675, 18675, 18675, 18675, 18675, 18683, 18683, 18683,
18683, 18683, 18683, 18683, 18683, 18683, 18683, 18683, 18683,
18683, 18683, 18683, 18683, 18683, 18683, 18683, 18683, 18687,
18689, 18689, 18689, 18689, 18689, 18689, 18689, 18689, 18689,
18689, 18689, 18689, 18689, 18689, 18689, 18689, 18689, 18689,
18689, 18690, 18690, 18690, 18690, 18690, 18690, 18690, 18690,
18690, 18690, 18690, 18690, 18690, 18690, 18690, 18690, 18690,
18690, 18690, 18690, 18690, 18691, 18691, 18691, 18691, 18694,
18694, 18694, 18694, 18694, 18694, 18694, 18694, 18694, 18694,
18694, 18694, 18694, 18694, 18694, 18694, 18694, 18694, 18694,
18694, 18694, 18694, 18694, 18694, 18695, 18695, 18695, 18695,
18695, 18695, 18695, 18695, 18695, 18695, 18695, 18695, 18695,
18695, 18695, 18695, 18695, 18695, 18695, 18695, 18695, 18695,
18695, 18695, 18695, 18695, 18695, 18695, 18695, 18695, 18697,
18697, 18697, 18697, 18697, 18697, 18697, 18697, 18697, 18697,
18697, 18701, 18702, 18702, 18702, 18702, 18702, 18702, 18702,
18703, 18703, 18703, 18703, 18703, 18718, 18744, 18744, 18749,
18749, 18751, 18751, 18753, 18753, 18753, 18753, 18753, 18754,
18755, 18755, 18756, 18756, 18756, 18757, 18757, 18757, 18757,
18758, 18758, 18758, 18758, 18758, 18758, 18758, 18758, 18759,
18759, 18759, 18759, 18759, 18759, 18760, 18760, 18760, 18760,
18760, 18760, 18760, 18761, 18761, 18761, 18761, 18761, 18761,
18761, 18761, 18761, 18761, 18761, 18761, 18761, 18761, 18761,
18761, 18761, 18761, 18761, 18761, 18762, 18762, 18762, 18762,
18762, 18762, 18762, 18762, 18762, 18762, 18762, 18763, 18763,
18763, 18763, 18763, 18763, 18763, 18763, 18763, 18763, 18763,
18763, 18763, 18763, 18763, 18763, 18763, 18764, 18764, 18764,
18764, 18764, 18764, 18764, 18764, 18764, 18765, 18765, 18765,
18765, 18765, 18765, 18765, 18765, 18765, 18765, 18765, 18765,
18765, 18765, 18765, 18765, 18765, 18765, 18765, 18765, 18765,
18765, 18765, 18765, 18765, 18765, 18766, 18766, 18766, 18766,
18766, 18766, 18766, 18766, 18766, 18766, 18766, 18766, 18766,
18766, 18766, 18766, 18766, 18766, 18766, 18766, 18766, 18766,
18767, 18767, 18767, 18767, 18767, 18767, 18767, 18767, 18767,
18767, 18767, 18767, 18767, 18767, 18767, 18767, 18767, 18767,
18767, 18767, 18767, 18767, 18767, 18768, 18768, 18768, 18768,
18768, 18768, 18768, 18768, 18768, 18768, 18768, 18768, 18768,
18768, 18768, 18768, 18768, 18768, 18768, 18769, 18769, 18769,
18769, 18769, 18769, 18769, 18769, 18769, 18769, 18769, 18769,
18769), class = "Date"), ID = c("afh2745", "afh2790", "afh2791",
"afh2796", "afh2798", "afh2800", "avl3251", "avl5042", "avl6835",
"avl7197", "afh2764", "afh2769", "avl6837", "avl6838", "avl7042",
"avl7173", "avl7174", "avl5520", "avl5521", "avl6844", "avl6845",
"avl6846", "avl6847", "avl6849", "avl6850", "avl6851", "avl6852",
"avl6854", "avl6855", "avl6856", "avl6857", "avl6860", "avl7464",
"avl7472", "avl3578", "avl4902", "avl6399", "avl6835", "avl6862",
"avl6863", "avl6864", "avl6866", "avl6871", "avl6872", "avl7149",
"avl4018", "avl4045", "avl4211", "avl4221", "avl4250", "avl4269",
"avl4308", "avl4422", "avl6246", "avl6367", "avl6400", "avl6874",
"avl6876", "avl6879", "avl6880", "avl6881", "avl6882", "avl6883",
"avl6884", "avl6885", "avl6888", "avl6889", "avl6890", "avl6891",
"avl6892", "avl6894", "avl6895", "avl6896", "avl6897", "avl6898",
"avl7164", "avl3545", "avl3578", "avl3596", "avl3691", "avl4398",
"avl4904", "avl4911", "avl4912", "avl4913", "avl4915", "avl6029",
"avl7481", "avl4148", "avl4228", "avl4328", "avl4917", "avl4918",
"avl4919", "avl4920", "avl4922", "avl4925", "avl4926", "avl4927",
"avl4929", "avl4930", "avl4931", "avl4932", "avl4933", "avl4934",
"avl5183", "avl6247", "avl6316", "avl6385", "avl6490", "avl7170",
"afh2742", "afh2743", "afh2791", "afh2798", "avl6835", "avl6894",
"avl7197", "avl7873", "avl7874", "avl7875", "avl7876", "avl7877",
"avl7878", "avl7880", "d182385", "avl3084", "avl6191", "avl7881",
"avl7882", "avl7884", "afh2789", "afh2791", "avl3138", "avl7885",
"avl7886", "afh2742", "afh2790", "d182385", "afh2781", "avl6170",
"avl6176", "avl6183", "avl6193", "avl6194", "avl7884", "avl7887",
"avl4513", "avl6364", "avl7146", "avl7150", "avl7890", "avl7892",
"avl7894", "abj6404", "abj6405", "abj6406", "abj6407", "abj6409",
"avl3751", "avl4010", "avl4218", "avl4237", "avl4315", "avl4422",
"avl6331", "avl6332", "avl6367", "avl6876", "avl6883", "avl6884",
"avl6889", "avl6890", "avl7898", "avl6193", "abj6411", "abj6412",
"abj6413", "abj6414", "abj6415", "abj6417", "abj6418", "abj6419",
"abj6420", "abj6421", "afh2751", "avl3089", "avl3255", "avl3552",
"avl4295", "avl5628", "avl6159", "avl6162", "avl7199", "abj6423",
"abj6424", "abj6428", "abj6429", "abj6431", "abj6432", "abj6433",
"abj6435", "abj6442", "abj6443", "abj6444", "abj6448", "abj6451",
"avl3510", "avl3658", "avl3708", "avl3746", "avl3818", "avl3978",
"avl6209", "avl7515", "avl3768", "avl3907", "avl4724", "avl7150",
"abj6453", "abj6454", "abj6455", "abj6456", "abj6457", "abj6458",
"abj6461", "abj6463", "abj6465", "abj6471", "afh2882", "avl3932",
"avl4126", "avl4174", "avl4641", "avl4918", "avl4920", "avl6032",
"avl6247", "avl6400", "avl6490", "avl6866", "avl7596", "avl7877",
"abj6473", "abj6474", "abj6475", "abj6476", "abj6477", "abj6479",
"abj6481", "abj6482", "abj6485", "abj6487", "abj6490", "abj6491",
"abj6496", "abj6497", "abj6498", "abj6499", "avl3697", "avl4141",
"avl4328", "avl4397", "avl4550", "avl5436", "avl5439", "avl5440",
"avl5442", "avl5906", "avl6622", "avl6864", "avl7139", "avl7884",
"avl4328", "avl5445", "avl5446", "avl5450", "avl5451", "avl5452",
"avl5454", "avl5455", "avl5456", "avl5457", "avl7210", "avl5458",
"avl4018", "avl4221", "avl4250", "avl4280", "avl5459", "avl5461",
"avl6891", "avl3987", "avl4100", "avl5464", "avl5466", "avl5468",
"afh2847", "avl3744", "avl4297", "afh2864", "avl5520", "avl5098",
"avl5563", "abj6413", "abj6415", "abj6498", "avl4141", "avl6851",
"avl7474", "afh2777", "avl3509", "avl3064", "avl4295", "avl6385",
"avl3978", "avl4018", "avl5520", "avl5596", "atr5701", "avl3882",
"avl4272", "avl4297", "avl4549", "avl5459", "avl5461", "avl6194",
"afh2888", "atr5702", "avl4511", "avl5466", "avl6132", "avl6864",
"afh5509", "atr5703", "avl3596", "avl5442", "avl6851", "avl7450",
"avl7515", "afh2887", "atr4202", "atr4203", "atr5201", "avl3509",
"avl3818", "avl4100", "avl4211", "avl4280", "avl4297", "avl4931",
"avl5456", "avl5803", "avl6247", "avl6396", "avl6892", "avl7115",
"avl7139", "avl7188", "avl7494", "abj6407", "abj6455", "afh2888",
"atr7201", "avl3691", "avl4010", "avl4269", "avl4902", "avl6132",
"avl6890", "avl7455", "afh2866", "atr4204", "atr4206", "atr4217",
"atr7202", "atr7203", "avl3556", "avl3575", "avl4142", "avl4208",
"avl5461", "avl5906", "avl6662", "avl6890", "avl7497", "avl7875",
"avl7877", "afh2754", "avl3084", "avl3978", "avl4308", "avl4612",
"avl4656", "avl5466", "avl5826", "avl6132", "afh2769", "afh2866",
"afh4277", "atr4229", "atr4715", "atr4716", "atr5764", "atr5765",
"atr5766", "avl3596", "avl3692", "avl4174", "avl4280", "avl4930",
"avl4932", "avl6042", "avl6086", "avl6121", "avl6136", "avl6209",
"avl6894", "avl6898", "avl7188", "avl7403", "avl7467", "avl7472",
"abj6411", "abj6457", "afh2755", "atr4248", "atr4260", "atr4721",
"atr4722", "atr5784", "atr6701", "avl4100", "avl4153", "avl4269",
"avl4318", "avl5138", "avl5442", "avl5701", "avl5803", "avl5906",
"avl6396", "avl6864", "avl7188", "avl7882", "abj6407", "abj6461",
"afh2751", "atr4318", "atr4741", "atr5807", "atr5808", "atr6713",
"avl3691", "avl3711", "avl4031", "avl4191", "avl4523", "avl4913",
"avl5060", "avl6170", "avl6315", "avl6890", "avl7185", "avl7818",
"avl7875", "avl7884", "j215781", "abj6421", "abj6498", "atr5261",
"atr5865", "atr6212", "atr6723", "atr7224", "atr7225", "avl3575",
"avl4141", "avl4173", "avl4549", "avl5098", "avl5464", "avl5596",
"avl6702", "avl6703", "avl6849", "avl7213", "afh2754", "afh2772",
"afh2777", "afh2779", "afh2781", "afh2864", "afh2869", "atr4339",
"atr4760", "at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 5L, 6L, 7L, 8L, 9L,
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
15L, 16L, 18L, 19L, 20L, 21L, 22L, 23L, 1L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 1L, 2L, 3L, 5L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 1L, 2L, 3L, 4L, 5L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
3L, 4L, 5L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 6L, 7L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 1L, 2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -500L), groups = structure(list(
Date = structure(c(18632, 18633, 18642, 18649, 18654, 18655,
18656, 18661, 18662, 18663, 18667, 18669, 18675, 18683, 18687,
18689, 18690, 18691, 18694, 18695, 18697, 18701, 18702, 18703,
18718, 18744, 18749, 18751, 18753, 18754, 18755, 18756, 18757,
18758, 18759, 18760, 18761, 18762, 18763, 18764, 18765, 18766,
18767, 18768, 18769), class = "Date"), .rows = structure(list(
1:10, 11:17, 18:34, 35:45, 46:76, 77:88, 89:111, 112:126,
127:131, 132:136, 137:139, 140:147, 148:154, 155:174,
175L, 176:194, 195:215, 216:219, 220:243, 244:273, 274:284,
285L, 286:292, 293:297, 298L, 299:300, 301:302, 303:304,
305:309, 310L, 311:312, 313:315, 316:319, 320:327, 328:333,
334:340, 341:360, 361:371, 372:388, 389:397, 398:423,
424:445, 446:468, 469:487, 488:500), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -45L), .drop = TRUE))
Just for ease of understanding here is what the data look like in table format
Date ID n cumcount
<date> <chr> <int> <int>
1 2021-01-05 afh2745 1 1
2 2021-01-05 afh2790 1 2
3 2021-01-05 afh2791 1 3
4 2021-01-05 afh2796 1 4
5 2021-01-05 afh2798 1 5
6 2021-01-05 afh2800 1 6
7 2021-01-05 avl3251 1 7
8 2021-01-05 avl5042 1 8
9 2021-01-05 avl6835 1 9
10 2021-01-05 avl7197 1 10
11 2021-01-06 afh2764 1 1
12 2021-01-06 afh2769 1 2
13 2021-01-06 avl6837 1 3
14 2021-01-06 avl6838 1 4
15 2021-01-06 avl7042 1 5
16 2021-01-06 avl7173 1 6
17 2021-01-06 avl7174 1 7
18 2021-01-15 avl5520 1 1
19 2021-01-15 avl5521 1 2
20 2021-01-15 avl6844 1 3
21 2021-01-15 avl6845 1 4
22 2021-01-15 avl6846 1 5
23 2021-01-15 avl6847 1 6
24 2021-01-15 avl6849 1 7
25 2021-01-15 avl6850 1 8
What I would like to do next is get the overall cumulative totals day by day. I have tried the following
df %>%
mutate(max_count = max(cumcount)) %>%
group_by(Date) %>%
count(Date) %>%
mutate(new_cumcount = cumsum(n))
However this is the result I get
Date n new_cumcount
<date> <int> <int>
1 2021-01-05 10 10
2 2021-01-06 7 7
3 2021-01-15 17 17
4 2021-01-22 11 11
5 2021-01-27 31 31
6 2021-01-28 12 12
7 2021-01-29 23 23
8 2021-02-03 15 15
9 2021-02-04 5 5
10 2021-02-05 5 5
Why does the newly created col using mutate
just repeat the col that I am trying use cumsum
on. What am I missing?
For absolute clarity, I wish to have one days total added to the next and so on for all of the dates.
CodePudding user response:
You could summarize the data with the maximum of max_count
for each Date
and calculate the cumulative sums for those maxima.
library(dplyr)
df %>%
group_by(Date) %>%
summarise(max_count = max(cumcount)) %>%
# summarise() automatically drops the last level of grouping, i.e. Date
mutate(new_cumcount = cumsum(max_count))
# # A tibble: 45 × 3
# Date max_count new_cumcount
# <date> <int> <int>
# 1 2021-01-05 10 10
# 2 2021-01-06 7 17
# 3 2021-01-15 17 34
# 4 2021-01-22 11 45
# 5 2021-01-27 31 76
# 6 2021-01-28 12 88
# 7 2021-01-29 23 111
# 8 2021-02-03 15 126
# 9 2021-02-04 5 131
# 10 2021-02-05 5 136
# # … with 35 more rows