In addition to the solution by @andresrcs, you could also use the clock package, which has a built in type for ISO year-week. I haven't added a direct parser for that yet, but you can use stringr here too to work around that:
library(dplyr)
library(stringr)
library(clock)
df <- data.frame(week_iso = c("2022024", "2022025"))
df
#> week_iso
#> 1 2022024
#> 2 2022025
df <- df %>%
mutate(
year = as.integer(str_sub(week_iso, end = 4)),
week = as.integer(str_sub(week_iso, start = 5)),
yw = iso_year_week_day(year, week),
start = set_day(yw, 1),
end = set_day(yw, 7)
)
df
#> week_iso year week yw start end
#> 1 2022024 2022 24 2022-W24 2022-W24-1 2022-W24-7
#> 2 2022025 2022 25 2022-W25 2022-W25-1 2022-W25-7
df %>%
mutate(start = as.Date(start), end = as.Date(end))
#> week_iso year week yw start end
#> 1 2022024 2022 24 2022-W24 2022-06-13 2022-06-19
#> 2 2022025 2022 25 2022-W25 2022-06-20 2022-06-26
Thank you both for your solutions. I find them very helpful. Thanks again!
@andresrcs How will your code change if we work with different year-week data? For example, consider df <- data.frame(week_iso = c("2021024", "2022025"))