interval for dates

I'm trying to use cut to get a date interval, using cut works, but it creates a factor which is the first day of the interval as it can be seen here:

> cut(tt$fecha_validacion, breaks = "3 days")
   [1] 2020-04-30 2020-04-21 2020-05-03 2020-05-03 2020-04-21 2020-04-27 2020-05-03 2020-04-30
   [9] 2020-03-31 2020-05-18 2020-03-19 2020-05-09 2020-04-30 2020-04-09 2020-05-09 2020-04-15
  [17] 2020-04-30 2020-04-15 2020-04-21 2020-05-12 2020-05-03 2020-04-24 2020-05-03 2020-05-06
  [25] 2020-05-18 2020-04-21 2020-04-24 2020-04-21 2020-04-30 2020-05-03 2020-05-15 2020-05-24
  [33] 2020-04-15 2020-05-03 2020-04-15 2020-04-15 2020-03-31 2020-05-12 2020-04-27 2020-04-06
  [41] 2020-04-21 2020-05-12 2020-04-21 2020-04-06 2020-04-30 2020-05-06 2020-04-24 2020-04-30
  [49] 2020-05-12 2020-05-21 2020-05-12 2020-04-18 2020-05-12 2020-04-27 2020-03-19 2020-04-21
  [57] 2020-05-21 2020-05-03 2020-04-27 2020-04-09 2020-04-24 2020-05-12 2020-04-27 2020-05-06
  [65] 2020-04-18 2020-04-15 2020-04-21 2020-04-03 2020-05-18 2020-05-03 2020-04-30 2020-05-27
  [73] 2020-05-15 2020-05-12 2020-04-06 2020-04-15 2020-05-18 2020-05-15 2020-04-21 2020-04-18
  [81] 2020-03-25 2020-04-30 2020-05-18 2020-05-15 2020-04-18 2020-05-18 2020-04-27 2020-05-09
  [89] 2020-05-15 2020-05-12 2020-03-19 2020-05-15 2020-04-30 2020-05-18 2020-04-21 2020-04-12
  [97] 2020-03-25 2020-05-06 2020-04-03 2020-05-18 2020-03-31 2020-05-03 2020-04-18 2020-05-12
 [105] 2020-05-09 2020-05-06 2020-05-18 2020-05-03 2020-05-06 2020-05-03 2020-04-24 2020-04-27
 [113] 2020-05-09 2020-03-28 2020-05-06 2020-05-15 2020-04-21 2020-05-09 2020-04-21 2020-05-24
 [121] 2020-05-03 2020-05-18 2020-04-12 2020-04-12 2020-04-06 2020-05-03 2020-05-06 2020-04-06
 [129] 2020-04-24 2020-04-15 2020-04-24 2020-04-21 2020-05-12 2020-04-27 2020-04-27 2020-04-09
 [137] 2020-04-21 2020-04-18 2020-03-19 2020-05-06 2020-05-18 2020-04-12 2020-04-15 2020-05-12
 [145] 2020-05-18 2020-05-18 2020-05-06 2020-04-06 2020-05-12 2020-05-12 2020-04-21 2020-04-15
 [153] 2020-05-12 2020-05-06 2020-04-15 2020-05-18 2020-05-06 2020-04-18 2020-04-24 2020-05-12
 [161] 2020-05-15 2020-04-24 2020-04-15 2020-04-27 2020-04-15 2020-05-03 2020-03-25 2020-04-24
 [169] 2020-05-03 2020-04-12 2020-03-31 2020-05-12 2020-04-24 2020-04-03 2020-03-28 2020-04-30
 [177] 2020-05-15 2020-04-24 2020-05-15 2020-05-03 2020-05-18 2020-05-15 2020-04-15 2020-05-12
 [185] 2020-03-25 2020-04-06 2020-05-03 2020-05-03 2020-03-22 2020-05-15 2020-03-19 2020-05-12
 [193] 2020-05-03 2020-05-18 2020-05-09 2020-03-25 2020-04-24 2020-05-12 2020-03-28 2020-05-24
 [201] 2020-03-22 2020-05-15 2020-04-30 2020-04-24 2020-05-09 2020-05-09 2020-04-30 2020-04-27
 [209] 2020-04-15 2020-04-09 2020-04-18 2020-04-15 2020-04-30 2020-05-12 2020-03-28 2020-05-21
 [217] 2020-05-12 2020-04-09 2020-05-03 2020-03-25 2020-05-12 2020-04-27 2020-05-18 2020-05-06
 [225] 2020-04-15 2020-04-27 2020-04-30 2020-05-12 2020-04-09 2020-05-18 2020-03-19 2020-05-12
 [233] 2020-04-24 2020-05-15 2020-04-24 2020-04-21 2020-05-12 2020-04-27 2020-04-24 2020-05-12
 [241] 2020-04-06 2020-05-18 2020-05-12 2020-05-12 2020-03-22 2020-04-21 2020-05-06 2020-05-03
 [249] 2020-05-18 2020-04-27 2020-05-12 2020-04-21 2020-05-18 2020-05-03 2020-04-09 2020-05-06
 [257] 2020-05-06 2020-05-12 2020-04-18 2020-04-27 2020-05-12 2020-04-24 2020-03-28 2020-05-06
 [265] 2020-03-19 2020-03-19 2020-05-15 2020-04-09 2020-05-09 2020-04-18 2020-04-30 2020-04-21
 [273] 2020-05-09 2020-03-28 2020-05-06 2020-04-30 2020-05-09 2020-03-28 2020-05-09 2020-03-22
 [281] 2020-04-06 2020-03-25 2020-04-09 2020-05-03 2020-05-12 2020-05-03 2020-05-18 2020-04-27
 [289] 2020-05-18 2020-05-12 2020-04-09 2020-04-24 2020-05-06 2020-03-25 2020-04-30 2020-05-06
 [297] 2020-04-18 2020-05-06 2020-04-24 2020-04-27 2020-05-12 2020-04-09 2020-05-03 2020-04-21
 [305] 2020-03-31 2020-05-09 2020-03-31 2020-05-09 2020-04-30 2020-04-27 2020-04-15 2020-05-06
 [313] 2020-05-06 2020-04-27 2020-03-16 2020-04-24 2020-03-28 2020-05-09 2020-04-30 2020-05-06
 [321] 2020-05-06 2020-04-24 2020-04-15 2020-05-27 2020-04-30 2020-05-12 2020-04-03 2020-04-27
 [329] 2020-05-09 2020-04-09 2020-04-15 2020-04-21 2020-04-18 2020-04-09 2020-04-12 2020-03-16
 [337] 2020-05-12 2020-05-18 2020-04-24 2020-05-09 2020-05-12 2020-04-21 2020-03-31 2020-05-12
 [345] 2020-05-06 2020-05-06 2020-04-30 2020-05-09 2020-04-09 2020-05-12 2020-04-21 2020-04-09
 [353] 2020-05-15 2020-05-12 2020-05-18 2020-05-09 2020-05-12 2020-05-15 2020-05-12 2020-04-21
 [361] 2020-04-21 2020-05-06 2020-04-12 2020-03-25 2020-05-03 2020-04-30 2020-03-28 2020-04-24
 [369] 2020-05-06 2020-04-21 2020-04-24 2020-04-15 2020-03-16 2020-04-09 2020-04-15 2020-05-12
 [377] 2020-04-30 2020-05-09 2020-04-12 2020-05-12 2020-04-15 2020-04-06 2020-05-15 2020-04-15
 [385] 2020-04-30 2020-05-09 2020-04-30 2020-05-12 2020-04-21 2020-03-28 2020-05-06 2020-05-18
 [393] 2020-05-09 2020-04-30 2020-05-18 2020-04-24 2020-05-03 2020-04-09 2020-04-27 2020-04-18
 [401] 2020-05-09 2020-04-18 2020-05-18 2020-03-25 2020-04-30 2020-05-15 2020-03-25 2020-05-03
 [409] 2020-04-06 2020-04-24 2020-05-15 2020-04-06 2020-04-30 2020-05-15 2020-05-15 2020-05-03
 [417] 2020-03-13 2020-05-12 2020-05-09 2020-05-15 2020-05-06 2020-04-21 2020-05-15 2020-03-19
 [425] 2020-03-19 2020-04-24 2020-04-18 2020-04-21 2020-03-25 2020-04-24 2020-05-21 2020-05-24
 [433] 2020-04-03 2020-03-25 2020-05-03 2020-05-09 2020-05-06 2020-04-24 2020-03-16 2020-05-12
 [441] 2020-05-12 2020-04-24 2020-03-22 2020-04-30 2020-03-19 2020-04-21 2020-04-15 2020-04-15
 [449] 2020-04-21 2020-04-15 2020-04-03 2020-04-30 2020-03-16 2020-05-15 2020-04-15 2020-04-06
 [457] 2020-04-21 2020-05-06 2020-05-09 2020-05-09 2020-05-09 2020-05-18 2020-05-15 2020-04-15
 [465] 2020-04-06 2020-05-03 2020-05-18 2020-05-06 2020-05-03 2020-04-09 2020-05-18 2020-05-18
 [473] 2020-04-21 2020-04-27 2020-03-19 2020-04-24 2020-03-19 2020-05-03 2020-04-27 2020-04-03
 [481] 2020-03-25 2020-04-21 2020-04-21 2020-04-18 2020-03-22 2020-04-06 2020-05-06 2020-03-28
 [489] 2020-05-18 2020-05-03 2020-04-27 2020-04-03 2020-05-12 2020-03-16 2020-03-31 2020-05-06
 [497] 2020-03-31 2020-05-18 2020-05-09 2020-05-09 2020-05-09 2020-05-09 2020-04-15 2020-05-03
 [505] 2020-04-24 2020-04-15 2020-05-12 2020-04-30 2020-03-19 2020-03-28 2020-05-06 2020-04-27
 [513] 2020-04-24 2020-03-31 2020-04-30 2020-04-18 2020-05-15 2020-05-06 2020-04-27 2020-05-06
 [521] 2020-04-30 2020-04-24 2020-05-15 2020-05-18 2020-05-12 2020-04-24 2020-04-21 2020-05-06
 [529] 2020-05-03 2020-05-18 2020-05-18 2020-04-27 2020-05-18 2020-03-31 2020-05-15 2020-04-15
 [537] 2020-04-24 2020-03-28 2020-03-31 2020-05-06 2020-05-06 2020-03-28 2020-05-18 2020-05-06
 [545] 2020-04-27 2020-04-15 2020-04-03 2020-04-15 2020-04-30 2020-05-09 2020-04-21 2020-04-24
 [553] 2020-05-18 2020-04-06 2020-05-15 2020-05-03 2020-04-30 2020-04-30 2020-04-24 2020-04-06
 [561] 2020-05-09 2020-04-30 2020-04-30 2020-05-03 2020-04-15 2020-04-09 2020-05-03 2020-04-24
 [569] 2020-05-12 2020-05-15 2020-04-03 2020-05-12 2020-05-12 2020-05-12 2020-04-21 2020-03-28
 [577] 2020-03-31 2020-05-18 2020-04-15 2020-03-19 2020-04-24 2020-04-18 2020-04-06 2020-05-03
 [585] 2020-05-06 2020-05-03 2020-05-12 2020-04-24 2020-04-24 2020-03-25 2020-05-15 2020-05-03
 [593] 2020-05-21 2020-05-18 2020-05-06 2020-03-19 2020-05-15 2020-04-18 2020-05-12 2020-03-25
 [601] 2020-04-06 2020-05-03 2020-04-06 2020-05-06 2020-04-06 2020-05-12 2020-04-21 2020-03-13
 [609] 2020-03-28 2020-04-03 2020-04-30 2020-05-09 2020-04-15 2020-04-21 2020-05-21 2020-05-15
 [617] 2020-03-19 2020-05-06 2020-05-06 2020-04-27 2020-04-24 2020-05-15 2020-05-15 2020-05-18
 [625] 2020-05-18 2020-05-15 2020-04-12 2020-04-15 2020-05-09 2020-03-19 2020-05-09 2020-04-03
 [633] 2020-05-03 2020-05-15 2020-04-18 2020-04-30 2020-04-21 2020-03-25 2020-04-12 2020-04-24
 [641] 2020-03-22 2020-05-12 2020-03-19 2020-05-15 2020-04-30 2020-04-15 2020-04-03 2020-04-30
 [649] 2020-04-18 2020-04-21 2020-05-06 2020-03-16 2020-04-06 2020-05-12 2020-05-09 2020-04-09
 [657] 2020-05-18 2020-03-19 2020-05-12 2020-05-12 2020-05-12 2020-05-12 2020-05-03 2020-05-06
 [665] 2020-05-21 2020-05-03 2020-04-21 2020-04-18 2020-05-09 2020-05-15 2020-03-31 2020-05-09
 [673] 2020-05-15 2020-04-24 2020-05-18 2020-04-21 2020-05-15 2020-04-24 2020-05-06 2020-05-06
 [681] 2020-05-24 2020-04-09 2020-04-18 2020-05-12 2020-05-18 2020-05-12 2020-04-06 2020-05-12
 [689] 2020-04-30 2020-04-24 2020-04-24 2020-05-06 2020-05-06 2020-05-18 2020-05-12 2020-05-15
 [697] 2020-04-24 2020-05-12 2020-05-12 2020-05-15 2020-05-12 2020-05-12 2020-04-09 2020-04-09
 [705] 2020-05-18 2020-05-06 2020-04-30 2020-05-09 2020-04-24 2020-05-18 2020-04-21 2020-05-09
 [713] 2020-05-15 2020-04-18 2020-05-09 2020-03-19 2020-04-21 2020-05-06 2020-05-15 2020-05-06
 [721] 2020-03-22 2020-03-25 2020-05-09 2020-05-06 2020-03-22 2020-04-30 2020-05-03 2020-05-09
 [729] 2020-05-12 2020-05-12 2020-05-18 2020-04-15 2020-05-21 2020-04-03 2020-04-30 2020-05-09
 [737] 2020-05-18 2020-05-03 2020-04-24 2020-05-21 2020-04-24 2020-05-03 2020-05-12 2020-04-24
 [745] 2020-04-18 2020-04-24 2020-05-06 2020-05-12 2020-04-03 2020-04-09 2020-04-24 2020-04-24
 [753] 2020-04-18 2020-05-06 2020-04-21 2020-05-15 2020-03-25 2020-04-06 2020-05-06 2020-04-06
 [761] 2020-05-18 2020-04-27 2020-04-27 2020-04-18 2020-05-15 2020-05-12 2020-05-24 2020-05-12
 [769] 2020-05-09 2020-04-21 2020-04-15 2020-04-21 2020-04-27 2020-05-21 2020-04-24 2020-04-21
 [777] 2020-04-18 2020-04-24 2020-04-27 2020-04-12 2020-05-24 2020-04-21 2020-05-18 2020-04-30
 [785] 2020-04-30 2020-04-21 2020-05-15 2020-04-18 2020-04-09 2020-03-28 2020-05-06 2020-03-28
 [793] 2020-05-18 2020-05-06 2020-04-24 2020-05-18 2020-04-21 2020-05-09 2020-05-03 2020-05-18
 [801] 2020-05-06 2020-05-12 2020-04-21 2020-05-18 2020-04-21 2020-05-12 2020-04-24 2020-04-24
 [809] 2020-05-24 2020-05-03 2020-04-15 2020-05-15 2020-04-21 2020-04-06 2020-03-25 2020-05-15
 [817] 2020-04-15 2020-04-27 2020-04-21 2020-04-24 2020-05-21 2020-05-21 2020-05-15 2020-04-03
 [825] 2020-05-03 2020-05-15 2020-04-21 2020-04-27 2020-05-15 2020-05-18 2020-05-18 2020-04-24
 [833] 2020-03-25 2020-04-24 2020-04-30 2020-04-21 2020-05-15 2020-05-06 2020-05-09 2020-04-30
 [841] 2020-03-28 2020-05-18 2020-05-24 2020-04-27 2020-05-12 2020-04-09 2020-04-24 2020-03-19
 [849] 2020-05-09 2020-03-28 2020-04-30 2020-05-06 2020-05-15 2020-05-03 2020-04-21 2020-04-24
 [857] 2020-04-18 2020-03-25 2020-03-31 2020-05-03 2020-05-03 2020-05-09 2020-05-12 2020-04-12
 [865] 2020-04-27 2020-04-21 2020-03-31 2020-04-24 2020-04-24 2020-04-21 2020-04-24 2020-05-06
 [873] 2020-04-15 2020-04-24 2020-05-09 2020-04-24 2020-04-24 2020-04-24 2020-05-18 2020-04-24
 [881] 2020-05-15 2020-04-24 2020-04-24 2020-04-21 2020-05-03 2020-04-24 2020-04-24 2020-04-30
 [889] 2020-05-09 2020-03-25 2020-04-21 2020-04-03 2020-04-24 2020-03-28 2020-04-24 2020-04-21
 [897] 2020-04-21 2020-04-09 2020-04-21 2020-04-24 2020-04-21 2020-04-03 2020-04-24 2020-04-18
 [905] 2020-04-24 2020-04-21 2020-04-21 2020-04-24 2020-04-24 2020-04-24 2020-03-28 2020-04-24
 [913] 2020-05-06 2020-03-28 2020-04-09 2020-04-21 2020-04-24 2020-05-18 2020-04-24 2020-04-15
 [921] 2020-04-24 2020-04-24 2020-03-25 2020-04-24 2020-04-21 2020-04-09 2020-04-30 2020-03-19
 [929] 2020-04-24 2020-05-18 2020-04-21 2020-04-24 2020-05-21 2020-05-09 2020-04-12 2020-04-27
 [937] 2020-04-24 2020-04-24 2020-04-21 2020-04-21 2020-04-24 2020-04-24 2020-05-15 2020-04-24
 [945] 2020-04-21 2020-04-27 2020-04-24 2020-04-18 2020-04-21 2020-04-21 2020-04-21 2020-04-24
 [953] 2020-05-15 2020-04-21 2020-04-24 2020-04-21 2020-05-09 2020-03-25 2020-05-06 2020-03-19
 [961] 2020-04-21 2020-04-24 2020-04-21 2020-05-12 2020-04-24 2020-04-21 2020-04-24 2020-04-12
 [969] 2020-04-24 2020-04-03 2020-04-21 2020-04-27 2020-04-21 2020-04-21 2020-04-15 2020-04-21
 [977] 2020-03-31 2020-04-24 2020-05-18 2020-04-24 2020-03-19 2020-05-03 2020-04-21 2020-04-24
 [985] 2020-04-21 2020-04-30 2020-03-31 2020-05-03 2020-04-27 2020-04-30 2020-04-21 2020-04-24
 [993] 2020-04-24 2020-04-21 2020-04-21 2020-04-24 2020-03-28 2020-04-21 2020-04-24 2020-04-30
 [ reached getOption("max.print") -- omitted 3505 entries ]
