using dummify with the dummies datapack in R - r

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

Related

R - Data Frame Forward and Backward Fill

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

format dates as quarters in ggplot2 x axis

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"))

Sum a daily variable per month taking into account the year

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

'cut' function for date column not labeling actual dates from data

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.

Construct multiple series ("mts") in R

I cannot find working example how to make mts object for time-series.
I have made an example of input data. There is one timeseries structured with frequency 4. I would like to separate it to 4 timeseries represented as mts format (described in documentation for ts):
class
class to be given to the result, or none if NULL or "none". The default is "ts" for a single series, c("mts", "ts", "matrix") for multiple series.
How I can split it? I couldn't find any documentation of examples how "mts" objects can be created.
Qtr1 Qtr2 Qtr3 Qtr4
1 100.00 102.09 102.50 102.60
2 102.66 102.01 100.70 102.21
3 102.50 102.81 102.91 103.08
4 103.25 101.70 101.10 101.49
5 100.12 99.40 99.28 97.75
6 96.65 99.03 99.28 99.91
7 99.29 98.57 98.45 100.27
8 100.65 102.12 101.55 101.55
9 99.51 99.95 98.91 96.87
10 95.14 94.82 96.04 96.91
11 95.53 93.14 92.97 94.54
12 91.72 92.35 94.79 94.78
13 95.50 95.97 94.64 96.56
14 96.45 95.13 94.32 92.92
Appendix
structure(c(100, 102.09, 102.5, 102.6, 102.66, 102.01, 100.7,
102.21, 102.5, 102.81, 102.91, 103.08, 103.25, 101.7, 101.1,
101.49, 100.12, 99.4, 99.28, 97.75, 96.65, 99.03, 99.28, 99.91,
99.29, 98.57, 98.45, 100.27, 100.65, 102.12, 101.55, 101.55,
99.51, 99.95, 98.91, 96.87, 95.14, 94.82, 96.04, 96.91, 95.53,
93.14, 92.97, 94.54, 91.72, 92.35, 94.79, 94.78, 95.5, 95.97,
94.64, 96.56, 96.45, 95.13, 94.32, 92.92, 92.58, 89.74, 87.7,
89.5, 86.69, 87.91, 89.12, 88.93, 89.95, 90.42, 90.22, 90.75,
90.2, 89.5, 90.52, 90.88, 90.98, 91.85, 92.03, 91.85, 92.54,
92.69, 92.36, 92.35, 91.56, 92.67, 92.88, 92.26, 92.36, 92.51,
92.26, 92.47, 92.65, 92.89, 91.67, 91.95, 92.77, 92.59, 91.83,
92.2, 91.47, 92.33, 92.03, 92.74, 92.74, 93.13, 94.61, 95.23,
95.04, 95.35, 96.46, 96.6, 95.93, 95.61, 95.22, 95.34, 95.58,
95.64, 94.41, 94.73, 93.64, 92.87, 92.89, 93.67, 94.69, 94.51,
94.14, 94.53, 94.63, 95.15, 94.67, 95.75, 96.16, 96.99, 97.3,
97.97, 98.34, 98.28, 97.85, 97.54, 97.48, 97.09, 97.28, 97.8,
97.08, 96.28, 94.58, 92.94, 91.26, 91.92, 91.03, 92.13, 94.45,
94.68, 95.02, 95.79, 92.29, 89.45, 91.52, 93.9, 94.51, 95.47,
95.1, 93.4, 92.55, 93.33, 94.33, 95.8, 96.19, 96.4, 96.72, 96.34,
96.7, 96.22, 97.27, 96.84, 97.08, 97.14, 97.45, 97.65, 96.6,
96.99, 95.89, 95.62, 96.48, 97.5, 97.57, 98.43, 98.37, 99.31,
99.27, 99.38, 98.45, 97.56, 98.06, 97.39, 97.69, 98.21, 98.12,
97.56, 97.97, 98.01, 98.47, 98.14, 97.87, 99.25, 99.73, 99.55,
99.8, 99.74, 98.96, 98.12, 98.07, 98.12, 98.53, 97.87, 98.66,
98.89, 98.9, 99.94, 99.66, 98.7, 98.59, 99.22, 98.68, 98.74,
99.39, 100.06, 99.77, 99.48, 98.73, 99.41, 98.95, 98.35, 97.54,
98.64, 97.71, 98.44, 98.85, 98.31, 98.22, 97.72, 97.27, 97.31,
97.31, 97.27, 96.9, 95.1, 94.73, 94.03, 95.68, 95.94, 97.13,
97.39, 96.62, 96.93, 97.26, 97.48, 98.23, 98.1, 97.52, 96.88,
96.81, 97.39, 98.39, 97.93, 98.48, 98.64, 97.77, 97.74, 98.44,
99.1, 99.16, 99.27, 100.41, 99.54, 100.67, 100.21, 100.87, 101.45,
101.42, 101.52, 101.55, 101.7, 101.8, 102.18, 102.16, 102.05,
101.86, 102.97, 103.43, 103.75, 103.85, 103.86, 103.99, 103.87,
103.39, 104.23, 103.47, 102.98, 103.01, 102.92, 102.75, 102.24,
102.22, 103.86, 104.07, 103.72, 102.97, 102.86, 103.26, 102.72,
103.57, 103.35, 103.88, 103.86, 104.56, 104.79, 104.9, 104.66,
105.25, 105.1, 105.4, 105.61, 106.15, 106.28, 106.15, 105.53,
105.67, 105.94, 106.88, 107.58, 107.64, 107.61, 107.35, 107.41,
107.53, 107.91, 108.02, 107.77, 108.07, 108.4, 108.69, 108.71,
107.81, 107.18, 108.02, 107.87, 107.53, 107.64, 108.33, 108.87,
108.71, 108.45, 108.64, 108.7, 108.74, 108.79, 109.23, 109.42,
109.82, 109.52, 108.95, 109.26, 109.36, 109.35, 110.22, 110.72
), .Dim = c(364L, 1L), index = structure(c(1447632000, 1447718400,
1447804800, 1447891200, 1447977600, 1448236800, 1448323200, 1448409600,
1448496000, 1448582400, 1448841600, 1448928000, 1449014400, 1449100800,
1449187200, 1449446400, 1449532800, 1449619200, 1449705600, 1449792000,
1450051200, 1450137600, 1450224000, 1450310400, 1450396800, 1450656000,
1450742400, 1450828800, 1451260800, 1451347200, 1451433600, 1451520000,
1451865600, 1451952000, 1452038400, 1452124800, 1452211200, 1452470400,
1452556800, 1452643200, 1452729600, 1452816000, 1453075200, 1453161600,
1453248000, 1453334400, 1453420800, 1453680000, 1453766400, 1453852800,
1453939200, 1454025600, 1454284800, 1454371200, 1454457600, 1454544000,
1454630400, 1454889600, 1454976000, 1455062400, 1455148800, 1455235200,
1455494400, 1455580800, 1455667200, 1455753600, 1455840000, 1456099200,
1456185600, 1456272000, 1456358400, 1456444800, 1456704000, 1456790400,
1456876800, 1456963200, 1457049600, 1457308800, 1457395200, 1457481600,
1457568000, 1457654400, 1457913600, 1.458e+09, 1458086400, 1458172800,
1458259200, 1458518400, 1458604800, 1458691200, 1458777600, 1459209600,
1459296000, 1459382400, 1459468800, 1459728000, 1459814400, 1459900800,
1459987200, 1460073600, 1460332800, 1460419200, 1460505600, 1460592000,
1460678400, 1460937600, 1461024000, 1461110400, 1461196800, 1461283200,
1461542400, 1461628800, 1461715200, 1461801600, 1461888000, 1462147200,
1462233600, 1462320000, 1462492800, 1462752000, 1462838400, 1462924800,
1463011200, 1463097600, 1463443200, 1463529600, 1463616000, 1463702400,
1463961600, 1464048000, 1464134400, 1464220800, 1464307200, 1464566400,
1464652800, 1464739200, 1464825600, 1464912000, 1465171200, 1465257600,
1465344000, 1465430400, 1465516800, 1465776000, 1465862400, 1465948800,
1466035200, 1466121600, 1466380800, 1466467200, 1466553600, 1466640000,
1466726400, 1466985600, 1467072000, 1467158400, 1467244800, 1467331200,
1467590400, 1467676800, 1467763200, 1467849600, 1467936000, 1468195200,
1468281600, 1468368000, 1468454400, 1468540800, 1468800000, 1468886400,
1468972800, 1469059200, 1469145600, 1469404800, 1469491200, 1469577600,
1469664000, 1469750400, 1470096000, 1470182400, 1470268800, 1470355200,
1470614400, 1470700800, 1470787200, 1470873600, 1470960000, 1471219200,
1471305600, 1471392000, 1471478400, 1471564800, 1471824000, 1471910400,
1471996800, 1472083200, 1472169600, 1472428800, 1472515200, 1472601600,
1472688000, 1472774400, 1473033600, 1473120000, 1473206400, 1473292800,
1473379200, 1473638400, 1473724800, 1473811200, 1473897600, 1473984000,
1474243200, 1474329600, 1474416000, 1474502400, 1474588800, 1474848000,
1474934400, 1475020800, 1475107200, 1475193600, 1475452800, 1475539200,
1475625600, 1475712000, 1475798400, 1476057600, 1476144000, 1476230400,
1476316800, 1476403200, 1476662400, 1476748800, 1476835200, 1476921600,
1477008000, 1477267200, 1477353600, 1477440000, 1477526400, 1477612800,
1477872000, 1478044800, 1478131200, 1478217600, 1478476800, 1478563200,
1478649600, 1478736000, 1478822400, 1479081600, 1479168000, 1479254400,
1479340800, 1479427200, 1479686400, 1479772800, 1479859200, 1479945600,
1480032000, 1480291200, 1480377600, 1480464000, 1480550400, 1480636800,
1480896000, 1480982400, 1481068800, 1481155200, 1481241600, 1481500800,
1481587200, 1481673600, 1481760000, 1481846400, 1482105600, 1482192000,
1482278400, 1482364800, 1482451200, 1482796800, 1482883200, 1482969600,
1483056000, 1483401600, 1483488000, 1483574400, 1483660800, 1483920000,
1484006400, 1484092800, 1484179200, 1484265600, 1484524800, 1484611200,
1484697600, 1484784000, 1484870400, 1485129600, 1485216000, 1485302400,
1485388800, 1485475200, 1485734400, 1485820800, 1485907200, 1485993600,
1486080000, 1486339200, 1486425600, 1486512000, 1486598400, 1486684800,
1486944000, 1487030400, 1487116800, 1487203200, 1487289600, 1487548800,
1487635200, 1487721600, 1487808000, 1487894400, 1488153600, 1488240000,
1488326400, 1488412800, 1488499200, 1488758400, 1488844800, 1488931200,
1489017600, 1489104000, 1489363200, 1489449600, 1489536000, 1489622400,
1489708800, 1489968000, 1490054400, 1490140800, 1490227200, 1490313600,
1490572800, 1490659200, 1490745600, 1490832000, 1490918400, 1491177600,
1491264000, 1491350400, 1491436800, 1491523200, 1491782400, 1491868800,
1491955200, 1492041600, 1492473600, 1492560000, 1492646400, 1492732800,
1492992000, 1493078400), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", .Dimnames = list(
NULL, "Series 1"), class = "ts", .Tsp = c(1, 91.75, 4))
A multiple time series example build on hourly data.
require(stats)
require(lubridate)
data_mts <- ts(data=yourDATA[,c("Qtr1","Qtr2","Qtr3","Qtr4")],
frequency=61320, start=c(year(min(data_IE$time)),month(min(data_IE$time)), date(min(data_IE$time)),hour(min(data_IE$time))))
#This frequency variable is for hourly data,hourly frequency, i.e. 61320.
Using xts and zoo packages you could create individual time series and merge them as follows:
library("xts")
library("zoo") #for as.yearqtr
#tsDF, your input data
#extract individual time series
tsMat = matrix(tsDF,ncol=4)
#setup hypothetical indices as yearqtr e.g. 1990 Q1, 1992 Q2 etc.
startYear1 = 1990
startYear2 = 1990
startYear3 = 1991
startYear4 = 1992
index1=seq(startYear1,by=1,length.out=nrow(tsMat))
index2=seq(startYear2,by=1,length.out=nrow(tsMat))
index3=seq(startYear3,by=1,length.out=nrow(tsMat))
index4=seq(startYear4,by=1,length.out=nrow(tsMat))
index1 = as.yearqtr(paste(index1,"Q1",sep=" "))
index2 = as.yearqtr(paste(index2,"Q2",sep=" "))
index3 = as.yearqtr(paste(index3,"Q3",sep=" "))
index4 = as.yearqtr(paste(index4,"Q4",sep=" "))
#combine indices in a list
listIndices = list(index1,index2,index3,index4)
nameVec = paste0("Qtr",1:4)
#Use lapply to create individual xts time series with its correponding index created above
tsList =
lapply(1:ncol(tsMat), function(x) {
xtsObj = xts(tsMat[,x],order.by=listIndices[[x]])
colnames(xtsObj) = nameVec[x]
return(xtsObj)
})
Output:
#Use Reduce function to merge the list output from previous step
tsAggregate = Reduce(function(x,y) merge.xts(x,y),tsList)
head(tsAggregate,10)
# Qtr1 Qtr2 Qtr3 Qtr4
#1990 Q1 100.00 NA NA NA
#1990 Q2 NA 91.95 NA NA
#1991 Q1 102.09 NA NA NA
#1991 Q2 NA 92.77 NA NA
#1991 Q3 NA NA 97.57 NA
#1992 Q1 102.50 NA NA NA
#1992 Q2 NA 92.59 NA NA
#1992 Q3 NA NA 98.43 NA
#1992 Q4 NA NA NA 100.21
#1993 Q1 102.60 NA NA NA

Resources