Thursday, 15 May 2014

date - 53rd week of the year in R? -



date - 53rd week of the year in R? -

i have week-date info in form yyyy-ww wwis week number in 2 digits. info span 2007-01 2010-30. week counting convention iso 8601, can see here on wikipedia's "week number" article, reaches 53 weeks in year. illustration 2009 had 53 weeks system, see week numbers in this iso 8601 calendar. (see other years; per wikipedia article, 53rd weeks rare.)

basically want read week date in, convert date object , save separate column in data.frame. test, reconverted date objects yyyy-ww formats format([date-object], format = "%y-%w", , threw error @ 2009-53. week fails interpreted date r. odd, other years not have 53rd week (in iso 8601 standard) converted fine, such 2007-53, whereas other years not have 53rd week (in iso 8601 standard) fail, such 2008-53

the next minimal illustration demonstrates issue.

minimal example:

dates <- c("2009-50", "2009-51", "2009-52", "2009-53", "2010-01", "2010-02") as.date(x = paste(dates, 1), format = "%y-%w %w") # [1] "2009-12-14" "2009-12-21" "2009-12-28" na "2010-01-04" # [6] "2010-01-11" other.dates <- c("2007-53", "2008-53", "2009-53", "2010-53") as.date(x = paste(other.dates, 1), format = "%y-%w %w") # [1] "2007-12-31" na na na

the question is, how r take week numbers in iso 8601 format?

note: question summarises problem have been struggling few hours. have searched , found various helpful posts such this, none solved problem.

the bundle isoweek manages iso 8601 style week numberings, converting , date objects in r. see isoweek more. continuing illustration dates above, first need modify formatting bit. must in form yyyy-www-w rather yyyy-ww, i.e. 2009-w53-1. final digit identifies day of week utilize in identifying week, in case monday. week number must two-digit.

library(isoweek) dates <- c("2009-50", "2009-51", "2009-52", "2009-53", "2010-01", "2010-02") other.dates <- c("2007-53", "2008-53", "2009-53", "2010-53") dates <- sub("(\\d{4}-)(\\d{2})", "\\1w\\2-1", dates) other.dates <- sub("(\\d{4}-)(\\d{2})", "\\1w\\2-1", other.dates) ## check: dates # [1] "2009-w50-1" "2009-w51-1" "2009-w52-1" "2009-w53-1" "2010-w01-1" # [6] "2010-w02-1" (iso.date <- isoweek2date(dates)) # deal correctly # [1] "2009-12-07" "2009-12-14" "2009-12-21" "2009-12-28" "2010-01-04" # [6] "2010-01-11" (iso.other.date <- isoweek2date(other.dates)) # deals # [1] "2007-12-31" "2008-12-29" "2009-12-28" "2011-01-03" ## check back-conversion works: all(date2isoweek(iso.date) == dates) # [1] true ## not work others, since 53rd week of ## e.g. 2008 back-converted first week of 2009, in ## line iso 6801 standard. date2isoweek(iso.other.date) == other.dates # [1] false false true false

r date dayofweek

No comments:

Post a Comment