[R] get start and end date of ISO weeks giving a date as input

Veronica Andreo veroandreo at gmail.com
Thu Sep 8 14:51:10 CEST 2016


Hello Luisfo and Enrico,

Thanks for your help! I've been testing both solutions... results differ
for the same date (I changed both functions to use ISO8601). And I added
contiguous dates, to see how they handle the start-end of the week.

So, here the results:

### one example
d <- c("2010-08-21","2010-08-22","2010-08-23","2010-08-24")
iso_start_end <- function(d) {
  d <- as.Date(d)
  wday <- as.POSIXlt(d)$wday
  data.frame(date = d,
             week = format(d, "%V"),
             starts = d - wday + 1,
             ends = d + 7 - wday)
}
iso_start_end(d)

        date week     starts       ends
1 2010-08-21   33 2010-08-16 2010-08-22
*2 2010-08-22   33 2010-08-23 2010-08-29*
3 2010-08-23   34 2010-08-23 2010-08-29
4 2010-08-24   34 2010-08-23 2010-08-29

### the other example:
dd <- as.Date(strptime('2010-08-21', format="%Y-%m-%d", tz="GMT"))
ref.date <- as.Date(strptime(paste0(year(dd),"-01-01"), format="%Y-%m-%d"))
bound.dates <- ref.date + 7 * (isoweek(dd)) + c(0,6)
bound.dates
[1] "2010-08-20" "2010-08-26"

So, researching a bit more and inspired by those examples, I eventually
came up with this solution that seems to work fine... I share in case that
any other has a similar problem:

# get ISOweek for my vector of dates
week_iso<-ISOweek(d)

# vector with the format %Y-W%V-1 for start day of the ISO week
week_iso_day1 <- paste(week_iso,1, sep="-")

#  vector with the format %Y-W%V-7 for end day of the ISO week
week_iso_day7 <- paste(week_iso, 7, sep="-")

# use ISOweek2date
data.frame(date= d, week_iso = week_iso, start =
ISOweek2date(week_iso_day1), end = ISOweek2date(week_iso_day7)

        date week_iso      start        end
1 2010-08-21 2010-W33 2010-08-16 2010-08-22
2 2010-08-22 2010-W33 2010-08-16 2010-08-22
3 2010-08-23 2010-W34 2010-08-23 2010-08-29
4 2010-08-24 2010-W34 2010-08-23 2010-08-29


Thanks again for your time, ideas and help!

Best,
Vero


2016-09-08 8:20 GMT-03:00 Luisfo <luisfo89 at yahoo.es>:

> Dear Veronica,
>
> Here there's a way of doing what you requested.
>
> library("lubridate")
> # your date '2010-08-21' as Date object
> dd <- as.Date(strptime("2010-08-21", format="%Y-%m-%d", tz="GMT"))
> # take the first day of the year as Date object, i.e. 2010-01-01 in our
> example
> ref.date <- as.Date(strptime(paste0(year(dd),"-01-01"),
> format="%Y-%m-%d", tz="GMT"))
> # the start and end dates
> bound.dates <- ref.date + 7 * (week(dd)-1) + c(0,6)
>
> I hope you find it useful.
>
> Best,
> *Luisfo Chiroque*
>
> *PhD Student | PhD Candidate IMDEA Networks Institute*
> http://fourier.networks.imdea.org/people/~luis_nunez/
>
> On 09/08/2016 12:13 PM, Veronica Andreo wrote:
>
> Hello list,
>
> Is there a quick way to get start and end date (%Y-%m-%d) from ISO
> weeks if I only have dates?
>
> For example, I have this date in which some event happened:
> "2010-08-21". Not only I want the ISO week, which I can obtain either
> with isoweek (lubridate) or ISOweek (ISOweek), but I want the start
> and end date of that ISO week.
>
> Do I need to print all ISO weeks from the period of interest and
> sample there for start and end date? Or is there a better way to do
> that?
>
> Thanks a lot in advance!
>
> Best,
> Veronica
>
> ______________________________________________R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, seehttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list