1

From my inputs, which is numeric format and represent the year and the week number, I need to create a sequence, from one input to the other.

Inputs example :

input.from <- 202144
input.to <- 202208

Desired output would be :

c(202144:202152, 202201:202208)

According to me, it is a little more complex, because of these constraints :

  • Years with 53 weeks : I tried lubridate::isoweek(), the %W or %v format, ...
  • Always keep two digits for the week : I tried "%02d", ...

I also tried to convert my input to date, ... Anyway, many attemps without success to create my function.

Thanks for your help !

Discus23
  • 471
  • 2
  • 11
  • Is there a reason you're choosing to encode your year-week as a number instead more properly as a `Date` object? – r2evans Sep 28 '21 at 14:25
  • My function will possibly be integrated in processes (out of R) where historically, the users' input are in this format (used in file names, etc ...) but I imagine that nothing prevents me from transforming my input in date object, but I couldn't do it either ... – Discus23 Sep 28 '21 at 14:39

3 Answers3

1

In case it would be useful to someone one day, here is finally the function I wrote, which respects ISO 8601 :

library(ISOweek)

foo <- function(pdeb, pfin) {
  from <- ISOweek::ISOweek2date(paste0(substr(pdeb, 1, 4), "-W", substr(pdeb, 5, 6), "-1"))
  to <- ISOweek::ISOweek2date(paste0(substr(pfin, 1, 4), "-W", substr(pfin, 5, 6), "-1"))
  res <- seq.Date(from, to, by = "week")
  return(format(res, format = "%G%V"))
}

foo(201950, 202205)

Step #1 : tranform input to character : YYYY-"W"WW-1

Step #2 : capture the ISOweek

Step #3 : sequence by week

Step #4 : return the sequence to the format "%G%V", still to respect ISO 8601 and YYYYWW

Discus23
  • 471
  • 2
  • 11
0

I'd go with

x <- c("202144", "202208")
out <- do.call(seq, c(as.list(as.Date(paste0(x, "1"), format="%Y%U%u")), by = "week"))
out
#  [1] "2021-11-01" "2021-11-08" "2021-11-15" "2021-11-22" "2021-11-29" "2021-12-06" "2021-12-13" "2021-12-20" "2021-12-27"
# [10] "2022-01-03" "2022-01-10" "2022-01-17" "2022-01-24" "2022-01-31" "2022-02-07" "2022-02-14" "2022-02-21"

If you really want to keep them in the %Y%W format, then

format(out, format = "%Y%W")
#  [1] "202144" "202145" "202146" "202147" "202148" "202149" "202150" "202151" "202152" "202201" "202202" "202203" "202204"
# [14] "202205" "202206" "202207" "202208"

(This answer heavily informed by Transform year/week to date object)

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • It seems great but with the year 2020 ```x <- c("202040", "202208")```, 202053 is missing. I got it with ```format(out, format = "%Y%V")``` but it makes me lose 202040... (Yes for the link, I had read it and I am learning from it) – Discus23 Sep 28 '21 at 15:46
0

We could do some mathematics.

f <- function(from, to) {
  r <- from:to
  r[r %% 100 > 0 & r %% 100 < 53]
}

input.from <- 202144; input.to <- 202208
f(input.from, input.to)
# [1] 202144 202145 202146 202147 202148 202149 202150 202151 202152
# [10] 202201 202202 202203 202204 202205 202206 202207 202208
jay.sf
  • 60,139
  • 8
  • 53
  • 110