Hi,
I have sequential days:
seq_days=seq(from=as.Date('2019-12-28'), to=as.Date('2020-12-25'), by = "1 day")
I want to repeat each consecutive 7 days 6 times.
For example:
2019-12-28
2019-12-29
2019-12-30
2019-12-31
2020-01-01
2020-01-02
2020-01-03
2019-12-28
2019-12-29
2019-12-30
2019-12-31
2020-01-01
2020-01-02
2020-01-03
2019-12-28
2019-12-29
2019-12-30
2019-12-31
2020-01-01
2020-01-02
2020-01-03
...
rep function does not work because I don't want to rep each row. I just want to rep all 7 consecutive dates.
seq_days=as.data.frame(seq_days) %>%
slice(rep(1:n(), each = 6))
Thank you!
SOLUTION:
a=-6
b=0
repted_days=NULL
for (i in 1:52){
a=a+7
b=b+7
rep=rep(seq_days[a:b,],6)
repted_days=rbind(repted_days,data.frame(rep))
}
DavoWW
2
Hi @atakzltn ,
You can achieve your required output via base
or dplyr
, without resorting to a for()
loop:
suppressPackageStartupMessages(library(tidyverse))
# Sequence of 7 days
(seq_days <- seq(from = as.Date("2019-12-28"),
to = as.Date("2020-01-03"),
by = "1 day"))
#> [1] "2019-12-28" "2019-12-29" "2019-12-30" "2019-12-31" "2020-01-01"
#> [6] "2020-01-02" "2020-01-03"
# Repeat sequence (end-on-end)
(rep(seq_days, times=6))
#> [1] "2019-12-28" "2019-12-29" "2019-12-30" "2019-12-31" "2020-01-01"
#> [6] "2020-01-02" "2020-01-03" "2019-12-28" "2019-12-29" "2019-12-30"
#> [11] "2019-12-31" "2020-01-01" "2020-01-02" "2020-01-03" "2019-12-28"
#> [16] "2019-12-29" "2019-12-30" "2019-12-31" "2020-01-01" "2020-01-02"
#> [21] "2020-01-03" "2019-12-28" "2019-12-29" "2019-12-30" "2019-12-31"
#> [26] "2020-01-01" "2020-01-02" "2020-01-03" "2019-12-28" "2019-12-29"
#> [31] "2019-12-30" "2019-12-31" "2020-01-01" "2020-01-02" "2020-01-03"
#> [36] "2019-12-28" "2019-12-29" "2019-12-30" "2019-12-31" "2020-01-01"
#> [41] "2020-01-02" "2020-01-03"
# Repeat sequence (day-by-day)
(rep(seq_days, each=3))
#> [1] "2019-12-28" "2019-12-28" "2019-12-28" "2019-12-29" "2019-12-29"
#> [6] "2019-12-29" "2019-12-30" "2019-12-30" "2019-12-30" "2019-12-31"
#> [11] "2019-12-31" "2019-12-31" "2020-01-01" "2020-01-01" "2020-01-01"
#> [16] "2020-01-02" "2020-01-02" "2020-01-02" "2020-01-03" "2020-01-03"
#> [21] "2020-01-03"
# dplyr method
seq_days %>%
as.data.frame() %>%
slice(rep(1:n(), times = 6))
#> .
#> 1 2019-12-28
#> 2 2019-12-29
#> 3 2019-12-30
#> 4 2019-12-31
#> 5 2020-01-01
#> 6 2020-01-02
#> 7 2020-01-03
#> 8 2019-12-28
#> 9 2019-12-29
#> 10 2019-12-30
#> 11 2019-12-31
#> 12 2020-01-01
#> 13 2020-01-02
#> 14 2020-01-03
#> 15 2019-12-28
#> 16 2019-12-29
#> 17 2019-12-30
#> 18 2019-12-31
#> 19 2020-01-01
#> 20 2020-01-02
#> 21 2020-01-03
#> 22 2019-12-28
#> 23 2019-12-29
#> 24 2019-12-30
#> 25 2019-12-31
#> 26 2020-01-01
#> 27 2020-01-02
#> 28 2020-01-03
#> 29 2019-12-28
#> 30 2019-12-29
#> 31 2019-12-30
#> 32 2019-12-31
#> 33 2020-01-01
#> 34 2020-01-02
#> 35 2020-01-03
#> 36 2019-12-28
#> 37 2019-12-29
#> 38 2019-12-30
#> 39 2019-12-31
#> 40 2020-01-01
#> 41 2020-01-02
#> 42 2020-01-03
Created on 2021-07-28 by the reprex package (v2.0.0)
1 Like
system
Closed
3
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.