80 Levels: 2020-03-13 2020-03-16 2020-03-19 2020-03-22 2020-03-25 2020-03-28 ... 2020-11-05

And then with ggplot looks like:

If I group in a similar way in a pivot table on excel I get a date range like this:

Is there any way using cut or any other function to have a factor created like start.date - end.date?

The cut function has labels parameter that allows you to make your own labels. There may be a more elegant way to do this but here is my method in this case.

Fechas <- seq.Date(from = as.Date("2020-03-13"), to = as.Date("2020-03-24"), by = 1)
Fechas
#>  [1] "2020-03-13" "2020-03-14" "2020-03-15" "2020-03-16" "2020-03-17"
#>  [6] "2020-03-18" "2020-03-19" "2020-03-20" "2020-03-21" "2020-03-22"
#> [11] "2020-03-23" "2020-03-24"

Labels1 <- seq.Date(from = as.Date("2020-03-13"), to = as.Date("2020-03-22"), by = 3)
Labels1 <- format(Labels1, "%d-%m-%Y")
Labels2 <- seq.Date(from = as.Date("2020-03-15"), to = as.Date("2020-03-24"), by = 3)
Labels2 <- format(Labels2, "%d-%m-%Y")
LabelsAll <- paste(Labels1, Labels2, sep = " - ")
LabelsAll
#> [1] "13-03-2020 - 15-03-2020" "16-03-2020 - 18-03-2020"
#> [3] "19-03-2020 - 21-03-2020" "22-03-2020 - 24-03-2020"
FechasFactor <- cut(Fechas, breaks = "3 days", labels = LabelsAll)
FechasFactor[1]
#> [1] 13-03-2020 - 15-03-2020
#> 4 Levels: 13-03-2020 - 15-03-2020 ... 22-03-2020 - 24-03-2020
FechasFactor[4]
#> [1] 16-03-2020 - 18-03-2020
#> 4 Levels: 13-03-2020 - 15-03-2020 ... 22-03-2020 - 24-03-2020

Created on 2020-06-29 by the reprex package (v0.3.0)

1 Like

Thanks a lot, I read the documentation multiple times and I couln't wrap my head around on how to use the label paramater. I always forgot that you can pass any element (even a function) as a parameter.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.