I have the following dataframe, df:
structure(list(x = structure(1:961, levels = c("2019-11-30",
"2019-12-01", "2019-12-02", "2019-12-03", "2019-12-04", "2019-12-05",
"2019-12-06", "2019-12-07", "2019-12-08", "2019-12-09", "2019-12-10",
"2019-12-11", "2019-12-12", "2019-12-13", "2019-12-14", "2019-12-15",
"2019-12-16", "2019-12-17", "2019-12-18", "2019-12-19", "2019-12-20",
"2019-12-21", "2019-12-22", "2019-12-23", "2019-12-24", "2019-12-25",
"2019-12-26", "2019-12-27", "2019-12-28", "2019-12-29", "2019-12-30",
"2019-12-31", "2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04",
"2020-01-05", "2020-01-06", "2020-01-07", "2020-01-08", "2020-01-09",
"2020-01-10", "2020-01-11", "2020-01-12", "2020-01-13", "2020-01-14",
"2020-01-15", "2020-01-16", "2020-01-17", "2020-01-18", "2020-01-19",
"2020-01-20", "2020-01-21", "2020-01-22", "2020-01-23", "2020-01-24",
"2020-01-25", "2020-01-26", "2020-01-27", "2020-01-28", "2020-01-29",
"2020-01-30", "2020-01-31", "2020-02-01", "2020-02-02", "2020-02-03",
"2020-02-04", "2020-02-05", "2020-02-06", "2020-02-07", "2020-02-08",
"2020-02-09", "2020-02-10", "2020-02-11", "2020-02-12", "2020-02-13",
"2020-02-14", "2020-02-15", "2020-02-16", "2020-02-17", "2020-02-18",
"2020-02-19", "2020-02-20", "2020-02-21", "2020-02-22", "2020-02-23",
"2020-02-24", "2020-02-25", "2020-02-26", "2020-02-27", "2020-02-28",
"2020-02-29", "2020-03-01", "2020-03-02", "2020-03-03", "2020-03-04",
"2020-03-05", "2020-03-06", "2020-03-07", "2020-03-08", "2020-03-09",
"2020-03-10", "2020-03-11", "2020-03-12", "2020-03-13", "2020-03-14",
"2020-03-15", "2020-03-16", "2020-03-17", "2020-03-18", "2020-03-19",
"2020-03-20", "2020-03-21", "2020-03-22", "2020-03-23", "2020-04-26",
"2020-04-27", "2020-04-28", "2020-04-29", "2020-04-30", "2020-05-01",
"2020-05-02", "2020-05-03", "2020-05-04", "2020-05-05", "2020-05-06",
"2020-05-07", "2020-05-08", "2020-05-09", "2020-05-10", "2020-05-11",
"2020-05-12", "2020-05-13", "2020-05-14", "2020-05-15", "2020-05-16",
"2020-05-17", "2020-05-18", "2020-05-19", "2020-05-20", "2020-05-21",
"2020-05-22", "2020-05-23", "2020-05-24", "2020-05-25", "2020-05-26",
"2020-05-27", "2020-05-28", "2020-05-29", "2020-05-30", "2020-05-31",
"2020-06-01", "2020-06-02", "2020-06-03", "2020-06-04", "2020-06-05",
"2020-06-06", "2020-06-07", "2020-06-08", "2020-06-09", "2020-06-10",
"2020-06-11", "2020-06-12", "2020-06-13", "2020-06-14", "2020-06-15",
"2020-06-16", "2020-06-17", "2020-06-18", "2020-06-19", "2020-06-20",
"2020-06-21", "2020-06-22", "2020-06-23", "2020-06-24", "2020-06-25",
"2020-06-26", "2020-06-27", "2020-06-28", "2020-06-29", "2020-06-30",
"2020-07-01", "2020-07-02", "2020-07-03", "2020-07-04", "2020-07-05",
"2020-07-06", "2020-07-07", "2020-07-08", "2020-07-09", "2020-07-10",
"2020-07-11", "2020-07-12", "2020-07-13", "2020-07-14", "2020-07-15",
"2020-07-16", "2020-07-17", "2020-07-18", "2020-07-19", "2020-07-20",
"2020-07-21", "2020-07-22", "2020-07-23", "2020-07-24", "2020-07-25",
"2020-07-26", "2020-07-27", "2020-07-28", "2020-07-29", "2020-07-30",
"2020-07-31", "2020-08-01", "2020-08-02", "2020-08-03", "2020-08-04",
"2020-08-05", "2020-08-06", "2020-08-07", "2020-08-08", "2020-08-09",
"2020-08-10", "2020-08-11", "2020-08-12", "2020-08-13", "2020-08-14",
"2020-08-15", "2020-08-16", "2020-08-17", "2020-08-18", "2020-08-19",
"2020-08-20", "2020-08-21", "2020-08-22", "2020-08-23", "2020-08-24",
"2020-08-25", "2020-08-26", "2020-08-27", "2020-08-28", "2020-08-29",
"2020-08-30", "2020-08-31", "2020-09-01", "2020-09-02", "2020-09-03",
"2020-09-04", "2020-09-05", "2020-09-06", "2020-09-07", "2020-09-08",
"2020-09-09", "2020-09-10", "2020-09-11", "2020-09-12", "2020-09-13",
"2020-09-14", "2020-09-15", "2020-09-16", "2020-09-17", "2020-09-18",
"2020-09-19", "2020-09-20", "2020-09-21", "2020-09-22", "2020-09-23",
"2020-09-24", "2020-09-25", "2020-09-26", "2020-09-27", "2020-09-28",
"2020-09-29", "2020-09-30", "2020-10-01", "2020-10-02", "2020-10-03",
"2020-10-04", "2020-10-05", "2020-10-06", "2020-10-07", "2020-10-08",
"2020-10-09", "2020-10-10", "2020-10-11", "2020-10-12", "2020-10-13",
"2020-10-14", "2020-10-15", "2020-10-16", "2020-10-17", "2020-10-18",
"2020-10-19", "2020-10-20", "2020-10-21", "2020-10-22", "2020-10-23",
"2020-10-24", "2020-10-25", "2020-10-26", "2020-10-27", "2020-10-28",
"2020-10-29", "2020-10-30", "2020-10-31", "2020-11-01", "2020-11-02",
"2020-11-03", "2020-11-04", "2020-11-05", "2020-11-06", "2020-11-07",
"2020-11-08", "2020-11-09", "2020-11-10", "2020-11-11", "2020-11-12",
"2020-11-13", "2020-11-14", "2020-11-15", "2020-11-16", "2020-11-17",
"2020-11-18", "2020-11-19", "2020-11-20", "2020-11-21", "2020-11-22",
"2020-11-23", "2020-11-24", "2020-11-25", "2020-11-26", "2020-11-27",
"2020-11-28", "2020-11-29", "2020-11-30", "2020-12-01", "2020-12-02",
"2020-12-03", "2020-12-04", "2020-12-05", "2020-12-06", "2020-12-07",
"2020-12-08", "2020-12-09", "2020-12-10", "2020-12-11", "2020-12-12",
"2020-12-13", "2020-12-14", "2020-12-15", "2020-12-16", "2020-12-17",
"2020-12-18", "2020-12-19", "2020-12-20", "2020-12-21", "2020-12-22",
"2020-12-23", "2020-12-24", "2020-12-25", "2020-12-26", "2020-12-27",
"2020-12-28", "2020-12-29", "2020-12-30", "2020-12-31", "2021-01-01",
"2021-01-02", "2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06",
"2021-01-07", "2021-01-08", "2021-01-09", "2021-01-10", "2021-01-11",
"2021-01-12", "2021-01-13", "2021-01-14", "2021-01-15", "2021-01-16",
"2021-01-17", "2021-01-18", "2021-01-19", "2021-01-20", "2021-01-21",
"2021-01-22", "2021-01-23", "2021-01-24", "2021-01-25", "2021-01-26",
"2021-01-27", "2021-01-28", "2021-01-29", "2021-01-30", "2021-01-31",
"2021-02-01", "2021-02-02", "2021-02-03", "2021-02-04", "2021-02-05",
"2021-02-06", "2021-02-07", "2021-02-08", "2021-02-09", "2021-02-10",
"2021-02-11", "2021-02-12", "2021-02-13", "2021-02-14", "2021-02-15",
"2021-02-18", "2021-02-19", "2021-02-20", "2021-02-21", "2021-02-22",
"2021-02-23", "2021-02-24", "2021-02-25", "2021-02-26", "2021-02-27",
"2021-02-28", "2021-03-01", "2021-03-02", "2021-03-03", "2021-03-04",
"2021-03-05", "2021-03-06", "2021-03-07", "2021-03-08", "2021-03-09",
"2021-03-10", "2021-03-11", "2021-03-12", "2021-03-13", "2021-03-14",
"2021-03-15", "2021-03-16", "2021-03-17", "2021-03-18", "2021-03-19",
"2021-03-20", "2021-03-21", "2021-03-22", "2021-03-23", "2021-03-24",
"2021-03-25", "2021-03-26", "2021-03-27", "2021-03-28", "2021-03-29",
"2021-03-30", "2021-03-31", "2021-04-01", "2021-04-02", "2021-04-03",
"2021-04-04", "2021-04-05", "2021-04-06", "2021-04-07", "2021-04-08",
"2021-04-09", "2021-04-10", "2021-04-11", "2021-04-12", "2021-04-13",
"2021-04-14", "2021-04-15", "2021-04-16", "2021-04-17", "2021-04-18",
"2021-04-19", "2021-04-20", "2021-04-21", "2021-04-22", "2021-04-23",
"2021-04-24", "2021-04-25", "2021-04-26", "2021-04-27", "2021-04-28",
"2021-04-29", "2021-04-30", "2021-05-01", "2021-05-02", "2021-05-03",
"2021-05-04", "2021-05-05", "2021-05-06", "2021-05-07", "2021-05-08",
"2021-05-09", "2021-05-10", "2021-05-11", "2021-05-12", "2021-05-13",
"2021-05-14", "2021-05-15", "2021-05-16", "2021-05-17", "2021-05-18",
"2021-05-19", "2021-05-20", "2021-05-21", "2021-05-22", "2021-05-23",
"2021-05-24", "2021-05-25", "2021-05-26", "2021-05-27", "2021-05-28",
"2021-05-29", "2021-05-30", "2021-05-31", "2021-06-01", "2021-06-02",
"2021-06-03", "2021-06-04", "2021-06-05", "2021-06-06", "2021-06-07",
"2021-06-08", "2021-06-09", "2021-06-10", "2021-06-11", "2021-06-12",
"2021-06-13", "2021-06-14", "2021-06-15", "2021-06-16", "2021-06-17",
"2021-06-18", "2021-06-19", "2021-06-20", "2021-06-21", "2021-06-22",
"2021-06-23", "2021-06-24", "2021-06-25", "2021-06-26", "2021-06-27",
"2021-06-28", "2021-06-29", "2021-06-30", "2021-07-01", "2021-07-02",
"2021-07-03", "2021-07-04", "2021-07-05", "2021-07-06", "2021-07-07",
"2021-07-08", "2021-07-09", "2021-07-10", "2021-07-11", "2021-07-12",
"2021-07-13", "2021-07-14", "2021-07-15", "2021-07-16", "2021-07-17",
"2021-07-18", "2021-07-19", "2021-07-20", "2021-07-21", "2021-07-22",
"2021-07-23", "2021-07-24", "2021-07-25", "2021-07-26", "2021-07-27",
"2021-07-28", "2021-07-29", "2021-07-30", "2021-07-31", "2021-08-01",
"2021-08-02", "2021-08-03", "2021-08-04", "2021-08-05", "2021-08-06",
"2021-08-07", "2021-08-08", "2021-08-09", "2021-08-10", "2021-08-11",
"2021-08-12", "2021-08-13", "2021-08-14", "2021-08-15", "2021-08-16",
"2021-08-17", "2021-08-18", "2021-08-19", "2021-08-20", "2021-08-21",
"2021-08-22", "2021-08-23", "2021-08-24", "2021-08-25", "2021-08-26",
"2021-08-27", "2021-08-28", "2021-08-29", "2021-08-30", "2021-08-31",
"2021-09-01", "2021-09-02", "2021-09-03", "2021-09-04", "2021-09-05",
"2021-09-06", "2021-09-07", "2021-09-08", "2021-09-09", "2021-09-10",
"2021-09-11", "2021-09-12", "2021-09-13", "2021-09-14", "2021-09-15",
"2021-09-16", "2021-09-17", "2021-09-18", "2021-09-19", "2021-09-20",
"2021-09-21", "2021-09-22", "2021-09-23", "2021-09-24", "2021-09-25",
"2021-09-26", "2021-09-27", "2021-09-28", "2021-09-29", "2021-09-30",
"2021-10-01", "2021-10-02", "2021-10-03", "2021-10-04", "2021-10-05",
"2021-10-06", "2021-10-07", "2021-10-08", "2021-10-09", "2021-10-10",
"2021-10-11", "2021-10-12", "2021-10-13", "2021-10-14", "2021-10-15",
"2021-10-16", "2021-10-17", "2021-10-18", "2021-10-19", "2021-10-20",
"2021-10-21", "2021-10-22", "2021-10-23", "2021-10-24", "2021-10-25",
"2021-10-26", "2021-10-27", "2021-10-28", "2021-10-29", "2021-10-30",
"2021-10-31", "2021-11-01", "2021-11-02", "2021-11-03", "2021-11-04",
"2021-11-05", "2021-11-06", "2021-11-07", "2021-11-08", "2021-11-09",
"2021-11-10", "2021-11-11", "2021-11-12", "2021-11-13", "2021-11-14",
"2021-11-15", "2021-11-16", "2021-11-17", "2021-11-18", "2021-11-19",
"2021-11-20", "2021-11-21", "2021-11-22", "2021-11-23", "2021-11-24",
"2021-11-25", "2021-11-26", "2021-11-27", "2021-11-28", "2021-11-29",
"2021-11-30", "2021-12-01", "2021-12-02", "2021-12-03", "2021-12-04",
"2021-12-05", "2021-12-06", "2021-12-07", "2021-12-08", "2021-12-09",
"2021-12-10", "2021-12-11", "2021-12-12", "2021-12-13", "2021-12-14",
"2021-12-15", "2021-12-16", "2021-12-17", "2021-12-18", "2021-12-19",
"2021-12-20", "2021-12-21", "2021-12-22", "2021-12-23", "2021-12-24",
"2021-12-25", "2021-12-26", "2021-12-27", "2021-12-28", "2021-12-29",
"2021-12-30", "2021-12-31", "2022-01-01", "2022-01-02", "2022-01-03",
"2022-01-04", "2022-01-05", "2022-01-06", "2022-01-07", "2022-01-08",
"2022-01-09", "2022-01-10", "2022-01-11", "2022-01-12", "2022-01-13",
"2022-01-14", "2022-01-15", "2022-01-16", "2022-01-17", "2022-01-18",
"2022-01-19", "2022-01-20", "2022-01-21", "2022-01-22", "2022-01-23",
"2022-01-24", "2022-01-25", "2022-01-26", "2022-01-27", "2022-01-28",
"2022-01-29", "2022-01-30", "2022-01-31", "2022-02-01", "2022-02-02",
"2022-02-03", "2022-02-04", "2022-02-05", "2022-02-06", "2022-02-07",
"2022-02-08", "2022-02-09", "2022-02-10", "2022-02-11", "2022-02-12",
"2022-02-13", "2022-02-14", "2022-02-15", "2022-02-16", "2022-02-17",
"2022-02-18", "2022-02-19", "2022-02-20", "2022-02-21", "2022-02-22",
"2022-02-23", "2022-02-24", "2022-02-25", "2022-02-26", "2022-02-27",
"2022-02-28", "2022-03-01", "2022-03-02", "2022-03-03", "2022-03-04",
"2022-03-05", "2022-03-06", "2022-03-07", "2022-03-08", "2022-03-09",
"2022-03-10", "2022-03-11", "2022-03-12", "2022-03-13", "2022-03-14",
"2022-03-15", "2022-03-16", "2022-03-17", "2022-03-18", "2022-03-19",
"2022-03-20", "2022-03-21", "2022-03-22", "2022-03-23", "2022-03-24",
"2022-03-25", "2022-03-26", "2022-03-27", "2022-03-28", "2022-03-29",
"2022-03-30", "2022-03-31", "2022-04-01", "2022-04-02", "2022-04-03",
"2022-04-04", "2022-04-05", "2022-04-06", "2022-04-07", "2022-04-08",
"2022-04-09", "2022-04-10", "2022-04-11", "2022-04-12", "2022-04-13",
"2022-04-14", "2022-04-15", "2022-04-16", "2022-04-17", "2022-04-18",
"2022-04-19", "2022-04-20", "2022-04-21", "2022-04-22", "2022-04-23",
"2022-04-24", "2022-04-25", "2022-04-26", "2022-04-27", "2022-04-28",
"2022-04-29", "2022-04-30", "2022-05-01", "2022-05-02", "2022-05-03",
"2022-05-04", "2022-05-05", "2022-05-06", "2022-05-07", "2022-05-08",
"2022-05-09", "2022-05-10", "2022-05-11", "2022-05-12", "2022-05-13",
"2022-05-14", "2022-05-15", "2022-05-16", "2022-05-17", "2022-05-18",
"2022-05-19", "2022-05-20", "2022-05-21", "2022-05-22", "2022-05-23",
"2022-05-24", "2022-05-25", "2022-05-26", "2022-05-27", "2022-05-28",
"2022-05-29", "2022-05-30", "2022-05-31", "2022-06-01", "2022-06-02",
"2022-06-03", "2022-06-04", "2022-06-05", "2022-06-06", "2022-06-07",
"2022-06-08", "2022-06-09", "2022-06-10", "2022-06-11", "2022-06-12",
"2022-06-13", "2022-06-14", "2022-06-15", "2022-06-16", "2022-06-17",
"2022-06-18", "2022-06-19", "2022-06-20", "2022-06-21", "2022-06-22",
"2022-06-23", "2022-06-24", "2022-06-25", "2022-06-26", "2022-06-27",
"2022-06-28", "2022-07-02", "2022-07-03", "2022-07-04", "2022-07-05",
"2022-07-06", "2022-07-07", "2022-07-08", "2022-07-09", "2022-07-10",
"2022-07-11", "2022-07-12", "2022-07-13", "2022-07-14", "2022-07-15",
"2022-07-16", "2022-07-17", "2022-07-18", "2022-07-19", "2022-07-20",
"2022-07-21", "2022-07-22", "2022-07-23", "2022-07-24", "2022-07-25",
"2022-07-26", "2022-07-30", "2022-07-31", "2022-08-01", "2022-08-02",
"2022-08-03", "2022-08-04", "2022-08-05", "2022-08-06", "2022-08-07",
"2022-08-08", "2022-08-09", "2022-08-13", "2022-08-14", "2022-08-15",
"2022-08-16", "2022-08-17", "2022-08-18", "2022-08-19", "2022-08-20",
"2022-08-21", "2022-08-22", "2022-08-23", "2022-08-24", "2022-08-25",
"2022-08-26", "2022-08-27", "2022-08-28", "2022-08-29", "2022-08-30"
), class = "factor"), Freq = c(1733L, 4015L, 4303L, 4910L, 3522L,
5321L, 5027L, 3238L, 3505L, 6589L, 5685L, 4947L, 3060L, 2937L,
2903L, 2848L, 2608L, 2612L, 2749L, 3517L, 3665L, 3060L, 2569L,
18294L, 12148L, 2411L, 3449L, 1988L, 1829L, 2326L, 5475L, 5540L,
2990L, 7606L, 4555L, 2594L, 2844L, 3852L, 3836L, 3197L, 2879L,
3681L, 3852L, 3883L, 4277L, 3852L, 4769L, 6053L, 3411L, 4044L,
4556L, 3033L, 3404L, 3781L, 3154L, 2989L, 2514L, 1779L, 2431L,
3367L, 3809L, 4100L, 3231L, 2386L, 4859L, 3366L, 3162L, 3927L,
4927L, 4745L, 3976L, 6663L, 5079L, 4604L, 4038L, 3539L, 3863L,
3229L, 4094L, 3737L, 4130L, 3258L, 2793L, 3518L, 2484L, 3091L,
3254L, 5077L, 6138L, 4785L, 3941L, 3169L, 3172L, 3169L, 4099L,
4359L, 2936L, 2727L, 2469L, 2607L, 2796L, 2175L, 2380L, 2736L,
1911L, 2116L, 1334L, 1755L, 1540L, 1498L, 1624L, 1565L, 1053L,
1136L, 531L, 2051L, 4747L, 3713L, 3444L, 3366L, 2351L, 2990L,
2737L, 3341L, 3324L, 3352L, 3025L, 3034L, 2798L, 6294L, 3173L,
2381L, 3116L, 7693L, 3305L, 2020L, 1519L, 4155L, 4851L, 2881L,
3363L, 2842L, 2473L, 2500L, 2807L, 3427L, 3242L, 2834L, 2277L,
1518L, 1293L, 2077L, 2391L, 2784L, 2598L, 2439L, 1581L, 1615L,
2260L, 3139L, 3706L, 3420L, 2294L, 1758L, 2064L, 4765L, 3167L,
3078L, 2347L, 2251L, 1973L, 2261L, 2020L, 3583L, 2567L, 2692L,
3120L, 1974L, 1999L, 2232L, 2142L, 2146L, 3031L, 2278L, 2159L,
2358L, 3676L, 2663L, 2473L, 2744L, 2209L, 1887L, 1714L, 2542L,
2463L, 2779L, 3076L, 2087L, 1888L, 2012L, 1663L, 1983L, 3122L,
2244L, 2244L, 1536L, 1341L, 2339L, 3059L, 3169L, 11450L, 4797L,
5767L, 6397L, 3979L, 4319L, 3883L, 2412L, 2363L, 1680L, 1390L,
3319L, 3630L, 4479L, 3336L, 3244L, 2561L, 3390L, 4487L, 3841L,
4393L, 4166L, 3168L, 3855L, 4768L, 3310L, 4581L, 4952L, 10117L,
4505L, 2392L, 2261L, 3535L, 3599L, 3519L, 2993L, 3874L, 13878L,
8176L, 7789L, 7467L, 4444L, 3813L, 3056L, 2626L, 3661L, 5643L,
4664L, 4888L, 4062L, 5176L, 2296L, 4172L, 3070L, 4331L, 3255L,
3690L, 3322L, 3449L, 3250L, 3954L, 2836L, 3593L, 2818L, 2307L,
2838L, 1984L, 572L, 25604L, 9775L, 5367L, 9423L, 10249L, 4169L,
3180L, 3800L, 3127L, 3917L, 3074L, 1715L, 1801L, 2905L, 5582L,
2982L, 2722L, 26054L, 7396L, 4722L, 6250L, 4534L, 5098L, 6007L,
3814L, 6095L, 3363L, 3243L, 2238L, 2364L, 2713L, 1996L, 2117L,
2377L, 2702L, 2456L, 3402L, 4027L, 2558L, 2205L, 3918L, 3791L,
2547L, 3756L, 3394L, 2516L, 2234L, 3435L, 2629L, 3200L, 2507L,
2407L, 2121L, 1851L, 2027L, 2695L, 2456L, 2968L, 3008L, 3136L,
1829L, 1788L, 2739L, 2605L, 2201L, 2063L, 2537L, 2114L, 1881L,
3009L, 4067L, 3310L, 2800L, 2663L, 1910L, 1923L, 2643L, 3565L,
3926L, 2087L, 2349L, 1421L, 1574L, 2572L, 2873L, 2276L, 1901L,
1987L, 2599L, 1979L, 2177L, 2019L, 2043L, 1761L, 2028L, 1444L,
1998L, 2362L, 2953L, 4943L, 3398L, 2331L, 1960L, 2021L, 2902L,
4087L, 3352L, 4174L, 4906L, 3828L, 2470L, 3122L, 3536L, 6151L,
5048L, 3972L, 2600L, 2982L, 3599L, 3689L, 4302L, 4236L, 5424L,
3668L, 4087L, 5934L, 4555L, 3594L, 3293L, 2973L, 3036L, 4203L,
2895L, 12486L, 24477L, 14390L, 12224L, 8245L, 12426L, 5115L,
4420L, 3097L, 2421L, 3333L, 5281L, 4770L, 3341L, 3720L, 3767L,
3975L, 5051L, 3300L, 3637L, 3701L, 6690L, 3341L, 3342L, 2353L,
2763L, 4509L, 3178L, 3249L, 2681L, 2176L, 2093L, 3360L, 2871L,
3125L, 4315L, 4101L, 3824L, 2833L, 14263L, 8366L, 3938L, 8002L,
5380L, 2634L, 2158L, 4239L, 3055L, 3789L, 3065L, 2614L, 2472L,
2316L, 4014L, 3266L, 3684L, 2873L, 2148L, 1651L, 2150L, 3484L,
3975L, 3940L, 5409L, 4057L, 3457L, 3172L, 3145L, 3629L, 3583L,
5291L, 5038L, 3359L, 2343L, 3311L, 3863L, 5337L, 6546L, 3562L,
2423L, 3213L, 3019L, 7039L, 8057L, 4782L, 3539L, 3071L, 2750L,
3641L, 3663L, 4227L, 3050L, 2971L, 2905L, 2042L, 3176L, 7276L,
5240L, 3357L, 4890L, 3753L, 2236L, 2385L, 2619L, 2865L, 3204L,
4096L, 2306L, 1933L, 3004L, 3244L, 3513L, 3978L, 3847L, 2170L,
2112L, 3865L, 8556L, 4143L, 3300L, 3131L, 2383L, 2423L, 2934L,
4324L, 3438L, 3181L, 2552L, 2023L, 2189L, 2557L, 2687L, 3077L,
3131L, 2595L, 2544L, 2255L, 2321L, 3070L, 4986L, 4231L, 3777L,
2369L, 2137L, 3174L, 3538L, 3096L, 2918L, 2517L, 2209L, 2345L,
2765L, 2839L, 3503L, 3547L, 2994L, 1981L, 1780L, 2664L, 2617L,
383L, 2590L, 2276L, 1819L, 1524L, 2595L, 2655L, 2639L, 3849L,
4118L, 2023L, 1707L, 4849L, 4485L, 3571L, 6129L, 3345L, 3796L,
1749L, 2692L, 2602L, 3518L, 2424L, 2368L, 2366L, 2495L, 3393L,
3079L, 2926L, 3068L, 2159L, 2516L, 4885L, 5354L, 3575L, 2737L,
2797L, 2479L, 1991L, 1844L, 3451L, 5084L, 3924L, 4083L, 2817L,
2171L, 2688L, 4361L, 5857L, 4872L, 4658L, 3599L, 3272L, 3288L,
4610L, 5998L, 4051L, 3585L, 3003L, 3037L, 2874L, 3612L, 3692L,
4677L, 3935L, 2495L, 1936L, 3933L, 5173L, 3000L, 4017L, 6276L,
4662L, 3565L, 3642L, 5743L, 5297L, 5630L, 5853L, 3580L, 8417L,
4506L, 5057L, 6657L, 5561L, 4834L, 4696L, 3108L, 2814L, 6732L,
5923L, 5640L, 5705L, 4738L, 3277L, 4096L, 5164L, 5010L, 6667L,
6826L, 4637L, 3161L, 2998L, 4010L, 6037L, 6656L, 4601L, 3872L,
3263L, 3283L, 3803L, 4384L, 3008L, 3361L, 3128L, 2023L, 1957L,
4382L, 4088L, 4175L, 4624L, 3032L, 2789L, 2273L, 3345L, 3724L,
5015L, 3708L, 3505L, 3008L, 2274L, 2743L, 3294L, 4371L, 3158L,
3007L, 4219L, 3859L, 3238L, 3030L, 5063L, 5001L, 3184L, 2338L,
2676L, 2844L, 4591L, 4058L, 3284L, 2072L, 1323L, 1517L, 3573L,
4495L, 2773L, 2803L, 3664L, 2864L, 4525L, 4830L, 3901L, 3386L,
3198L, 3998L, 2477L, 2281L, 3167L, 2994L, 4391L, 4079L, 3951L,
4111L, 4174L, 6018L, 5062L, 3319L, 3644L, 3898L, 3056L, 2258L,
3418L, 3101L, 2766L, 3021L, 3097L, 3260L, 2541L, 3015L, 3068L,
3579L, 5615L, 6024L, 3544L, 4279L, 3485L, 3670L, 6398L, 5753L,
4069L, 2541L, 2256L, 2472L, 2849L, 4941L, 6027L, 6580L, 3299L,
3621L, 4608L, 5117L, 4109L, 4547L, 6919L, 7645L, 4715L, 5153L,
5915L, 4653L, 4711L, 8633L, 5522L, 5302L, 7510L, 9617L, 10461L,
8382L, 6701L, 5517L, 5467L, 4868L, 8606L, 5472L, 4702L, 3944L,
3490L, 4387L, 4968L, 4486L, 6137L, 4928L, 4191L, 3149L, 3511L,
9130L, 10724L, 10832L, 7911L, 5721L, 4824L, 7941L, 5077L, 7290L,
11041L, 18202L, 10671L, 5761L, 6211L, 7978L, 6625L, 5173L, 5715L,
7147L, 7357L, 3573L, 5490L, 5299L, 5416L, 4809L, 5494L, 3789L,
2804L, 3543L, 2972L, 2284L, 2354L, 3991L, 2356L, 2375L, 4355L,
3598L, 4654L, 4372L, 3993L, 3108L, 3080L, 3528L, 3755L, 4401L,
4272L, 2962L, 5252L, 2792L, 4034L, 3912L, 5813L, 4372L, 3205L,
3408L, 3824L, 4700L, 3707L, 3797L, 3802L, 3796L, 2830L, 4175L,
5389L, 3898L, 5583L, 5946L, 6932L, 3978L, 3678L, 5044L, 8381L,
4723L, 5084L, 4141L, 3298L, 11642L, 5766L, 7767L, 9179L, 11131L,
8179L, 6905L, 3631L, 3326L, 3467L, 2945L, 3753L, 1917L, 1336L,
1571L, 3512L, 1116L, 1251L, 1932L, 2189L, 2785L, 3161L, 3804L,
3859L, 4937L, 4200L, 10621L, 6268L, 7342L, 7001L, 5932L, 5183L,
11917L, 8051L, 9763L, 14061L, 10722L, 6173L, 5492L, 5360L, 5220L,
1108L, 1935L, 3790L, 4732L, 10167L, 7199L, 5544L, 7308L, 6308L,
6190L, 7392L, 1951L, 854L, 10228L, 11237L, 10738L, 7127L, 8779L,
8854L, 7333L, 5916L, 6135L, 8854L, 6963L, 5880L, 6865L, 7618L,
8886L, 7626L, 1355L)), class = "data.frame", row.names = c(NA,
-961L))
In the variable Freq we find the number of times that a particular event occurs. I would like to count these numbers by month, i.e. totaling all the entries of variable Freq by month. I tried with the following code:
library(lubridate)
aggregate(Freq~month(x), data = n_occur, sum)
but did not obtain the desired result:
month(x) Freq
1 1 317257
2 2 404837
3 3 373429
4 4 301919
5 5 339101
6 6 315882
7 7 324160
8 8 406973
9 9 243593
10 10 326444
11 11 204803
12 12 318932
I would inted like to take into account the year, for example November 2019, December 2019 and so on.
We could extract the year, month as grouping variable and get the sum
library(dplyr)
library(lubridate)
n_occur %>%
group_by(x = format(ymd(x), '%B %Y')) %>%
summarise(Freq = sum(Freq))
Or convert to yearmon class from zoo and get the sum of 'Freq' by 'x'
library(zoo)
n_occur %>%
group_by(x = as.yearmon(x)) %>%
summarise(Freq = sum(Freq))
# A tibble: 34 × 2
x Freq
<yearmon> <int>
1 Nov 2019 1733
2 Dec 2019 137050
3 Jan 2020 114323
4 Feb 2020 115832
5 Mar 2020 51688
6 Apr 2020 17321
7 May 2020 96916
8 Jun 2020 76297
9 Jul 2020 85132
10 Aug 2020 120480
# … with 24 more rows
Related
I have a data frame and am trying to fill the missing values with the previous and next values in the data frame. I used the following code, but it did not fill and returns missing values still. I tried 2 different variations, but both do not work. Could someone please let me know what I am doing wrong? Thanks!
Data frame: 'oil.csv'
date dcoilwtico
1/1/13
1/2/13 93.14
1/3/13 92.97
1/4/13 93.12
1/7/13 93.2
1/8/13 93.21
1/9/13 93.08
1/10/13 93.81
1/11/13 93.6
1/14/13 94.27
1/15/13 93.26
1/16/13 94.28
1/17/13 95.49
1/18/13 95.61
1/21/13
1/22/13 96.09
dput(oil_df):
structure(list(date = c("2013-01-01", "2013-01-02", "2013-01-03",
"2013-01-04", "2013-01-07", "2013-01-08", "2013-01-09", "2013-01-10",
"2013-01-11", "2013-01-14", "2013-01-15", "2013-01-16", "2013-01-17",
"2013-01-18", "2013-01-21", "2013-01-22", "2013-01-23", "2013-01-24",
"2013-01-25", "2013-01-28", "2013-01-29", "2013-01-30", "2013-01-31",
"2013-02-01", "2013-02-04", "2013-02-05", "2013-02-06", "2013-02-07",
"2013-02-08", "2013-02-11", "2013-02-12", "2013-02-13", "2013-02-14",
"2013-02-15", "2013-02-18", "2013-02-19", "2013-02-20", "2013-02-21",
"2013-02-22", "2013-02-25", "2013-02-26", "2013-02-27", "2013-02-28",
"2013-03-01", "2013-03-04", "2013-03-05", "2013-03-06", "2013-03-07",
"2013-03-08", "2013-03-11", "2013-03-12", "2013-03-13", "2013-03-14",
"2013-03-15", "2013-03-18", "2013-03-19", "2013-03-20", "2013-03-21",
"2013-03-22", "2013-03-25", "2013-03-26", "2013-03-27", "2013-03-28",
"2013-03-29", "2013-04-01", "2013-04-02", "2013-04-03", "2013-04-04",
"2013-04-05", "2013-04-08", "2013-04-09", "2013-04-10", "2013-04-11",
"2013-04-12", "2013-04-15", "2013-04-16", "2013-04-17", "2013-04-18",
"2013-04-19", "2013-04-22", "2013-04-23", "2013-04-24", "2013-04-25",
"2013-04-26", "2013-04-29", "2013-04-30", "2013-05-01", "2013-05-02",
"2013-05-03", "2013-05-06", "2013-05-07", "2013-05-08", "2013-05-09",
"2013-05-10", "2013-05-13", "2013-05-14", "2013-05-15", "2013-05-16",
"2013-05-17", "2013-05-20", "2013-05-21", "2013-05-22", "2013-05-23",
"2013-05-24", "2013-05-27", "2013-05-28", "2013-05-29", "2013-05-30",
"2013-05-31", "2013-06-03", "2013-06-04", "2013-06-05", "2013-06-06",
"2013-06-07", "2013-06-10", "2013-06-11", "2013-06-12", "2013-06-13",
"2013-06-14", "2013-06-17", "2013-06-18", "2013-06-19", "2013-06-20",
"2013-06-21", "2013-06-24", "2013-06-25", "2013-06-26", "2013-06-27",
"2013-06-28", "2013-07-01", "2013-07-02", "2013-07-03", "2013-07-04",
"2013-07-05", "2013-07-08", "2013-07-09", "2013-07-10", "2013-07-11",
"2013-07-12", "2013-07-15", "2013-07-16", "2013-07-17", "2013-07-18",
"2013-07-19", "2013-07-22", "2013-07-23", "2013-07-24", "2013-07-25",
"2013-07-26", "2013-07-29", "2013-07-30", "2013-07-31", "2013-08-01",
"2013-08-02", "2013-08-05", "2013-08-06", "2013-08-07", "2013-08-08",
"2013-08-09", "2013-08-12", "2013-08-13", "2013-08-14", "2013-08-15",
"2013-08-16", "2013-08-19", "2013-08-20", "2013-08-21", "2013-08-22",
"2013-08-23", "2013-08-26", "2013-08-27", "2013-08-28", "2013-08-29",
"2013-08-30", "2013-09-02", "2013-09-03", "2013-09-04", "2013-09-05",
"2013-09-06", "2013-09-09", "2013-09-10", "2013-09-11", "2013-09-12",
"2013-09-13", "2013-09-16", "2013-09-17", "2013-09-18", "2013-09-19",
"2013-09-20", "2013-09-23", "2013-09-24", "2013-09-25", "2013-09-26",
"2013-09-27", "2013-09-30", "2013-10-01", "2013-10-02", "2013-10-03",
"2013-10-04", "2013-10-07", "2013-10-08", "2013-10-09", "2013-10-10",
"2013-10-11", "2013-10-14", "2013-10-15", "2013-10-16", "2013-10-17",
"2013-10-18", "2013-10-21", "2013-10-22", "2013-10-23", "2013-10-24",
"2013-10-25", "2013-10-28", "2013-10-29", "2013-10-30", "2013-10-31",
"2013-11-01", "2013-11-04", "2013-11-05", "2013-11-06", "2013-11-07",
"2013-11-08", "2013-11-11", "2013-11-12", "2013-11-13", "2013-11-14",
"2013-11-15", "2013-11-18", "2013-11-19", "2013-11-20", "2013-11-21",
"2013-11-22", "2013-11-25", "2013-11-26", "2013-11-27", "2013-11-28",
"2013-11-29", "2013-12-02", "2013-12-03", "2013-12-04", "2013-12-05",
"2013-12-06", "2013-12-09", "2013-12-10", "2013-12-11", "2013-12-12",
"2013-12-13", "2013-12-16", "2013-12-17", "2013-12-18", "2013-12-19",
"2013-12-20", "2013-12-23", "2013-12-24", "2013-12-25", "2013-12-26",
"2013-12-27", "2013-12-30", "2013-12-31", "2014-01-01", "2014-01-02",
"2014-01-03", "2014-01-06", "2014-01-07", "2014-01-08", "2014-01-09",
"2014-01-10", "2014-01-13", "2014-01-14", "2014-01-15", "2014-01-16",
"2014-01-17", "2014-01-20", "2014-01-21", "2014-01-22", "2014-01-23",
"2014-01-24", "2014-01-27", "2014-01-28", "2014-01-29", "2014-01-30",
"2014-01-31", "2014-02-03", "2014-02-04", "2014-02-05", "2014-02-06",
"2014-02-07", "2014-02-10", "2014-02-11", "2014-02-12", "2014-02-13",
"2014-02-14", "2014-02-17", "2014-02-18", "2014-02-19", "2014-02-20",
"2014-02-21", "2014-02-24", "2014-02-25", "2014-02-26", "2014-02-27",
"2014-02-28", "2014-03-03", "2014-03-04", "2014-03-05", "2014-03-06",
"2014-03-07", "2014-03-10", "2014-03-11", "2014-03-12", "2014-03-13",
"2014-03-14", "2014-03-17", "2014-03-18", "2014-03-19", "2014-03-20",
"2014-03-21", "2014-03-24", "2014-03-25", "2014-03-26", "2014-03-27",
"2014-03-28", "2014-03-31", "2014-04-01", "2014-04-02", "2014-04-03",
"2014-04-04", "2014-04-07", "2014-04-08", "2014-04-09", "2014-04-10",
"2014-04-11", "2014-04-14", "2014-04-15", "2014-04-16", "2014-04-17",
"2014-04-18", "2014-04-21", "2014-04-22", "2014-04-23", "2014-04-24",
"2014-04-25", "2014-04-28", "2014-04-29", "2014-04-30", "2014-05-01",
"2014-05-02", "2014-05-05", "2014-05-06", "2014-05-07", "2014-05-08",
"2014-05-09", "2014-05-12", "2014-05-13", "2014-05-14", "2014-05-15",
"2014-05-16", "2014-05-19", "2014-05-20", "2014-05-21", "2014-05-22",
"2014-05-23", "2014-05-26", "2014-05-27", "2014-05-28", "2014-05-29",
"2014-05-30", "2014-06-02", "2014-06-03", "2014-06-04", "2014-06-05",
"2014-06-06", "2014-06-09", "2014-06-10", "2014-06-11", "2014-06-12",
"2014-06-13", "2014-06-16", "2014-06-17", "2014-06-18", "2014-06-19",
"2014-06-20", "2014-06-23", "2014-06-24", "2014-06-25", "2014-06-26",
"2014-06-27", "2014-06-30", "2014-07-01", "2014-07-02", "2014-07-03",
"2014-07-04", "2014-07-07", "2014-07-08", "2014-07-09", "2014-07-10",
"2014-07-11", "2014-07-14", "2014-07-15", "2014-07-16", "2014-07-17",
"2014-07-18", "2014-07-21", "2014-07-22", "2014-07-23", "2014-07-24",
"2014-07-25", "2014-07-28", "2014-07-29", "2014-07-30", "2014-07-31",
"2014-08-01", "2014-08-04", "2014-08-05", "2014-08-06", "2014-08-07",
"2014-08-08", "2014-08-11", "2014-08-12", "2014-08-13", "2014-08-14",
"2014-08-15", "2014-08-18", "2014-08-19", "2014-08-20", "2014-08-21",
"2014-08-22", "2014-08-25", "2014-08-26", "2014-08-27", "2014-08-28",
"2014-08-29", "2014-09-01", "2014-09-02", "2014-09-03", "2014-09-04",
"2014-09-05", "2014-09-08", "2014-09-09", "2014-09-10", "2014-09-11",
"2014-09-12", "2014-09-15", "2014-09-16", "2014-09-17", "2014-09-18",
"2014-09-19", "2014-09-22", "2014-09-23", "2014-09-24", "2014-09-25",
"2014-09-26", "2014-09-29", "2014-09-30", "2014-10-01", "2014-10-02",
"2014-10-03", "2014-10-06", "2014-10-07", "2014-10-08", "2014-10-09",
"2014-10-10", "2014-10-13", "2014-10-14", "2014-10-15", "2014-10-16",
"2014-10-17", "2014-10-20", "2014-10-21", "2014-10-22", "2014-10-23",
"2014-10-24", "2014-10-27", "2014-10-28", "2014-10-29", "2014-10-30",
"2014-10-31", "2014-11-03", "2014-11-04", "2014-11-05", "2014-11-06",
"2014-11-07", "2014-11-10", "2014-11-11", "2014-11-12", "2014-11-13",
"2014-11-14", "2014-11-17", "2014-11-18", "2014-11-19", "2014-11-20",
"2014-11-21", "2014-11-24", "2014-11-25", "2014-11-26", "2014-11-27",
"2014-11-28", "2014-12-01", "2014-12-02", "2014-12-03", "2014-12-04",
"2014-12-05", "2014-12-08", "2014-12-09", "2014-12-10", "2014-12-11",
"2014-12-12", "2014-12-15", "2014-12-16", "2014-12-17", "2014-12-18",
"2014-12-19", "2014-12-22", "2014-12-23", "2014-12-24", "2014-12-25",
"2014-12-26", "2014-12-29", "2014-12-30", "2014-12-31", "2015-01-01",
"2015-01-02", "2015-01-05", "2015-01-06", "2015-01-07", "2015-01-08",
"2015-01-09", "2015-01-12", "2015-01-13", "2015-01-14", "2015-01-15",
"2015-01-16", "2015-01-19", "2015-01-20", "2015-01-21", "2015-01-22",
"2015-01-23", "2015-01-26", "2015-01-27", "2015-01-28", "2015-01-29",
"2015-01-30", "2015-02-02", "2015-02-03", "2015-02-04", "2015-02-05",
"2015-02-06", "2015-02-09", "2015-02-10", "2015-02-11", "2015-02-12",
"2015-02-13", "2015-02-16", "2015-02-17", "2015-02-18", "2015-02-19",
"2015-02-20", "2015-02-23", "2015-02-24", "2015-02-25", "2015-02-26",
"2015-02-27", "2015-03-02", "2015-03-03", "2015-03-04", "2015-03-05",
"2015-03-06", "2015-03-09", "2015-03-10", "2015-03-11", "2015-03-12",
"2015-03-13", "2015-03-16", "2015-03-17", "2015-03-18", "2015-03-19",
"2015-03-20", "2015-03-23", "2015-03-24", "2015-03-25", "2015-03-26",
"2015-03-27", "2015-03-30", "2015-03-31", "2015-04-01", "2015-04-02",
"2015-04-03", "2015-04-06", "2015-04-07", "2015-04-08", "2015-04-09",
"2015-04-10", "2015-04-13", "2015-04-14", "2015-04-15", "2015-04-16",
"2015-04-17", "2015-04-20", "2015-04-21", "2015-04-22", "2015-04-23",
"2015-04-24", "2015-04-27", "2015-04-28", "2015-04-29", "2015-04-30",
"2015-05-01", "2015-05-04", "2015-05-05", "2015-05-06", "2015-05-07",
"2015-05-08", "2015-05-11", "2015-05-12", "2015-05-13", "2015-05-14",
"2015-05-15", "2015-05-18", "2015-05-19", "2015-05-20", "2015-05-21",
"2015-05-22", "2015-05-25", "2015-05-26", "2015-05-27", "2015-05-28",
"2015-05-29", "2015-06-01", "2015-06-02", "2015-06-03", "2015-06-04",
"2015-06-05", "2015-06-08", "2015-06-09", "2015-06-10", "2015-06-11",
"2015-06-12", "2015-06-15", "2015-06-16", "2015-06-17", "2015-06-18",
"2015-06-19", "2015-06-22", "2015-06-23", "2015-06-24", "2015-06-25",
"2015-06-26", "2015-06-29", "2015-06-30", "2015-07-01", "2015-07-02",
"2015-07-03", "2015-07-06", "2015-07-07", "2015-07-08", "2015-07-09",
"2015-07-10", "2015-07-13", "2015-07-14", "2015-07-15", "2015-07-16",
"2015-07-17", "2015-07-20", "2015-07-21", "2015-07-22", "2015-07-23",
"2015-07-24", "2015-07-27", "2015-07-28", "2015-07-29", "2015-07-30",
"2015-07-31", "2015-08-03", "2015-08-04", "2015-08-05", "2015-08-06",
"2015-08-07", "2015-08-10", "2015-08-11", "2015-08-12", "2015-08-13",
"2015-08-14", "2015-08-17", "2015-08-18", "2015-08-19", "2015-08-20",
"2015-08-21", "2015-08-24", "2015-08-25", "2015-08-26", "2015-08-27",
"2015-08-28", "2015-08-31", "2015-09-01", "2015-09-02", "2015-09-03",
"2015-09-04", "2015-09-07", "2015-09-08", "2015-09-09", "2015-09-10",
"2015-09-11", "2015-09-14", "2015-09-15", "2015-09-16", "2015-09-17",
"2015-09-18", "2015-09-21", "2015-09-22", "2015-09-23", "2015-09-24",
"2015-09-25", "2015-09-28", "2015-09-29", "2015-09-30", "2015-10-01",
"2015-10-02", "2015-10-05", "2015-10-06", "2015-10-07", "2015-10-08",
"2015-10-09", "2015-10-12", "2015-10-13", "2015-10-14", "2015-10-15",
"2015-10-16", "2015-10-19", "2015-10-20", "2015-10-21", "2015-10-22",
"2015-10-23", "2015-10-26", "2015-10-27", "2015-10-28", "2015-10-29",
"2015-10-30", "2015-11-02", "2015-11-03", "2015-11-04", "2015-11-05",
"2015-11-06", "2015-11-09", "2015-11-10", "2015-11-11", "2015-11-12",
"2015-11-13", "2015-11-16", "2015-11-17", "2015-11-18", "2015-11-19",
"2015-11-20", "2015-11-23", "2015-11-24", "2015-11-25", "2015-11-26",
"2015-11-27", "2015-11-30", "2015-12-01", "2015-12-02", "2015-12-03",
"2015-12-04", "2015-12-07", "2015-12-08", "2015-12-09", "2015-12-10",
"2015-12-11", "2015-12-14", "2015-12-15", "2015-12-16", "2015-12-17",
"2015-12-18", "2015-12-21", "2015-12-22", "2015-12-23", "2015-12-24",
"2015-12-25", "2015-12-28", "2015-12-29", "2015-12-30", "2015-12-31",
"2016-01-01", "2016-01-04", "2016-01-05", "2016-01-06", "2016-01-07",
"2016-01-08", "2016-01-11", "2016-01-12", "2016-01-13", "2016-01-14",
"2016-01-15", "2016-01-18", "2016-01-19", "2016-01-20", "2016-01-21",
"2016-01-22", "2016-01-25", "2016-01-26", "2016-01-27", "2016-01-28",
"2016-01-29", "2016-02-01", "2016-02-02", "2016-02-03", "2016-02-04",
"2016-02-05", "2016-02-08", "2016-02-09", "2016-02-10", "2016-02-11",
"2016-02-12", "2016-02-15", "2016-02-16", "2016-02-17", "2016-02-18",
"2016-02-19", "2016-02-22", "2016-02-23", "2016-02-24", "2016-02-25",
"2016-02-26", "2016-02-29", "2016-03-01", "2016-03-02", "2016-03-03",
"2016-03-04", "2016-03-07", "2016-03-08", "2016-03-09", "2016-03-10",
"2016-03-11", "2016-03-14", "2016-03-15", "2016-03-16", "2016-03-17",
"2016-03-18", "2016-03-21", "2016-03-22", "2016-03-23", "2016-03-24",
"2016-03-25", "2016-03-28", "2016-03-29", "2016-03-30", "2016-03-31",
"2016-04-01", "2016-04-04", "2016-04-05", "2016-04-06", "2016-04-07",
"2016-04-08", "2016-04-11", "2016-04-12", "2016-04-13", "2016-04-14",
"2016-04-15", "2016-04-18", "2016-04-19", "2016-04-20", "2016-04-21",
"2016-04-22", "2016-04-25", "2016-04-26", "2016-04-27", "2016-04-28",
"2016-04-29", "2016-05-02", "2016-05-03", "2016-05-04", "2016-05-05",
"2016-05-06", "2016-05-09", "2016-05-10", "2016-05-11", "2016-05-12",
"2016-05-13", "2016-05-16", "2016-05-17", "2016-05-18", "2016-05-19",
"2016-05-20", "2016-05-23", "2016-05-24", "2016-05-25", "2016-05-26",
"2016-05-27", "2016-05-30", "2016-05-31", "2016-06-01", "2016-06-02",
"2016-06-03", "2016-06-06", "2016-06-07", "2016-06-08", "2016-06-09",
"2016-06-10", "2016-06-13", "2016-06-14", "2016-06-15", "2016-06-16",
"2016-06-17", "2016-06-20", "2016-06-21", "2016-06-22", "2016-06-23",
"2016-06-24", "2016-06-27", "2016-06-28", "2016-06-29", "2016-06-30",
"2016-07-01", "2016-07-04", "2016-07-05", "2016-07-06", "2016-07-07",
"2016-07-08", "2016-07-11", "2016-07-12", "2016-07-13", "2016-07-14",
"2016-07-15", "2016-07-18", "2016-07-19", "2016-07-20", "2016-07-21",
"2016-07-22", "2016-07-25", "2016-07-26", "2016-07-27", "2016-07-28",
"2016-07-29", "2016-08-01", "2016-08-02", "2016-08-03", "2016-08-04",
"2016-08-05", "2016-08-08", "2016-08-09", "2016-08-10", "2016-08-11",
"2016-08-12", "2016-08-15", "2016-08-16", "2016-08-17", "2016-08-18",
"2016-08-19", "2016-08-22", "2016-08-23", "2016-08-24", "2016-08-25",
"2016-08-26", "2016-08-29", "2016-08-30", "2016-08-31", "2016-09-01",
"2016-09-02", "2016-09-05", "2016-09-06", "2016-09-07", "2016-09-08",
"2016-09-09", "2016-09-12", "2016-09-13", "2016-09-14", "2016-09-15",
"2016-09-16", "2016-09-19", "2016-09-20", "2016-09-21", "2016-09-22",
"2016-09-23", "2016-09-26", "2016-09-27", "2016-09-28", "2016-09-29",
"2016-09-30", "2016-10-03", "2016-10-04", "2016-10-05", "2016-10-06",
"2016-10-07", "2016-10-10", "2016-10-11", "2016-10-12", "2016-10-13",
"2016-10-14", "2016-10-17", "2016-10-18", "2016-10-19", "2016-10-20",
"2016-10-21", "2016-10-24", "2016-10-25", "2016-10-26", "2016-10-27",
"2016-10-28", "2016-10-31", "2016-11-01", "2016-11-02", "2016-11-03",
"2016-11-04", "2016-11-07", "2016-11-08", "2016-11-09", "2016-11-10",
"2016-11-11", "2016-11-14", "2016-11-15", "2016-11-16", "2016-11-17",
"2016-11-18", "2016-11-21", "2016-11-22", "2016-11-23", "2016-11-24",
"2016-11-25", "2016-11-28", "2016-11-29", "2016-11-30", "2016-12-01",
"2016-12-02", "2016-12-05", "2016-12-06", "2016-12-07", "2016-12-08",
"2016-12-09", "2016-12-12", "2016-12-13", "2016-12-14", "2016-12-15",
"2016-12-16", "2016-12-19", "2016-12-20", "2016-12-21", "2016-12-22",
"2016-12-23", "2016-12-26", "2016-12-27", "2016-12-28", "2016-12-29",
"2016-12-30", "2017-01-02", "2017-01-03", "2017-01-04", "2017-01-05",
"2017-01-06", "2017-01-09", "2017-01-10", "2017-01-11", "2017-01-12",
"2017-01-13", "2017-01-16", "2017-01-17", "2017-01-18", "2017-01-19",
"2017-01-20", "2017-01-23", "2017-01-24", "2017-01-25", "2017-01-26",
"2017-01-27", "2017-01-30", "2017-01-31", "2017-02-01", "2017-02-02",
"2017-02-03", "2017-02-06", "2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23",
"2017-02-24", "2017-02-27", "2017-02-28", "2017-03-01", "2017-03-02",
"2017-03-03", "2017-03-06", "2017-03-07", "2017-03-08", "2017-03-09",
"2017-03-10", "2017-03-13", "2017-03-14", "2017-03-15", "2017-03-16",
"2017-03-17", "2017-03-20", "2017-03-21", "2017-03-22", "2017-03-23",
"2017-03-24", "2017-03-27", "2017-03-28", "2017-03-29", "2017-03-30",
"2017-03-31", "2017-04-03", "2017-04-04", "2017-04-05", "2017-04-06",
"2017-04-07", "2017-04-10", "2017-04-11", "2017-04-12", "2017-04-13",
"2017-04-14", "2017-04-17", "2017-04-18", "2017-04-19", "2017-04-20",
"2017-04-21", "2017-04-24", "2017-04-25", "2017-04-26", "2017-04-27",
"2017-04-28", "2017-05-01", "2017-05-02", "2017-05-03", "2017-05-04",
"2017-05-05", "2017-05-08", "2017-05-09", "2017-05-10", "2017-05-11",
"2017-05-12", "2017-05-15", "2017-05-16", "2017-05-17", "2017-05-18",
"2017-05-19", "2017-05-22", "2017-05-23", "2017-05-24", "2017-05-25",
"2017-05-26", "2017-05-29", "2017-05-30", "2017-05-31", "2017-06-01",
"2017-06-02", "2017-06-05", "2017-06-06", "2017-06-07", "2017-06-08",
"2017-06-09", "2017-06-12", "2017-06-13", "2017-06-14", "2017-06-15",
"2017-06-16", "2017-06-19", "2017-06-20", "2017-06-21", "2017-06-22",
"2017-06-23", "2017-06-26", "2017-06-27", "2017-06-28", "2017-06-29",
"2017-06-30", "2017-07-03", "2017-07-04", "2017-07-05", "2017-07-06",
"2017-07-07", "2017-07-10", "2017-07-11", "2017-07-12", "2017-07-13",
"2017-07-14", "2017-07-17", "2017-07-18", "2017-07-19", "2017-07-20",
"2017-07-21", "2017-07-24", "2017-07-25", "2017-07-26", "2017-07-27",
"2017-07-28", "2017-07-31", "2017-08-01", "2017-08-02", "2017-08-03",
"2017-08-04", "2017-08-07", "2017-08-08", "2017-08-09", "2017-08-10",
"2017-08-11", "2017-08-14", "2017-08-15", "2017-08-16", "2017-08-17",
"2017-08-18", "2017-08-21", "2017-08-22", "2017-08-23", "2017-08-24",
"2017-08-25", "2017-08-28", "2017-08-29", "2017-08-30", "2017-08-31"
), dcoilwtico = c(NA, 93.14, 92.97, 93.12, 93.2, 93.21, 93.08,
93.81, 93.6, 94.27, 93.26, 94.28, 95.49, 95.61, NA, 96.09, 95.06,
95.35, 95.15, 95.95, 97.62, 97.98, 97.65, 97.46, 96.21, 96.68,
96.44, 95.84, 95.71, 97.01, 97.48, 97.03, 97.3, 95.95, NA, 96.69,
94.92, 92.79, 93.12, 92.74, 92.63, 92.84, 92.03, 90.71, 90.13,
90.88, 90.47, 91.53, 92.01, 92.07, 92.44, 92.47, 93.03, 93.49,
93.71, 92.44, 93.21, 92.46, 93.41, 94.55, 95.99, 96.53, 97.24,
NA, 97.1, 97.23, 95.02, 93.26, 92.76, 93.36, 94.18, 94.59, 93.44,
91.23, 88.75, 88.73, 86.65, 87.83, 88.04, 88.81, 89.21, 91.07,
93.27, 92.63, 94.09, 93.22, 90.74, 93.7, 95.25, 95.8, 95.28,
96.24, 96.09, 95.81, 94.76, 93.96, 93.95, 94.85, 95.72, 96.29,
95.55, 93.98, 94.12, 93.84, NA, 94.65, 93.13, 93.57, 91.93, 93.41,
93.36, 93.66, 94.71, 96.11, 95.82, 95.5, 95.98, 96.66, 97.83,
97.86, 98.46, 98.24, 94.89, 93.81, 95.07, 95.25, 95.47, 97, 96.36,
97.94, 99.65, 101.92, NA, 103.09, 103.03, 103.46, 106.41, 104.77,
105.85, 106.2, 105.88, 106.39, 107.94, 108, 106.61, 107.13, 105.41,
105.47, 104.76, 104.61, 103.14, 105.1, 107.93, 106.94, 106.61,
105.32, 104.41, 103.45, 106.04, 106.19, 106.78, 106.89, 107.43,
107.58, 107.14, 104.9, 103.93, 104.93, 106.48, 105.88, 109.11,
110.17, 108.51, 107.98, NA, 108.67, 107.29, 108.5, 110.62, 109.62,
107.48, 107.65, 108.72, 108.31, 106.54, 105.36, 108.23, 106.26,
104.7, 103.62, 103.22, 102.68, 103.1, 102.86, 102.36, 102.09,
104.15, 103.29, 103.83, 103.07, 103.54, 101.63, 103.08, 102.17,
102.46, 101.15, 102.34, 100.72, 100.87, 99.28, 97.63, 96.9, 96.65,
97.4, 98.74, 98.29, 96.81, 96.29, 94.56, 94.58, 93.4, 94.74,
94.25, 94.56, 95.13, 93.12, 93.91, 93.76, 93.8, 93.03, 93.35,
93.34, 95.35, 94.53, 93.86, 93.41, 92.05, NA, 92.55, 93.61, 95.83,
96.97, 97.14, 97.48, 97.1, 98.32, 97.25, 97.21, 96.27, 97.18,
96.99, 97.59, 98.4, 99.11, 98.62, 98.87, NA, 99.18, 99.94, 98.9,
98.17, NA, 95.14, 93.66, 93.12, 93.31, 91.9, 91.36, 92.39, 91.45,
92.15, 93.78, 93.54, 93.96, NA, 94.51, 96.35, 97.23, 96.66, 95.82,
97.49, 97.34, 98.25, 97.55, 96.44, 97.24, 97.4, 97.84, 99.98,
100.12, 99.96, 100.38, 100.27, 100.31, NA, 102.54, 103.46, 103.2,
102.53, 103.17, 102.2, 102.93, 102.68, 102.88, 105.34, 103.64,
101.75, 101.82, 102.82, 101.39, 100.29, 98.29, 98.57, 99.23,
98.43, 100.08, 100.71, 99.68, 99.97, 100.05, 99.66, 100.61, 101.25,
101.73, 101.57, 99.69, 99.6, 100.29, 101.16, 100.43, 102.57,
103.55, 103.37, 103.68, 104.05, 103.7, 103.71, 104.33, NA, 104.35,
101.69, 101.47, 102.2, 100.85, 101.13, 101.56, 100.07, 99.69,
100.09, 99.74, 99.81, 101.06, 100.52, 100.32, 100.89, 102.01,
102.63, 101.74, 102.31, 102.95, 102.8, 104.31, 104.03, 105.01,
NA, 104.78, 103.37, 104.26, 103.4, 103.07, 103.34, 103.27, 103.17,
103.32, 105.09, 105.02, 105.04, 107.2, 107.49, 107.52, 106.95,
106.64, 107.08, 107.95, 106.83, 106.64, 107.04, 106.49, 106.46,
106.07, 106.06, 105.18, 104.76, NA, 104.19, 104.06, 102.93, 103.61,
101.48, 101.73, 100.56, 101.88, 103.84, 103.83, 105.34, 104.59,
103.81, 102.76, 105.23, 105.68, 104.91, 104.29, 98.23, 97.86,
98.26, 97.34, 96.93, 97.34, 97.61, 98.09, 97.36, 97.57, 95.54,
97.3, 96.44, 94.35, 96.4, 93.97, 93.61, 95.39, 95.78, 95.82,
96.44, 97.86, NA, 92.92, 95.5, 94.51, 93.32, 92.64, 92.73, 91.71,
92.89, 92.18, 92.86, 94.91, 94.33, 93.07, 92.43, 91.46, 91.55,
93.6, 93.59, 95.55, 94.53, 91.17, 90.74, 91.02, 89.76, 90.33,
88.89, 87.29, 85.76, 85.87, 85.73, 81.72, 81.82, 82.33, 82.8,
82.76, 83.25, 80.52, 82.81, 81.27, 81.26, 81.36, 82.25, 81.06,
80.53, 78.77, 77.15, 78.71, 77.87, 78.71, 77.43, 77.85, 77.16,
74.13, 75.91, 75.64, 74.55, 74.55, 75.63, 76.52, 75.74, 74.04,
73.7, NA, 65.94, 68.98, 66.99, 67.3, 66.73, 65.89, 63.13, 63.74,
60.99, 60.01, 57.81, 55.96, 55.97, 56.43, 54.18, 56.91, 55.25,
56.78, 55.7, NA, 54.59, 53.46, 54.14, 53.45, NA, 52.72, 50.05,
47.98, 48.69, 48.8, 48.35, 46.06, 45.92, 48.49, 46.37, 48.49,
NA, 46.79, 47.85, 45.93, 45.26, 44.8, 45.84, 44.08, 44.12, 47.79,
49.25, 53.04, 48.45, 50.48, 51.66, 52.99, 50.06, 48.8, 51.17,
52.66, NA, 53.56, 52.13, 51.12, 49.95, 49.56, 48.48, 50.25, 47.65,
49.84, 49.59, 50.43, 51.53, 50.76, 49.61, 49.95, 48.42, 48.06,
47.12, 44.88, 43.93, 43.39, 44.63, 44.02, 46, 47.4, 47.03, 48.75,
51.41, 48.83, 48.66, 47.72, 50.12, 49.13, NA, 52.08, 53.95, 50.44,
50.79, 51.63, 51.95, 53.3, 56.25, 56.69, 55.71, 56.37, 55.58,
56.17, 56.59, 55.98, 55.56, 57.05, 58.55, 59.62, 59.1, 58.92,
60.38, 60.93, 58.99, 59.41, 59.23, 60.72, 60.5, 59.89, 59.73,
59.44, 57.3, 58.96, 60.18, 58.88, NA, 57.29, 57.51, 57.69, 60.25,
60.24, 61.3, 59.67, 58, 59.11, 58.15, 60.15, 61.36, 60.74, 59.96,
59.53, 60.01, 59.89, 60.41, 59.62, 60.01, 61.05, 60.01, 59.59,
59.41, 58.34, 59.48, 56.94, 56.93, NA, 52.48, 52.33, 51.61, 52.76,
52.74, 52.19, 53.05, 51.4, 50.9, 50.88, 50.11, 50.59, 49.27,
48.11, 47.98, 47.17, 47.97, 48.77, 48.53, 47.11, 45.25, 45.75,
45.13, 44.69, 43.87, 44.94, 43.11, 43.22, 42.27, 42.45, 41.93,
42.58, 40.75, 41, 40.45, 38.22, 39.15, 38.5, 42.47, 45.29, 49.2,
45.38, 46.3, 46.75, 46.02, NA, 45.92, 44.13, 45.85, 44.75, 44.07,
44.58, 47.12, 46.93, 44.71, 46.67, 46.17, 44.53, 44.94, 45.55,
44.4, 45.24, 45.06, 44.75, 45.54, 46.28, 48.53, 47.86, 49.46,
49.67, 47.09, 46.7, 46.63, 46.38, 47.3, 45.91, 45.84, 45.22,
44.9, 43.91, 43.19, 43.21, 45.93, 46.02, 46.6, 46.12, 47.88,
46.32, 45.27, 44.32, 43.87, 44.23, 42.95, 41.74, 40.69, 41.68,
40.73, 40.75, 40.55, 39.39, 39.27, 40.89, 41.22, NA, 40.57, 40.43,
40.58, 39.93, 41.08, 40, 37.64, 37.46, 37.16, 36.76, 35.65, 36.31,
37.32, 35.55, 34.98, 34.72, 34.55, 36.12, 36.76, 37.62, NA, 36.36,
37.88, 36.59, 37.13, NA, 36.81, 35.97, 33.97, 33.29, 33.2, 31.42,
30.42, 30.42, 31.22, 29.45, NA, 28.47, 26.68, 29.55, 32.07, 30.31,
29.54, 32.32, 33.21, 33.66, 31.62, 29.9, 32.29, 31.63, 30.86,
29.71, 27.96, 27.54, 26.19, 29.32, NA, 29.05, 30.68, 30.77, 29.59,
31.37, 31.84, 30.35, 31.4, 31.65, 32.74, 34.39, 34.57, 34.56,
35.91, 37.9, 36.67, 37.62, 37.77, 38.51, 37.2, 36.32, 38.43,
40.17, 39.47, 39.91, 41.45, 38.28, 38.14, NA, 37.99, 36.91, 36.91,
36.94, 35.36, 34.3, 34.52, 37.74, 37.3, 39.74, 40.46, 42.12,
41.7, 41.45, 40.4, 39.74, 40.88, 42.72, 43.18, 42.76, 41.67,
42.52, 45.29, 46.03, 45.98, 44.75, 43.65, 43.77, 44.33, 44.58,
43.45, 44.68, 46.21, 46.64, 46.22, 47.72, 48.29, 48.12, 48.16,
47.67, 48.12, 48.04, 49.1, 49, 49.36, NA, 49.1, 49.07, 49.14,
48.69, 49.71, 50.37, 51.23, 50.52, 49.09, 48.89, 48.49, 47.92,
46.14, 48, 49.4, 48.95, 49.16, 49.34, 46.7, 45.8, 47.93, 49.85,
48.27, 49.02, NA, 46.73, 47.37, 45.22, 45.37, 44.73, 46.82, 44.87,
45.64, 45.93, 45.23, 44.64, 44.96, 43.96, 43.41, 42.4, 42.16,
41.9, 41.13, 41.54, 40.05, 39.5, 40.8, 41.92, 41.83, 43.06, 42.78,
41.75, 43.51, 44.47, 45.72, 46.57, 46.81, 48.2, 48.48, 46.8,
47.54, 46.29, 46.97, 47.64, 46.97, 46.32, 44.68, 43.17, 44.39,
NA, 44.85, 45.47, 47.63, 45.88, 46.28, 44.91, 43.62, 43.85, 43.04,
43.34, 43.85, 45.33, 46.1, 44.36, 45.6, 44.65, 47.07, 47.72,
47.72, 48.8, 48.67, 49.75, 50.44, 49.76, 49.76, 50.72, 50.14,
50.47, 50.35, 49.97, 50.3, 51.59, 50.31, 50.61, 50.18, 49.45,
48.75, 49.71, 48.72, 46.83, 46.66, 45.32, 44.66, 44.07, 44.88,
44.96, 45.2, 44.62, 43.39, 43.29, 45.86, 45.56, 45.37, 45.69,
47.48, 48.07, 46.72, NA, 46.72, 45.66, 45.29, 49.41, 51.08, 51.7,
51.72, 50.95, 49.85, 50.84, 51.51, 52.74, 52.99, 51.01, 50.9,
51.93, 52.13, 52.22, 51.44, 51.98, 52.01, NA, 52.82, 54.01, 53.8,
53.75, NA, 52.36, 53.26, 53.77, 53.98, 51.95, 50.82, 52.19, 53.01,
52.36, NA, 52.45, 51.12, 51.39, 52.33, 52.77, 52.38, 52.14, 53.24,
53.18, 52.63, 52.75, 53.9, 53.55, 53.81, 53.01, 52.19, 52.37,
52.99, 53.84, 52.96, 53.21, 53.11, 53.41, 53.41, NA, 54.02, 53.61,
54.48, 53.99, 54.04, 54, 53.82, 52.63, 53.33, 53.19, 52.68, 49.83,
48.75, 48.05, 47.95, 47.24, 48.34, 48.3, 48.34, 47.79, 47.02,
47.29, 47, 47.3, 47.02, 48.36, 49.47, 50.3, 50.54, 50.25, 50.99,
51.14, 51.69, 52.25, 53.06, 53.38, 53.12, 53.19, NA, 52.62, 52.46,
50.49, 50.26, 49.64, 48.9, 49.22, 49.22, 48.96, 49.31, 48.83,
47.65, 47.79, 45.55, 46.23, 46.46, 45.84, 47.28, 47.81, 47.83,
48.86, 48.64, 49.04, 49.36, 50.32, 50.81, 51.12, 50.99, 48.57,
49.58, NA, 49.63, 48.29, 48.32, 47.68, 47.4, 48.13, 45.8, 45.68,
45.82, 46.1, 46.41, 44.79, 44.47, 44.73, 44.24, 43.34, 42.48,
42.53, 42.86, 43.24, 44.25, 44.74, 44.88, 46.02, NA, NA, 45.11,
45.52, 44.25, 44.4, 45.06, 45.48, 46.06, 46.53, 46.02, 46.4,
47.1, 46.73, 45.78, 46.21, 47.77, 48.58, 49.05, 49.72, 50.21,
49.19, 49.6, 49.03, 49.57, 49.37, 49.07, 49.59, 48.54, 48.81,
47.59, 47.57, 46.8, 47.07, 48.59, 47.39, 47.65, 48.45, 47.24,
47.65, 46.4, 46.46, 45.96, 47.26)), class = "data.frame", row.names = c(NA,
-1218L))
Code:
# Tried both of these options, and both don't work
oil_df %>% fill(everything()) %>% fill(everything(), .direction = 'up')
na.locf(na.locf(oil_df), fromLast = TRUE)
# Confirm oil_df has no more missing values
sum(is.na(oil_df))
Output: Excepted 0
> na.locf(na.locf(oil_df), fromLast = TRUE)
>
> # Confirm oil_df has no more missing values
> sum(is.na(oil_df))
[1] 43
The problem with the code in the question is that it is calculating a result and then throwing it away and instead of checking the result it is checking the original data frame. Assign the result of the na.locf to a variable and then check that object. Also note that since there are no NA's on the end we can just use one na.locf if it is ok that the NA's get filled in with the next non-NA.
library(zoo)
oil_df2 <- na.locf(oil_df, fromLast = TRUE)
sum(is.na(oil_df2))
## [1] 0
You can use the downup parameter of fill() in the tidyr package :
Direction in which to fill missing values. Currently either "down" (the default), "up", "downup" (i.e. first down and then up) or "updown" (first up and then down)
df <- data.frame(
id = 1:10,
value = c(NA,100,runif(4,100,200),NA,runif(3,100,200))
)
id value
1 1 NA
2 2 100.0000
3 3 129.3819
4 4 137.7009
5 5 187.6759
6 6 155.0566
7 7 NA
8 8 125.9839
9 9 159.3502
10 10 129.0750
df <- df %>%
fill(value, .direction = "downup")
df
id value
1 1 100.0000
2 2 100.0000
3 3 129.3819
4 4 137.7009
5 5 187.6759
6 6 155.0566
7 7 155.0566
8 8 125.9839
9 9 159.3502
10 10 129.0750
I have the following chart generated using ggplot2. Is there a way in which I can format my dates on the x axis as quarters (for example 2022-Q1) instead of the current date format which I have?
data <- structure(list(Date = structure(c(19083, 19086, 19087, 19088,
19089, 19090, 19093, 19094, 19095, 19096, 19101, 19102, 19103,
19104, 19107, 19108, 19109, 19110, 19111, 19114, 19115, 19116,
19117, 19118, 19121, 19122, 19123, 19124, 19125, 19128, 19129,
19130, 19131, 19132, 19135, 19136, 19137, 19138, 19139, 19143,
19144, 19145, 19146, 19149, 19150, 19151, 19152, 19153, 19156,
19157, 19158, 19159, 19160, 19164, 19165, 19166, 19167, 19170,
19171, 19172, 19173, 19174, 19178, 19179, 19180, 19181, 19184,
19185, 19186, 19187, 19188, 19191, 19192, 19193, 19194, 19195,
19198, 19199, 19200, 19201, 19202, 19205, 19206, 19207, 19208,
19209, 19212, 19213, 19214, 19215, 19216, 19219, 19220, 19221,
19222, 19223, 19226, 19227, 19228, 19229, 19230, 19233, 19234,
19235, 19236, 19237, 19241, 19242, 19243, 19244, 19247), class = "Date"),
US = c(-0.099, -0.082, -0.102, -0.276, -0.265, -0.214, -0.254,
-0.321, -0.263, -0.195, -0.189, -0.077, -0.025, -0.278, -0.543,
-0.595, -0.638, -0.587, -0.571, -0.754, -0.681, -0.597, -0.68,
-0.738, -0.942, -0.882, -0.657, -0.673, -0.468, -0.394, -0.323,
-0.357, -0.604, -0.533, -0.409, -0.529, -0.577, -0.496, -0.344,
-0.362, -0.326, -0.367, -0.365, -0.318, -0.43, -0.405, -0.515,
-0.658, -0.77, -1.033, -1.058, -1.217, -1.112, -1.096, -1.114,
-1.158, -1.013, -0.915, -0.863, -1.099, -1.192, -1.236, -1.395,
-1.144, -1.09, -1.167, -1.325, -1.378, -1.343, -1.492, -1.591,
-1.56, -1.254, -1.505, -1.327, -1.505, -1.542, -1.528, -1.526,
-1.591, -1.416, -1.513, -1.577, -1.389, -1.429, -1.486, -1.547,
-1.649, -1.61, -1.585, -1.475, -1.393, -1.307, -1.376, -1.571,
-1.744, -2.067, -2.294, -2.401, -2.423, -2.449, -2.69, -2.633,
-2.654, -2.812, -2.909, -3.037, -2.912, -2.456, -2.523, -2.51
)), row.names = c(NA, -111L), class = c("tbl_df", "tbl",
"data.frame"))
library(tidyverse)
ggplot(data, aes(Date, US)) +
geom_line(size=2) +
scale_x_date(breaks = '3 months')
You could try making use of the zoo package:
ggplot(data, aes(Date, US)) +
geom_line(size=2) +
scale_x_date(breaks = "3 months",
labels = function(x) zoo::format.yearqtr(x, "%Y-Q%q"))
I am trying to plot a dataset with 3 durations from July-December for 3 years (2019, 2020 & 2021).
Below is my sample dataset:
df_temp <- structure(list(created_at = structure(c(18077, 18077, 18077,
18077, 18077, 18077, 18077, 18077, 18084, 18084, 18084, 18084,
18084, 18084, 18084, 18084, 18091, 18091, 18091, 18091, 18091,
18091, 18091, 18091, 18098, 18098, 18098, 18098, 18098, 18098,
18098, 18098, 18105, 18105, 18105, 18105, 18105, 18105, 18105,
18105, 18112, 18112, 18112, 18112, 18112, 18112, 18112, 18112,
18119, 18119, 18119, 18119, 18119, 18119, 18119, 18119, 18126,
18126, 18126, 18126, 18126, 18126, 18126, 18126, 18133, 18133,
18133, 18133, 18133, 18133, 18133, 18133, 18140, 18140, 18140,
18140, 18140, 18140, 18140, 18140, 18147, 18147, 18147, 18147,
18147, 18147, 18147, 18147, 18154, 18154, 18154, 18154, 18154,
18154, 18154, 18154, 18161, 18161, 18161, 18161, 18161, 18161,
18161, 18161, 18168, 18168, 18168, 18168, 18168, 18168, 18168,
18168, 18175, 18175, 18175, 18175, 18175, 18175, 18175, 18175,
18182, 18182, 18182, 18182, 18182, 18182, 18182, 18182, 18189,
18189, 18189, 18189, 18189, 18189, 18189, 18189, 18196, 18196,
18196, 18196, 18196, 18196, 18196, 18196, 18203, 18203, 18203,
18203, 18203, 18203, 18203, 18203, 18210, 18210, 18210, 18210,
18210, 18210, 18210, 18210, 18217, 18217, 18217, 18217, 18217,
18217, 18217, 18217, 18224, 18224, 18224, 18224, 18224, 18224,
18224, 18224, 18231, 18231, 18231, 18231, 18231, 18231, 18231,
18231, 18238, 18238, 18238, 18238, 18238, 18238, 18238, 18238,
18245, 18245, 18245, 18245, 18245, 18245, 18245, 18245, 18252,
18252, 18252, 18252, 18252, 18252, 18252, 18252, 18259, 18259,
18259, 18259, 18259, 18259, 18259, 18259, 18441, 18441, 18441,
18441, 18441, 18441, 18441, 18441, 18448, 18448, 18448, 18448,
18448, 18448, 18448, 18448, 18455, 18455, 18455, 18455, 18455,
18455, 18455, 18455, 18462, 18462, 18462, 18462, 18462, 18462,
18462, 18462, 18469, 18469, 18469, 18469, 18469, 18469, 18469,
18469, 18476, 18476, 18476, 18476, 18476, 18476, 18476, 18476,
18483, 18483, 18483, 18483, 18483, 18483, 18483, 18483, 18490,
18490, 18490, 18490, 18490, 18490, 18490, 18490, 18497, 18497,
18497, 18497, 18497, 18497, 18497, 18497, 18504, 18504, 18504,
18504, 18504, 18504, 18504, 18504, 18511, 18511, 18511, 18511,
18511, 18511, 18511, 18511, 18518, 18518, 18518, 18518, 18518,
18518, 18518, 18518, 18525, 18525, 18525, 18525, 18525, 18525,
18525, 18525, 18532, 18532, 18532, 18532, 18532, 18532, 18532,
18532, 18539, 18539, 18539, 18539, 18539, 18539, 18539, 18539,
18546, 18546, 18546, 18546, 18546, 18546, 18546, 18546, 18553,
18553, 18553, 18553, 18553, 18553, 18553, 18553, 18560, 18560,
18560, 18560, 18560, 18560, 18560, 18560, 18567, 18567, 18567,
18567, 18567, 18567, 18567, 18567, 18574, 18574, 18574, 18574,
18574, 18574, 18574, 18574, 18581, 18581, 18581, 18581, 18581,
18581, 18581, 18581, 18588, 18588, 18588, 18588, 18588, 18588,
18588, 18588, 18595, 18595, 18595, 18595, 18595, 18595, 18595,
18595, 18602, 18602, 18602, 18602, 18602, 18602, 18602, 18602,
18609, 18609, 18609, 18609, 18609, 18609, 18609, 18609, 18616,
18616, 18616, 18616, 18616, 18616, 18616, 18616, 18623, 18623,
18623, 18623, 18623, 18623, 18623, 18623, 18805, 18805, 18805,
18805, 18805, 18805, 18805, 18805, 18812, 18812, 18812, 18812,
18812, 18812, 18812, 18812, 18819, 18819, 18819, 18819, 18819,
18819, 18819, 18819, 18826, 18826, 18826, 18826, 18826, 18826,
18826, 18826, 18833, 18833, 18833, 18833, 18833, 18833, 18833,
18833, 18840, 18840, 18840, 18840, 18840, 18840, 18840, 18840,
18847, 18847, 18847, 18847, 18847, 18847, 18847, 18847, 18854,
18854, 18854, 18854, 18854, 18854, 18854, 18854, 18861, 18861,
18861, 18861, 18861, 18861, 18861, 18861, 18868, 18868, 18868,
18868, 18868, 18868, 18868, 18868, 18875, 18875, 18875, 18875,
18875, 18875, 18875, 18875, 18882, 18882, 18882, 18882, 18882,
18882, 18882, 18882, 18889, 18889, 18889, 18889, 18889, 18889,
18889, 18889, 18896, 18896, 18896, 18896, 18896, 18896, 18896,
18896, 18903, 18903, 18903, 18903, 18903, 18903, 18903, 18903,
18910, 18910, 18910, 18910, 18910, 18910, 18910, 18910, 18917,
18917, 18917, 18917, 18917, 18917, 18917, 18917, 18924, 18924,
18924, 18924, 18924, 18924, 18924, 18924, 18931, 18931, 18931,
18931, 18931, 18931, 18931, 18931, 18938, 18938, 18938, 18938,
18938, 18938, 18938, 18938, 18945, 18945, 18945, 18945, 18945,
18945, 18945, 18945, 18952, 18952, 18952, 18952, 18952, 18952,
18952, 18952, 18959, 18959, 18959, 18959, 18959, 18959, 18959,
18959, 18966, 18966, 18966, 18966, 18966, 18966, 18966, 18966,
18973, 18973, 18973, 18973, 18973, 18973, 18973, 18973, 18980,
18980, 18980, 18980, 18980, 18980, 18980, 18980, 18987, 18987,
18987, 18987, 18987, 18987, 18987, 18987), class = "Date"), value_count = c(1223L,
2670L, 234L, 633L, 2356L, 614L, 1396L, 2726L, 1512L, 3311L, 316L,
717L, 2920L, 686L, 1865L, 3251L, 1545L, 3164L, 373L, 885L, 2796L,
731L, 1816L, 3096L, 1851L, 3481L, 304L, 747L, 3017L, 602L, 2134L,
3345L, 1335L, 2849L, 287L, 667L, 2420L, 658L, 1523L, 2699L, 1484L,
2916L, 258L, 740L, 2466L, 649L, 1627L, 2721L, 1448L, 2966L, 220L,
715L, 2593L, 661L, 1601L, 2866L, 1568L, 3127L, 297L, 702L, 2697L,
742L, 1657L, 3113L, 1470L, 2987L, 303L, 702L, 2631L, 692L, 1642L,
2874L, 1560L, 3124L, 281L, 751L, 2720L, 721L, 1785L, 3033L, 1566L,
3046L, 300L, 754L, 2606L, 777L, 1767L, 3034L, 1555L, 3102L, 306L,
694L, 2637L, 731L, 1694L, 2985L, 1758L, 3441L, 286L, 782L, 3017L,
731L, 1918L, 3258L, 1520L, 3066L, 248L, 623L, 2702L, 642L, 1719L,
2950L, 1447L, 2947L, 297L, 579L, 2458L, 700L, 1580L, 2839L, 1223L,
2986L, 253L, 575L, 2594L, 997L, 1371L, 2847L, 1211L, 2814L, 201L,
568L, 2304L, 685L, 1369L, 2594L, 1250L, 2527L, 189L, 534L, 2118L,
475L, 1371L, 2370L, 1135L, 2489L, 229L, 637L, 2096L, 645L, 1253L,
2393L, 1276L, 2793L, 206L, 614L, 2393L, 710L, 1332L, 2626L, 1392L,
3098L, 197L, 853L, 2557L, 1041L, 1373L, 2804L, 1013L, 2197L,
159L, 548L, 1893L, 561L, 1049L, 2264L, 1107L, 2437L, 171L, 637L,
1921L, 527L, 1131L, 2556L, 1104L, 2261L, 196L, 690L, 1888L, 583L,
998L, 2380L, 1057L, 2127L, 151L, 586L, 1787L, 444L, 1069L, 2183L,
841L, 1878L, 148L, 423L, 1557L, 345L, 913L, 1771L, 431L, 936L,
61L, 191L, 764L, 174L, 438L, 926L, 1504L, 4509L, 531L, 1392L,
3410L, 1455L, 1951L, 4620L, 2691L, 7555L, 783L, 2652L, 5728L,
2475L, 3062L, 7803L, 2605L, 7468L, 1117L, 2775L, 6166L, 2414L,
3103L, 8453L, 9079L, 14271L, 1097L, 8780L, 12412L, 8430L, 4066L,
14868L, 2794L, 7797L, 908L, 2757L, 5996L, 2596L, 3322L, 8229L,
2995L, 8362L, 881L, 2773L, 6468L, 2708L, 3513L, 8465L, 2705L,
7339L, 827L, 2555L, 5672L, 2326L, 3179L, 8030L, 2949L, 7893L,
788L, 2740L, 6007L, 2533L, 3116L, 8552L, 3660L, 8221L, 1033L,
3254L, 6905L, 2781L, 3607L, 9263L, 2508L, 8395L, 939L, 2417L,
6133L, 3010L, 3891L, 8188L, 2488L, 7144L, 888L, 2408L, 5604L,
2456L, 3060L, 7532L, 2241L, 6646L, 785L, 2221L, 5133L, 2244L,
3046L, 7051L, 2502L, 7347L, 904L, 2906L, 5778L, 2514L, 3215L,
8034L, 2382L, 6440L, 735L, 2292L, 4984L, 2288L, 2837L, 7206L,
2279L, 6113L, 601L, 2269L, 4758L, 2086L, 2726L, 6543L, 2137L,
6519L, 655L, 2314L, 5125L, 2267L, 2961L, 7106L, 2152L, 6474L,
710L, 2296L, 5158L, 2108L, 2802L, 7085L, 2027L, 5667L, 872L,
1983L, 4572L, 2134L, 2535L, 6270L, 2078L, 5207L, 611L, 1999L,
4161L, 1810L, 2234L, 6075L, 1888L, 5688L, 639L, 1892L, 4520L,
1984L, 2630L, 6169L, 2054L, 5880L, 711L, 2154L, 4408L, 2043L,
2641L, 6054L, 2128L, 5477L, 666L, 2185L, 4328L, 2193L, 2516L,
5755L, 1934L, 5126L, 686L, 2065L, 4086L, 2119L, 2280L, 5798L,
1948L, 5488L, 580L, 2091L, 3966L, 1787L, 2291L, 5634L, 1825L,
5352L, 530L, 1926L, 3756L, 1613L, 2100L, 5872L, 1787L, 5226L,
509L, 1677L, 3359L, 1575L, 1937L, 5402L, 1229L, 3912L, 324L,
1086L, 2511L, 1028L, 1505L, 4132L, 673L, 2000L, 148L, 702L, 1538L,
670L, 856L, 2211L, 2070L, 5369L, 424L, 1839L, 4083L, 1685L, 2312L,
5559L, 1955L, 5179L, 383L, 1925L, 3963L, 1587L, 2035L, 5388L,
1730L, 4559L, 434L, 1688L, 4001L, 1607L, 1954L, 5232L, 1419L,
4405L, 411L, 1630L, 3475L, 1473L, 1797L, 4776L, 1584L, 4642L,
391L, 1472L, 3522L, 1494L, 1869L, 4920L, 1529L, 4852L, 473L,
1555L, 3920L, 1583L, 2013L, 5357L, 1527L, 4410L, 454L, 1395L,
3501L, 1296L, 1897L, 4706L, 1558L, 4509L, 474L, 1355L, 3588L,
1214L, 1932L, 4670L, 1341L, 4433L, 432L, 1266L, 3660L, 1351L,
2034L, 4868L, 1457L, 4302L, 345L, 1164L, 3361L, 1118L, 1943L,
4462L, 1562L, 4553L, 357L, 1394L, 3520L, 1200L, 1898L, 4845L,
1649L, 4671L, 377L, 1440L, 3718L, 1236L, 2016L, 4909L, 1878L,
5150L, 544L, 1508L, 3783L, 1362L, 2234L, 5001L, 1861L, 4942L,
424L, 1331L, 3726L, 1120L, 2400L, 4820L, 1540L, 4352L, 429L,
1268L, 3230L, 1016L, 1872L, 4242L, 1926L, 5164L, 391L, 1753L,
4156L, 1199L, 2259L, 4622L, 2000L, 4655L, 402L, 1826L, 3809L,
1076L, 2356L, 4533L, 1910L, 4004L, 341L, 1816L, 3364L, 908L,
2147L, 4332L, 1914L, 4483L, 345L, 1794L, 3451L, 997L, 2220L,
4605L, 1376L, 3966L, 336L, 1361L, 2881L, 1029L, 1712L, 4098L,
1592L, 3425L, 372L, 1093L, 2721L, 1252L, 1536L, 3564L, 1359L,
3583L, 407L, 1099L, 2906L, 1121L, 1551L, 3706L, 1454L, 4175L,
482L, 1345L, 3272L, 1381L, 1684L, 4273L, 1517L, 4008L, 535L,
1571L, 3330L, 1221L, 1640L, 4289L, 1299L, 3473L, 513L, 1290L,
2587L, 1152L, 1441L, 3513L, 817L, 2278L, 289L, 745L, 1719L, 671L,
964L, 2242L)), row.names = c(NA, -648L), groups = structure(list(
created_at = structure(c(18077, 18084, 18091, 18098, 18105,
18112, 18119, 18126, 18133, 18140, 18147, 18154, 18161, 18168,
18175, 18182, 18189, 18196, 18203, 18210, 18217, 18224, 18231,
18238, 18245, 18252, 18259, 18441, 18448, 18455, 18462, 18469,
18476, 18483, 18490, 18497, 18504, 18511, 18518, 18525, 18532,
18539, 18546, 18553, 18560, 18567, 18574, 18581, 18588, 18595,
18602, 18609, 18616, 18623, 18805, 18812, 18819, 18826, 18833,
18840, 18847, 18854, 18861, 18868, 18875, 18882, 18889, 18896,
18903, 18910, 18917, 18924, 18931, 18938, 18945, 18952, 18959,
18966, 18973, 18980, 18987), class = "Date"), .rows = structure(list(
1:8, 9:16, 17:24, 25:32, 33:40, 41:48, 49:56, 57:64,
65:72, 73:80, 81:88, 89:96, 97:104, 105:112, 113:120,
121:128, 129:136, 137:144, 145:152, 153:160, 161:168,
169:176, 177:184, 185:192, 193:200, 201:208, 209:216,
217:224, 225:232, 233:240, 241:248, 249:256, 257:264,
265:272, 273:280, 281:288, 289:296, 297:304, 305:312,
313:320, 321:328, 329:336, 337:344, 345:352, 353:360,
361:368, 369:376, 377:384, 385:392, 393:400, 401:408,
409:416, 417:424, 425:432, 433:440, 441:448, 449:456,
457:464, 465:472, 473:480, 481:488, 489:496, 497:504,
505:512, 513:520, 521:528, 529:536, 537:544, 545:552,
553:560, 561:568, 569:576, 577:584, 585:592, 593:600,
601:608, 609:616, 617:624, 625:632, 633:640, 641:648), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -81L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
When I try to plot it using geom_point, there's unwanted space as shown in the plot below:
I am using the following code to create my plot:
ggplot(data = df_temp, aes(x = created_at)) +
geom_point(aes(y = value_count)) +
labs(title = 'The Plot',
x = '',
y = 'Count',
color = " ") +
theme_bw() +
scale_x_date(date_breaks = '2 month', date_labels = "%b/%y", expand = expansion(0,0))+
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))+
theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
panel.spacing.y=unit(1, "lines"))+
scale_linetype_manual(name = NULL, values = 2)
Can someone please guide how to remove that unwanted space?
If p is the result of the ggplot2 command shown then:
p + facet_wrap(vars(format(created_at, "%Y")), scales = "free_x", nrow = 1)
You could use the function scale_x_bd from the bdscale package like this:
library(ggplot2)
library(scales)
library(bdscale)
ggplot(data = df_temp, aes(x = created_at)) +
geom_point(aes(y = value_count)) +
labs(title = 'The Plot',
x = '',
y = 'Count',
color = " ") +
theme_bw() +
scale_x_bd(business.dates=df_temp$created_at, labels=date_format("%b/%y"), max.major.breaks = 30) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))+
theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
panel.spacing.y=unit(1, "lines"),
axis.text.x=element_text(angle=90))+
scale_linetype_manual(name = NULL, values = 2)
Created on 2022-08-14 by the reprex package (v2.0.1)
I'm trying to bin a date column by every 7 days. If the following date does not land within the 7 days of the previous date, I want the bin to start from that new date and check 7 days forward. I'm using the cut function, but I can't seem to get the function to "start over" when it reaches a new date that doesn't fall within the interval. I have an example below:
ex <- structure(list(svcdate_form = structure(c(17700, 17700, 17700,
17711, 17711, 17711, 17714, 17714, 17714), class = "Date"), map = structure(c(42L,
42L, 42L, 4L, 4L, 4L, 43L, 43L, 43L), .Label = c("2019-12-05",
"2018-08-28", "2018-08-13", "2018-06-25", "2018-04-11", "2019-06-03",
"2019-04-15", "2019-04-22", "2019-04-29", "2019-05-06", "2019-05-13",
"2019-05-20", "2019-05-27", "2019-11-21", "2019-05-21", "2019-08-21",
"2019-05-23", "2018-12-03", "2018-11-08", "2018-11-15", "2019-11-11",
"2019-06-20", "2019-12-23", "2018-08-15", "2018-08-22", "2018-08-29",
"2018-09-05", "2018-01-12", "2018-04-28", "2019-03-15", "2019-04-04",
"2019-02-12", "2019-10-14", "2019-12-15", "2019-02-26", "2019-03-05",
"2018-07-18", "2018-10-03", "2018-01-29", "2018-02-05", "2018-02-12",
"2018-06-18", "2018-07-02", "2019-01-22", "2018-10-26", "2018-02-28",
"2018-03-07", "2019-11-18", "2019-12-17", "2019-07-24", "2019-11-14",
"2018-02-07", "2018-11-05", "2018-02-13", "2018-08-14", "2018-08-21",
"2018-09-04", "2018-09-11", "2018-09-18", "2018-09-25", "2018-10-02",
"2018-10-09", "2018-10-16", "2018-10-23", "2018-10-30", "2018-01-03",
"2019-02-05", "2019-01-18", "2019-04-09", "2019-04-10", "2019-04-17",
"2019-12-13", "2018-11-27", "2018-12-04", "2018-12-11", "2018-04-01",
"2018-03-01", "2018-05-14", "2019-01-28", "2019-02-04", "2018-05-15",
"2018-02-21", "2018-03-14", "2018-03-21", "2018-12-20", "2019-02-14",
"2018-04-16", "2019-01-08", "2019-02-21", "2019-02-28", "2019-03-07",
"2019-03-14", "2018-06-12", "2018-06-19", "2018-06-26", "2018-07-03",
"2018-07-10", "2018-07-17", "2018-07-24", "2018-07-31", "2018-11-20",
"2019-09-06", "2018-06-13", "2018-07-30", "2019-02-06", "2019-11-13",
"2018-12-26", "2018-08-06", "2018-07-20", "2018-03-23", "2018-09-26",
"2019-01-09", "2019-12-30", "2018-08-27", "2019-08-14", "2018-04-17",
"2019-12-20", "2019-07-16", "2019-12-03", "2018-07-23", "2018-09-19",
"2018-11-29", "2019-03-18", "2019-05-16", "2019-09-16", "2019-10-24",
"2018-02-26", "2019-08-23", "2018-03-06", "2019-03-01", "2019-01-07",
"2019-07-30", "2018-12-14", "2018-01-10", "2018-03-08", "2018-10-10",
"2019-10-15", "2019-05-29", "2019-12-19", "2019-05-14", "2019-05-28",
"2018-09-10", "2019-06-17", "2018-11-21", "2018-11-28", "2018-12-05",
"2019-04-01", "2019-10-23", "2018-11-16", "2019-03-22", "2018-06-27",
"2018-10-22", "2019-10-11", "2019-09-19", "2018-07-12", "2018-01-15",
"2019-12-31", "2019-12-02", "2018-10-31", "2019-07-13", "2019-07-20",
"2019-07-27", "2019-08-03", "2019-08-10", "2019-07-03", "2018-04-26",
"2018-03-28", "2018-04-04", "2018-04-18", "2018-04-25", "2018-05-02",
"2019-08-22", "2019-08-29", "2019-09-05", "2019-09-12", "2019-09-26",
"2019-10-03", "2019-10-10", "2019-10-17", "2019-10-31", "2019-11-07",
"2019-01-23", "2018-07-25", "2019-01-21", "2019-12-09", "2018-10-20",
"2018-02-09", "2018-04-24", "2019-11-20", "2019-08-07", "2019-01-02",
"2019-01-16", "2018-12-12", "2018-12-19", "2019-01-30", "2019-02-13",
"2019-02-20", "2019-02-27", "2019-03-06", "2019-03-13", "2019-03-20",
"2019-03-27", "2019-04-03", "2019-04-24", "2019-05-01", "2019-05-08",
"2019-05-15", "2019-06-27", "2019-03-28", "2019-06-04", "2018-04-10",
"2019-06-26", "2019-07-01", "2018-01-02", "2018-02-01", "2019-05-03",
"2019-05-09", "2018-07-06", "2019-06-24", "2019-07-08", "2019-07-15",
"2019-07-22", "2018-02-23", "2018-11-12", "2018-08-20", "2019-06-13",
"2018-10-29", "2019-11-04", "2019-03-21", "2018-07-19", "2018-02-19",
"2019-06-11", "2019-02-19", "2019-07-29", "2018-12-18", "2019-12-10",
"2019-10-30", "2019-12-01", "2019-04-02", "2019-09-03", "2019-09-10",
"2019-09-17", "2018-04-20", "2018-10-01", "2018-01-18", "2019-06-06",
"2019-12-16", "2019-03-29", "2018-03-13", "2019-07-09", "2019-07-23",
"2019-03-19", "2019-07-05", "2018-05-01", "2018-06-05", "2018-08-23",
"2019-04-30", "2018-10-19", "2018-05-31", "2018-01-11", "2019-03-25",
"2018-04-23", "2019-06-28", "2018-03-26", "2019-08-28", "2018-05-08",
"2019-02-11", "2018-08-24", "2019-06-21", "2018-08-19", "2019-07-26",
"2018-08-01", "2019-03-12", "2019-08-12", "2018-09-17", "2019-11-25",
"2019-09-23", "2018-05-05", "2019-08-20", "2018-09-27", "2018-02-16",
"2019-05-07", "2019-03-11", "2019-06-18", "2019-12-04", "2018-09-12",
"2018-12-17", "2018-03-15", "2018-02-08", "2019-04-12", "2019-08-05",
"2018-03-05", "2018-06-15", "2019-11-29", "2018-09-24", "2019-07-17",
"2019-07-31", "2018-07-09", "2018-11-19", "2018-11-26", "2018-10-24",
"2019-09-09", "2018-05-30", "2019-03-26", "2018-07-16", "2019-02-18",
"2019-02-25", "2019-03-04", "2019-05-10", "2019-09-25", "2019-09-24",
"2019-10-01", "2018-12-28", "2019-09-27", "2018-07-26", "2018-10-15",
"2018-05-29", "2018-05-04", "2019-09-13", "2019-10-28", "2018-04-30",
"2019-04-16", "2019-04-23", "2018-02-14", "2018-04-19", "2019-02-08",
"2018-05-18", "2019-12-24", "2019-01-03", "2018-05-21", "2019-06-10",
"2018-12-21", "2019-01-04", "2018-02-06", "2019-10-29", "2018-03-27",
"2018-09-06", "2019-07-12", "2019-10-07", "2019-12-12", "2018-04-03",
"2018-10-08", "2019-06-25", "2019-07-02", "2019-04-19", "2019-04-26",
"2019-05-17", "2019-05-24", "2019-05-31", "2018-11-07", "2018-11-14",
"2018-03-30", "2018-08-09", "2018-01-04", "2018-01-25", "2018-03-02",
"2018-06-06", "2018-05-07", "2018-02-20", "2018-02-27", "2019-09-04",
"2018-11-02", "2019-11-28", "2019-01-17", "2019-09-11", "2019-09-18",
"2019-12-11", "2018-01-13", "2018-01-22", "2019-11-12", "2019-10-08",
"2018-12-10", "2018-06-11", "2018-08-17", "2018-01-31", "2018-01-19",
"2019-11-16", "2018-04-02", "2018-04-09", "2018-05-28", "2018-06-04",
"2018-09-03", "2018-12-24", "2018-12-31", "2019-01-14", "2019-04-08",
"2018-06-14", "2018-06-21", "2018-06-28", "2018-07-05", "2019-08-30",
"2019-01-15", "2018-05-09", "2018-09-28", "2018-04-05", "2018-01-26",
"2018-02-02", "2018-08-10", "2019-08-26", "2018-08-31", "2019-09-30",
"2019-07-18", "2018-01-09", "2018-01-16", "2018-01-23", "2018-01-30",
"2018-08-07", "2019-03-02", "2018-01-17", "2018-01-24", "2018-05-16",
"2018-05-23", "2018-06-20", "2018-07-04", "2018-07-11", "2018-08-08",
"2018-10-17", "2019-06-19", "2019-07-10", "2018-08-03", "2019-08-06",
"2018-04-27", "2018-04-06", "2018-12-06", "2019-10-22", "2019-11-05",
"2019-11-19", "2019-11-26", "2018-05-24", "2019-02-15", "2018-03-20",
"2019-04-11", "2019-12-06", "2019-07-19", "2019-11-22", "2018-03-19",
"2019-10-02", "2019-10-09", "2019-12-27", "2019-08-27", "2019-08-02",
"2019-08-09", "2019-05-22", "2019-06-05", "2019-10-25", "2019-10-16",
"2019-11-06", "2018-05-22", "2018-01-08", "2019-10-21", "2019-01-29",
"2019-08-19", "2019-04-05", "2018-06-22", "2018-06-29", "2019-11-27",
"2018-10-12", "2018-11-25", "2019-12-26", "2018-06-07", "2019-08-15",
"2018-04-12", "2019-02-01", "2019-08-13", "2018-05-03", "2018-09-13",
"2018-09-20", "2018-10-04", "2018-10-11", "2018-10-18", "2018-10-25",
"2019-10-18", "2018-09-07", "2019-11-08", "2018-05-11", "2019-04-18",
"2019-12-07", "2018-11-13", "2018-09-21", "2019-01-11", "2018-03-29",
"2019-07-25", "2019-08-01", "2019-08-08", "2019-09-20", "2018-03-12",
"2019-06-12", "2018-12-27", "2018-07-27", "2019-07-11", "2019-09-01",
"2019-01-10", "2018-02-15", "2018-12-07", "2018-09-14", "2019-01-31",
"2018-01-05", "2018-05-10", "2019-02-02", "2018-07-08", "2018-07-15",
"2018-07-22", "2018-07-29", "2018-08-05", "2018-08-12", "2018-11-01",
"2018-03-16", "2018-11-30", "2019-11-15", "2018-08-02", "2018-02-22",
"2019-03-08", "2018-12-13", "2019-06-07", "2019-06-14", "2018-05-17",
"2018-08-04", "2018-06-08", "2018-08-30", "2018-07-13", "2019-01-24",
"2019-02-07", "2018-12-25", "2019-01-01", "2018-08-16", "2018-03-22",
"2018-11-06", "2018-04-13", "2018-05-25", "2018-06-01", "2018-11-22",
"2019-12-21", "2019-01-25", "2019-10-04", "2019-11-01", "2019-05-30",
"2019-08-16", "2019-08-25", "2019-12-18", "2018-03-09", "2019-04-25",
"2019-11-24", "2019-04-28", "2018-05-27", "2019-07-04", "2019-04-06",
"2019-01-06", "2019-02-22", "2018-10-05", "2019-12-08", "2019-06-22",
"2019-07-28", "2018-11-09", "2018-11-23", "2019-08-31", "2019-09-07",
"2019-09-14", "2019-09-21", "2019-05-18", "2019-10-19", "2019-04-14",
"2018-03-03", "2018-03-10", "2018-03-17", "2018-03-24", "2018-03-31",
"2018-04-07", "2018-12-01", "2018-12-08", "2019-05-02", "2019-06-23",
"2018-06-02", "2019-09-02", "2018-08-18", "2018-01-01", "2018-12-15",
"2018-05-26", "2018-06-09", "2019-10-06", "2019-09-15", "2019-12-14",
"2018-11-11", "2018-11-18", "2018-12-02", "2018-12-09", "2018-07-01",
"2019-05-12", "2019-10-12", "2019-10-26", "2019-09-22", "2018-09-01",
"2019-06-02", "2019-06-01", "2018-01-07", "2018-01-14", "2018-01-21",
"2018-01-28", "2018-02-04", "2018-02-11", "2018-02-18", "2018-02-25",
"2018-03-04", "2018-03-11", "2018-03-18", "2018-03-25", "2018-04-08",
"2018-04-15", "2018-04-22", "2018-04-29", "2018-05-06", "2018-05-13",
"2018-05-20", "2018-06-03", "2018-06-10", "2018-06-17", "2018-06-24",
"2018-08-26", "2018-09-02", "2018-09-09", "2018-09-16", "2018-09-23",
"2018-09-30", "2018-10-07", "2018-10-14", "2018-10-21", "2018-10-28",
"2019-12-25", "2018-09-15", "2018-02-24", "2018-06-30", "2019-01-20",
"2018-07-21", "2019-01-27", "2019-10-27", "2018-11-04", "2018-12-16",
"2018-12-23", "2018-12-30", "2019-01-13", "2019-02-03", "2019-02-10",
"2019-02-17", "2019-02-24", "2019-03-03", "2019-03-10", "2019-03-17",
"2019-03-24", "2019-03-31", "2019-04-07", "2019-04-21", "2019-05-05",
"2019-05-19", "2019-05-26", "2019-06-09", "2019-10-05", "2019-04-27",
"2019-05-04", "2019-05-11", "2019-05-25", "2019-04-13", "2019-04-20",
"2018-02-17", "2019-07-14", "2019-01-05", "2019-03-30", "2018-11-24",
"2019-01-12", "2018-05-19", "2018-06-16", "2018-06-23", "2019-09-08",
"2018-09-22", "2018-11-10", "2019-11-09", "2018-01-06", "2019-07-06",
"2018-07-14", "2018-10-27", "2018-11-03", "2018-11-17", "2018-09-08",
"2019-03-23", "2019-08-11", "2019-07-21", "2019-08-04", "2019-08-18",
"2019-01-26", "2019-12-29", "2018-02-10", "2018-07-28", "2019-02-23",
"2019-09-29", "2019-08-24", "2018-02-03", "2019-06-30", "2018-10-13"
), class = "factor")), row.names = c(NA, -9L), class = c("data.table",
"data.frame"))
The svcdate_form column is what I'm basing this on. The map column is the output I get after using cut. My code is below:
setDT(ex)
ex[,map:= cut(svcdate_form, "7 days", include.lowest = TRUE, right = FALSE)]
I've messed around with the include.lowest and right arguments, but the second group in map always starts with 2018-06-25. What I wanted was, since 2018-06-29 isn't within 7 days of 2018-06-18, the bin would 'start over' and count 7 days from 2018-06-29 and include 2018-07-02. Therefore, in the end my map column would only have two labels: 2018-06-18 and 2018-06-29, but instead it has 3, and 2018-06-25 isn't even in the data.
Is there anything I'm missing, or maybe I'm approaching this with the wrong function? Thank you in advance for any help or insight you can give!
Perhaps this?
ex[, map := cut(svcdate_form, "7 days"), by = .(cumsum(c(TRUE, diff(svcdate_form) > 7)))]
# svcdate_form map
# <Date> <fctr>
# 1: 2018-06-18 2018-06-18
# 2: 2018-06-18 2018-06-18
# 3: 2018-06-18 2018-06-18
# 4: 2018-06-29 2018-06-29
# 5: 2018-06-29 2018-06-29
# 6: 2018-06-29 2018-06-29
# 7: 2018-07-02 2018-06-29
# 8: 2018-07-02 2018-06-29
# 9: 2018-07-02 2018-06-29
This presumes that the data is ordered meaningfully. Depending on previous steps in the pipe, you may prefer to setorder(ex, svcdate_form) or something similar so that diff(svcdate_form) is appropriate.
We want to perform a regression modelof the following dataset:
There are 3 different variables, all factors. For the regression model we want to dummify the variable: container_ids using the dummies package in R
Once that variable is dummified we want to combine the variables "datum" and "fill_level" with the different dummy variables so we can perform our regression model
the problem we have:
we can create dummy data of container_ids in a separate matrix but not in a dataframe. If we want to combine this matrix with the two variables datum and fill_level it doesn't show what we want it to show.
It shows this:
but it needs to add all the different container_ids dummy data
can someone please help us out, would be very nice!
the code we used:
#containerids dummies
library(dummies)
str(container_group)
#creating a matrix with all the dummy data
dummy_container_ids <- dummy(container_group$container_ids, sep="_")
#this doesn't work
containers_dummy <- dummy.data.frame(container_group,sep="_")
#combine the data
#but it only adds one column instead of the multiple dummy data we want to add
combined_data <- cbind(container_group[c(1,3)], dummy_container_ids)
the output i get when: dput(head(container_group, 20))
structure(list(datum = structure(1:20, .Label = c("2017-01-02",
"2017-01-04", "2017-01-05", "2017-01-09", "2017-01-12", "2017-01-16",
"2017-01-19", "2017-01-21", "2017-01-23", "2017-01-26", "2017-01-28",
"2017-01-30", "2017-02-02", "2017-02-03", "2017-02-06", "2017-02-10",
"2017-02-13", "2017-02-18", "2017-02-20", "2017-02-23", "2017-02-25",
"2017-02-27", "2017-03-03", "2017-03-06", "2017-03-13", "2017-03-15",
"2017-03-17", "2017-03-20", "2017-03-21", "2017-03-27", "2017-03-31",
"2017-04-03", "2017-04-05", "2017-04-07", "2017-04-10", "2017-04-15",
"2017-04-16", "2017-04-20", "2017-04-24", "2017-04-25", "2017-04-28",
"2017-05-01", "2017-05-06", "2017-05-08", "2017-05-11", "2017-05-13",
"2017-05-15", "2017-05-17", "2017-05-19", "2017-05-22", "2017-05-26",
"2017-05-31", "2017-06-03", "2017-06-06", "2017-06-08", "2017-06-13",
"2017-06-23", "2017-06-26", "2017-07-04", "2017-07-11", "2017-07-12",
"2017-07-15", "2017-07-17", "2017-07-19", "2017-07-28", "2017-07-31",
"2017-08-03", "2017-08-05", "2017-08-07", "2017-08-10", "2017-08-12",
"2017-08-15", "2017-08-16", "2017-08-18", "2017-08-21", "2017-08-24",
"2017-08-25", "2017-08-29", "2017-09-02", "2017-09-05", "2017-09-07",
"2017-09-12", "2017-09-13", "2017-09-16", "2017-09-18", "2017-09-25",
"2017-10-06", "2017-10-09", "2017-10-11", "2017-10-13", "2017-10-16",
"2017-10-17", "2017-10-20", "2017-10-23", "2017-10-25", "2017-10-27",
"2017-11-02", "2017-11-06", "2017-11-09", "2017-11-11", "2017-11-13",
"2017-11-15", "2017-11-20", "2017-11-24", "2017-11-27", "2017-11-28",
"2017-12-07", "2017-12-14", "2017-12-15", "2017-12-18", "2017-12-20",
"2017-12-22", "2017-12-24", "2017-12-29", "2017-12-31", "2018-01-02",
"2018-01-04", "2018-01-09", "2018-01-13", "2018-01-16", "2018-01-17",
"2018-01-22", "2018-01-25", "2018-01-29", "2018-02-02", "2018-02-03",
"2018-02-05", "2018-02-06", "2018-02-09", "2018-02-13", "2018-02-16",
"2018-02-19", "2018-02-24", "2018-02-26", "2018-03-05", "2018-03-08",
"2018-03-11", "2018-03-14", "2018-03-16", "2018-03-21", "2018-03-26",
"2018-03-29", "2018-03-30", "2018-03-31", "2018-04-04", "2018-04-05",
"2018-04-08", "2018-04-12", "2018-04-20", "2018-04-23", "2018-04-26",
"2018-05-06", "2018-05-10", "2018-05-15", "2018-05-18", "2018-05-22",
"2018-06-04", "2018-06-08", "2018-06-11", "2018-06-16", "2018-06-17",
"2018-06-20", "2018-06-22", "2018-06-28", "2018-07-02", "2018-07-04",
"2018-07-09", "2018-07-17", "2018-07-21", "2018-07-23", "2018-07-24",
"2018-07-31", "2018-08-01", "2018-08-02", "2018-08-03", "2018-08-06",
"2018-08-07", "2018-08-08", "2018-08-10", "2018-08-13", "2018-08-14",
"2018-08-15", "2018-08-17", "2018-08-21", "2018-09-07", "2018-09-11",
"2018-09-19", "2018-09-21", "2018-09-24", "2018-09-25", "2018-09-26",
"2018-09-28", "2018-10-02", "2018-10-05", "2018-10-07", "2018-10-10",
"2018-10-11", "2018-10-12", "2018-10-15", "2018-10-17", "2018-10-19",
"2018-10-22", "2018-10-23", "2018-10-24", "2018-10-26", "2018-10-28",
"2018-10-29", "2018-10-30", "2018-10-31", "2018-11-02", "2018-11-05",
"2018-11-06", "2018-11-07", "2018-11-09", "2018-11-10", "2018-11-11",
"2018-11-12", "2018-11-13", "2018-11-14", "2018-11-16", "2018-11-19",
"2018-11-20", "2018-11-21", "2018-11-23", "2018-11-24", "2018-11-26",
"2018-11-30", "2018-12-02", "2018-12-03", "2018-12-04", "2018-12-05",
"2018-12-06", "2018-12-07", "2018-12-09", "2018-12-10", "2018-12-11",
"2018-12-12", "2018-12-16", "2018-12-17", "2018-12-18", "2018-12-19",
"2018-12-21", "2018-12-24", "2018-12-25", "2018-12-28", "2018-12-30",
"2018-12-31", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-05",
"2019-01-06", "2019-01-09", "2019-01-11", "2019-01-14", "2019-01-15",
"2019-01-16", "2019-01-17", "2019-01-18", "2019-01-21", "2019-01-23",
"2019-01-26", "2019-01-27", "2019-01-28", "2019-01-29", "2019-01-30",
"2019-01-31", "2019-02-01", "2019-02-02", "2019-02-04", "2019-02-05",
"2019-02-06", "2019-02-08", "2019-02-10", "2019-02-11", "2019-02-12",
"2019-02-13", "2019-02-14", "2019-02-15", "2019-02-17", "2019-02-18",
"2019-02-19", "2019-02-20", "2019-02-22", "2019-02-24", "2019-02-25",
"2019-02-26", "2019-02-27", "2019-03-01", "2019-03-02", "2019-03-03",
"2019-03-04", "2019-03-05", "2019-03-06", "2019-03-08", "2019-03-11",
"2019-03-12", "2019-03-13", "2019-03-14", "2019-03-15", "2019-03-16",
"2019-03-18", "2019-03-20", "2019-03-22", "2019-03-24", "2019-03-25",
"2019-03-26", "2019-03-28", "2019-03-29", "2019-03-30", "2019-04-01",
"2019-04-02", "2019-04-03", "2019-04-05", "2019-04-07", "2019-04-08",
"2019-04-09", "2019-04-10", "2019-04-11", "2019-04-12", "2019-04-13",
"2019-04-14", "2019-04-15", "2019-04-16", "2019-04-17", "2019-04-19",
"2019-04-20", "2019-04-22", "2019-04-23", "2019-04-24", "2019-04-25",
"2019-04-26", "2019-04-28", "2019-04-29", "2019-04-30", "2019-05-01",
"2019-05-02", "2019-05-03", "2019-05-04", "2019-05-05", "2019-05-06",
"2019-05-07", "2019-05-08", "2019-05-09", "2019-05-10", "2019-05-12",
"2019-05-13", "2019-05-14", "2019-05-15", "2019-05-16", "2019-05-17",
"2019-05-18", "2019-05-20", "2019-05-21", "2019-05-22", "2019-05-23",
"2019-05-24", "2019-05-25", "2019-05-27", "2019-05-28", "2019-05-29",
"2019-05-31", "2019-06-01", "2019-06-03", "2019-06-04", "2019-06-05",
"2019-06-07", "2019-06-08", "2019-06-10", "2019-06-11", "2019-06-12",
"2019-06-13", "2019-06-14", "2019-06-15", "2019-06-17", "2019-06-18",
"2019-06-19", "2019-06-20", "2019-06-21", "2019-06-22", "2019-06-24",
"2019-06-25", "2019-06-27", "2019-06-28", "2019-06-30", "2019-07-01",
"2019-07-02", "2019-07-03", "2019-07-04", "2019-07-05", "2019-07-06",
"2019-07-07", "2019-07-08", "2019-07-09", "2019-07-10", "2019-07-11",
"2019-07-12", "2019-07-14", "2019-07-15", "2019-07-16", "2019-07-17",
"2019-07-19", "2019-07-20", "2019-07-22", "2019-07-23", "2019-07-26",
"2019-07-27", "2019-07-29", "2019-07-30", "2019-07-31", "2019-08-03",
"2019-08-05", "2019-08-07", "2019-08-09", "2019-08-10", "2019-08-12",
"2019-08-15", "2019-08-20", "2019-08-21", "2019-08-26", "2019-08-27",
"2019-08-29", "2019-08-30", "2019-08-31", "2019-09-02", "2019-09-03",
"2019-09-05", "2019-09-06", "2019-09-08", "2019-09-09", "2019-09-11",
"2019-09-12", "2019-09-13", "2019-09-14", "2019-09-16", "2019-09-17",
"2019-09-18", "2019-09-20", "2019-09-21", "2019-09-25", "2019-09-26",
"2019-09-30", "2019-10-04", "2019-10-07", "2019-10-09", "2019-10-11",
"2019-10-12", "2019-10-15", "2019-10-16", "2019-10-17", "2019-10-18",
"2019-10-21", "2019-10-22", "2019-10-23", "2019-10-24", "2019-10-25",
"2019-10-27", "2019-10-28", "2019-10-31", "2019-11-01", "2019-11-02",
"2019-11-04", "2019-11-05", "2019-11-06", "2019-11-08", "2019-11-09",
"2019-11-11", "2019-11-12", "2019-11-13", "2019-11-14", "2019-11-15",
"2019-11-18", "2019-11-19", "2019-11-20", "2019-11-21", "2019-11-22",
"2019-11-23", "2019-11-26", "2019-11-30", "2019-12-01", "2019-12-02",
"2019-12-03", "2019-12-05", "2019-12-06", "2019-12-07", "2019-12-09",
"2019-12-11", "2019-12-16", "2019-12-18", "2019-12-20", "2019-12-21",
"2019-12-25", "2019-12-29", "2019-12-30", "2020-01-02", "2020-01-03",
"2020-01-06", "2020-01-07", "2020-01-09", "2020-01-13", "2020-01-14",
"2020-01-16", "2020-01-17", "2020-01-20", "2020-01-22", "2020-01-25",
"2020-01-27", "2020-01-28", "2020-01-30", "2020-01-31", "2020-02-01",
"2020-02-03", "2020-02-10", "2020-02-12", "2020-02-14", "2020-02-20",
"2020-02-21", "2020-02-22", "2020-02-24", "2020-02-25", "2020-02-26",
"2020-02-28", "2020-03-02", "2020-03-03", "2020-03-04", "2020-03-05",
"2020-03-07", "2020-03-09", "2020-03-10", "2020-03-11", "2020-03-12",
"2020-03-13", "2020-03-16", "2020-03-17", "2020-03-18", "2020-03-21",
"2020-03-23", "2020-03-28", "2020-03-30", "2020-04-01", "2020-04-04",
"2020-04-10", "2020-04-13", "2020-04-14", "2020-04-15", "2020-04-16",
"2020-04-17", "2020-04-21", "2020-04-24", "2020-04-25", "2020-04-27",
"2020-05-01", "2020-05-02", "2020-05-04", "2020-05-08", "2020-05-11",
"2020-05-13", "2020-05-15", "2020-05-18", "2020-05-20", "2020-05-23",
"2020-05-25", "2020-05-27", "2020-06-01", "2020-06-02", "2020-06-03",
"2020-06-04", "2020-06-05", "2020-06-06", "2020-06-08", "2020-06-09",
"2020-06-10", "2020-06-12", "2020-06-13", "2020-06-15", "2020-06-17",
"2020-06-19", "2020-06-20", "2020-06-22", "2020-06-23", "2020-06-24",
"2020-06-26", "2020-06-27", "2020-06-29", "2020-06-30", "2020-07-01",
"2020-07-02", "2020-07-03", "2020-07-04", "2020-07-05", "2020-07-06",
"2020-07-13", "2020-07-15", "2020-07-17", "2020-07-18", "2020-07-20",
"2020-07-21", "2020-07-31", "2020-08-01", "2020-08-05", "2020-08-11",
"2020-08-15", "2020-08-19", "2020-08-21", "2020-08-22", "2020-08-24",
"2020-08-25", "2020-08-26", "2020-08-31", "2020-09-05", "2020-09-07",
"2020-09-08", "2020-09-10", "2020-09-11", "2020-09-12", "2020-09-13",
"2020-09-16", "2020-09-18", "2020-09-20", "2020-09-21", "2020-09-22",
"2020-09-23", "2020-09-24", "2020-09-25", "2020-09-26", "2020-09-28",
"2020-09-29", "2020-09-30", "2020-10-04", "2020-10-05", "2020-10-06",
"2020-10-07", "2020-10-09", "2020-10-10", "2020-10-12", "2020-10-13",
"2020-10-19", "2020-10-23", "2020-10-26", "2020-10-28", "2020-10-30",
"2020-11-02", "2020-11-07", "2020-11-13", "2020-11-16", "2020-11-17",
"2020-11-18", "2020-11-20", "2020-11-23", "2020-11-25", "2020-11-28",
"2020-11-30", "2020-12-01", "2020-12-02", "2020-12-04", "2020-12-05",
"2020-12-07", "2020-12-08", "2020-12-09", "2020-12-11", "2020-12-12",
"2020-12-13", "2020-12-14", "2020-12-15", "2020-12-16", "2020-12-17",
"2020-12-18", "2020-12-19", "2020-12-21", "2020-12-22", "2020-12-25",
"2020-12-28", "2020-12-29", "2020-12-30", "2021-01-01", "2021-01-04",
"2021-01-05", "2021-01-06", "2021-01-07", "2021-01-08"), class = "factor"),
container_ids = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("GL1",
"GL2", "PA1", "PA2", "PA3", "PL1", "PMD1", "REF1", "TEX1"
), class = "factor"), fill_level = structure(c(48L, 29L,
29L, 70L, 44L, 63L, 45L, 41L, 47L, 35L, 40L, 51L, 33L, 38L,
62L, 45L, 35L, 59L, 61L, 41L), .Label = c("1", "2", "3",
"4", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45",
"46", "47", "48", "49", "50", "51", "52", "53", "54", "55",
"56", "57", "58", "59", "60", "61", "62", "63", "64", "65",
"66", "67", "68", "69", "70", "71", "72", "73", "74", "75",
"76", "78", "79", "80", "82", "84", "85", "86", "88", "89",
"90", "92", "93", "94", "100"), class = "factor")), row.names = c(NA,
-20L), groups = structure(list(datum = structure(1:20, .Label = c("2017-01-02",
"2017-01-04", "2017-01-05", "2017-01-09", "2017-01-12", "2017-01-16",
"2017-01-19", "2017-01-21", "2017-01-23", "2017-01-26", "2017-01-28",
"2017-01-30", "2017-02-02", "2017-02-03", "2017-02-06", "2017-02-10",
"2017-02-13", "2017-02-18", "2017-02-20", "2017-02-23", "2017-02-25",
"2017-02-27", "2017-03-03", "2017-03-06", "2017-03-13", "2017-03-15",
"2017-03-17", "2017-03-20", "2017-03-21", "2017-03-27", "2017-03-31",
"2017-04-03", "2017-04-05", "2017-04-07", "2017-04-10", "2017-04-15",
"2017-04-16", "2017-04-20", "2017-04-24", "2017-04-25", "2017-04-28",
"2017-05-01", "2017-05-06", "2017-05-08", "2017-05-11", "2017-05-13",
"2017-05-15", "2017-05-17", "2017-05-19", "2017-05-22", "2017-05-26",
"2017-05-31", "2017-06-03", "2017-06-06", "2017-06-08", "2017-06-13",
"2017-06-23", "2017-06-26", "2017-07-04", "2017-07-11", "2017-07-12",
"2017-07-15", "2017-07-17", "2017-07-19", "2017-07-28", "2017-07-31",
"2017-08-03", "2017-08-05", "2017-08-07", "2017-08-10", "2017-08-12",
"2017-08-15", "2017-08-16", "2017-08-18", "2017-08-21", "2017-08-24",
"2017-08-25", "2017-08-29", "2017-09-02", "2017-09-05", "2017-09-07",
"2017-09-12", "2017-09-13", "2017-09-16", "2017-09-18", "2017-09-25",
"2017-10-06", "2017-10-09", "2017-10-11", "2017-10-13", "2017-10-16",
"2017-10-17", "2017-10-20", "2017-10-23", "2017-10-25", "2017-10-27",
"2017-11-02", "2017-11-06", "2017-11-09", "2017-11-11", "2017-11-13",
"2017-11-15", "2017-11-20", "2017-11-24", "2017-11-27", "2017-11-28",
"2017-12-07", "2017-12-14", "2017-12-15", "2017-12-18", "2017-12-20",
"2017-12-22", "2017-12-24", "2017-12-29", "2017-12-31", "2018-01-02",
"2018-01-04", "2018-01-09", "2018-01-13", "2018-01-16", "2018-01-17",
"2018-01-22", "2018-01-25", "2018-01-29", "2018-02-02", "2018-02-03",
"2018-02-05", "2018-02-06", "2018-02-09", "2018-02-13", "2018-02-16",
"2018-02-19", "2018-02-24", "2018-02-26", "2018-03-05", "2018-03-08",
"2018-03-11", "2018-03-14", "2018-03-16", "2018-03-21", "2018-03-26",
"2018-03-29", "2018-03-30", "2018-03-31", "2018-04-04", "2018-04-05",
"2018-04-08", "2018-04-12", "2018-04-20", "2018-04-23", "2018-04-26",
"2018-05-06", "2018-05-10", "2018-05-15", "2018-05-18", "2018-05-22",
"2018-06-04", "2018-06-08", "2018-06-11", "2018-06-16", "2018-06-17",
"2018-06-20", "2018-06-22", "2018-06-28", "2018-07-02", "2018-07-04",
"2018-07-09", "2018-07-17", "2018-07-21", "2018-07-23", "2018-07-24",
"2018-07-31", "2018-08-01", "2018-08-02", "2018-08-03", "2018-08-06",
"2018-08-07", "2018-08-08", "2018-08-10", "2018-08-13", "2018-08-14",
"2018-08-15", "2018-08-17", "2018-08-21", "2018-09-07", "2018-09-11",
"2018-09-19", "2018-09-21", "2018-09-24", "2018-09-25", "2018-09-26",
"2018-09-28", "2018-10-02", "2018-10-05", "2018-10-07", "2018-10-10",
"2018-10-11", "2018-10-12", "2018-10-15", "2018-10-17", "2018-10-19",
"2018-10-22", "2018-10-23", "2018-10-24", "2018-10-26", "2018-10-28",
"2018-10-29", "2018-10-30", "2018-10-31", "2018-11-02", "2018-11-05",
"2018-11-06", "2018-11-07", "2018-11-09", "2018-11-10", "2018-11-11",
"2018-11-12", "2018-11-13", "2018-11-14", "2018-11-16", "2018-11-19",
"2018-11-20", "2018-11-21", "2018-11-23", "2018-11-24", "2018-11-26",
"2018-11-30", "2018-12-02", "2018-12-03", "2018-12-04", "2018-12-05",
"2018-12-06", "2018-12-07", "2018-12-09", "2018-12-10", "2018-12-11",
"2018-12-12", "2018-12-16", "2018-12-17", "2018-12-18", "2018-12-19",
"2018-12-21", "2018-12-24", "2018-12-25", "2018-12-28", "2018-12-30",
"2018-12-31", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-05",
"2019-01-06", "2019-01-09", "2019-01-11", "2019-01-14", "2019-01-15",
"2019-01-16", "2019-01-17", "2019-01-18", "2019-01-21", "2019-01-23",
"2019-01-26", "2019-01-27", "2019-01-28", "2019-01-29", "2019-01-30",
"2019-01-31", "2019-02-01", "2019-02-02", "2019-02-04", "2019-02-05",
"2019-02-06", "2019-02-08", "2019-02-10", "2019-02-11", "2019-02-12",
"2019-02-13", "2019-02-14", "2019-02-15", "2019-02-17", "2019-02-18",
"2019-02-19", "2019-02-20", "2019-02-22", "2019-02-24", "2019-02-25",
"2019-02-26", "2019-02-27", "2019-03-01", "2019-03-02", "2019-03-03",
"2019-03-04", "2019-03-05", "2019-03-06", "2019-03-08", "2019-03-11",
"2019-03-12", "2019-03-13", "2019-03-14", "2019-03-15", "2019-03-16",
"2019-03-18", "2019-03-20", "2019-03-22", "2019-03-24", "2019-03-25",
"2019-03-26", "2019-03-28", "2019-03-29", "2019-03-30", "2019-04-01",
"2019-04-02", "2019-04-03", "2019-04-05", "2019-04-07", "2019-04-08",
"2019-04-09", "2019-04-10", "2019-04-11", "2019-04-12", "2019-04-13",
"2019-04-14", "2019-04-15", "2019-04-16", "2019-04-17", "2019-04-19",
"2019-04-20", "2019-04-22", "2019-04-23", "2019-04-24", "2019-04-25",
"2019-04-26", "2019-04-28", "2019-04-29", "2019-04-30", "2019-05-01",
"2019-05-02", "2019-05-03", "2019-05-04", "2019-05-05", "2019-05-06",
"2019-05-07", "2019-05-08", "2019-05-09", "2019-05-10", "2019-05-12",
"2019-05-13", "2019-05-14", "2019-05-15", "2019-05-16", "2019-05-17",
"2019-05-18", "2019-05-20", "2019-05-21", "2019-05-22", "2019-05-23",
"2019-05-24", "2019-05-25", "2019-05-27", "2019-05-28", "2019-05-29",
"2019-05-31", "2019-06-01", "2019-06-03", "2019-06-04", "2019-06-05",
"2019-06-07", "2019-06-08", "2019-06-10", "2019-06-11", "2019-06-12",
"2019-06-13", "2019-06-14", "2019-06-15", "2019-06-17", "2019-06-18",
"2019-06-19", "2019-06-20", "2019-06-21", "2019-06-22", "2019-06-24",
"2019-06-25", "2019-06-27", "2019-06-28", "2019-06-30", "2019-07-01",
"2019-07-02", "2019-07-03", "2019-07-04", "2019-07-05", "2019-07-06",
"2019-07-07", "2019-07-08", "2019-07-09", "2019-07-10", "2019-07-11",
"2019-07-12", "2019-07-14", "2019-07-15", "2019-07-16", "2019-07-17",
"2019-07-19", "2019-07-20", "2019-07-22", "2019-07-23", "2019-07-26",
"2019-07-27", "2019-07-29", "2019-07-30", "2019-07-31", "2019-08-03",
"2019-08-05", "2019-08-07", "2019-08-09", "2019-08-10", "2019-08-12",
"2019-08-15", "2019-08-20", "2019-08-21", "2019-08-26", "2019-08-27",
"2019-08-29", "2019-08-30", "2019-08-31", "2019-09-02", "2019-09-03",
"2019-09-05", "2019-09-06", "2019-09-08", "2019-09-09", "2019-09-11",
"2019-09-12", "2019-09-13", "2019-09-14", "2019-09-16", "2019-09-17",
"2019-09-18", "2019-09-20", "2019-09-21", "2019-09-25", "2019-09-26",
"2019-09-30", "2019-10-04", "2019-10-07", "2019-10-09", "2019-10-11",
"2019-10-12", "2019-10-15", "2019-10-16", "2019-10-17", "2019-10-18",
"2019-10-21", "2019-10-22", "2019-10-23", "2019-10-24", "2019-10-25",
"2019-10-27", "2019-10-28", "2019-10-31", "2019-11-01", "2019-11-02",
"2019-11-04", "2019-11-05", "2019-11-06", "2019-11-08", "2019-11-09",
"2019-11-11", "2019-11-12", "2019-11-13", "2019-11-14", "2019-11-15",
"2019-11-18", "2019-11-19", "2019-11-20", "2019-11-21", "2019-11-22",
"2019-11-23", "2019-11-26", "2019-11-30", "2019-12-01", "2019-12-02",
"2019-12-03", "2019-12-05", "2019-12-06", "2019-12-07", "2019-12-09",
"2019-12-11", "2019-12-16", "2019-12-18", "2019-12-20", "2019-12-21",
"2019-12-25", "2019-12-29", "2019-12-30", "2020-01-02", "2020-01-03",
"2020-01-06", "2020-01-07", "2020-01-09", "2020-01-13", "2020-01-14",
"2020-01-16", "2020-01-17", "2020-01-20", "2020-01-22", "2020-01-25",
"2020-01-27", "2020-01-28", "2020-01-30", "2020-01-31", "2020-02-01",
"2020-02-03", "2020-02-10", "2020-02-12", "2020-02-14", "2020-02-20",
"2020-02-21", "2020-02-22", "2020-02-24", "2020-02-25", "2020-02-26",
"2020-02-28", "2020-03-02", "2020-03-03", "2020-03-04", "2020-03-05",
"2020-03-07", "2020-03-09", "2020-03-10", "2020-03-11", "2020-03-12",
"2020-03-13", "2020-03-16", "2020-03-17", "2020-03-18", "2020-03-21",
"2020-03-23", "2020-03-28", "2020-03-30", "2020-04-01", "2020-04-04",
"2020-04-10", "2020-04-13", "2020-04-14", "2020-04-15", "2020-04-16",
"2020-04-17", "2020-04-21", "2020-04-24", "2020-04-25", "2020-04-27",
"2020-05-01", "2020-05-02", "2020-05-04", "2020-05-08", "2020-05-11",
"2020-05-13", "2020-05-15", "2020-05-18", "2020-05-20", "2020-05-23",
"2020-05-25", "2020-05-27", "2020-06-01", "2020-06-02", "2020-06-03",
"2020-06-04", "2020-06-05", "2020-06-06", "2020-06-08", "2020-06-09",
"2020-06-10", "2020-06-12", "2020-06-13", "2020-06-15", "2020-06-17",
"2020-06-19", "2020-06-20", "2020-06-22", "2020-06-23", "2020-06-24",
"2020-06-26", "2020-06-27", "2020-06-29", "2020-06-30", "2020-07-01",
"2020-07-02", "2020-07-03", "2020-07-04", "2020-07-05", "2020-07-06",
"2020-07-13", "2020-07-15", "2020-07-17", "2020-07-18", "2020-07-20",
"2020-07-21", "2020-07-31", "2020-08-01", "2020-08-05", "2020-08-11",
"2020-08-15", "2020-08-19", "2020-08-21", "2020-08-22", "2020-08-24",
"2020-08-25", "2020-08-26", "2020-08-31", "2020-09-05", "2020-09-07",
"2020-09-08", "2020-09-10", "2020-09-11", "2020-09-12", "2020-09-13",
"2020-09-16", "2020-09-18", "2020-09-20", "2020-09-21", "2020-09-22",
"2020-09-23", "2020-09-24", "2020-09-25", "2020-09-26", "2020-09-28",
"2020-09-29", "2020-09-30", "2020-10-04", "2020-10-05", "2020-10-06",
"2020-10-07", "2020-10-09", "2020-10-10", "2020-10-12", "2020-10-13",
"2020-10-19", "2020-10-23", "2020-10-26", "2020-10-28", "2020-10-30",
"2020-11-02", "2020-11-07", "2020-11-13", "2020-11-16", "2020-11-17",
"2020-11-18", "2020-11-20", "2020-11-23", "2020-11-25", "2020-11-28",
"2020-11-30", "2020-12-01", "2020-12-02", "2020-12-04", "2020-12-05",
"2020-12-07", "2020-12-08", "2020-12-09", "2020-12-11", "2020-12-12",
"2020-12-13", "2020-12-14", "2020-12-15", "2020-12-16", "2020-12-17",
"2020-12-18", "2020-12-19", "2020-12-21", "2020-12-22", "2020-12-25",
"2020-12-28", "2020-12-29", "2020-12-30", "2021-01-01", "2021-01-04",
"2021-01-05", "2021-01-06", "2021-01-07", "2021-01-08"), class = "factor"),
.rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 20L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Most likely you do the cbind() wrongly, it should be container_group[,c(1,3)] instead of container_group[c(1,3)]:
da = data.frame(datum = c("2017-04-20","2019-01-11","2019-05-03"),container_ids = c("REF1","GL1","PA1"),fill_level = c(100,94,93))
dummy_container_ids <- dummy(da$container_ids, sep="_")
cbind(container_group[,c(1,3)],dummy_container_ids)
datum fill_level container_ids_GL1 container_ids_PA1 container_ids_REF1
1 2017-04-20 100 0 0 1
2 2019-01-11 94 1 0 0
3 2019-05-03 93 0 1 0