Programming Rhino
Programming Rhino

Reputation: 83

Weekly Dates in R

I am having troubles converting weekly to monthly dates, starting from a string which looks like this

tt <-  c("2009-24-1", "2001-10-1", "2000-16-1", "2013-18-1", "2015-21-1",
         "2015-53-1", "2009-53-1")

I was of the opinion that the correct format for this date should be format = "%Y-%V-%u" as the R documentation yields

%V
Week of the year as decimal number (01–53) as defined in ISO 8601.

Unfortunately, however, when I try to convert to date it does not work

as.Date(tt, format = "%Y-%V-%u", origin = "1970-01-01")

# "2009-02-21" "2001-02-21" "2000-02-21" "2013-02-21" "2015-02-21" "2015-02-21" "2009-02-21"

which is obviously incorrect. I then tried using %U

%U
Week of the year as decimal number (00–53) using Sunday as the first day 1 of the week 

and since all my dates are weekly dates that start on monday this is also not correct but at least yields somewhat correct dates with a few NAs.

as.Date(tt, format = "%Y-%U-%u", origin = "1970-01-01")

# "2009-06-15" "2001-03-12" "2000-04-17" "2013-05-06" "2015-05-25" NA           NA  

I appreciate any Ideas that could solve this problem (preferably without the use of packages).

Upvotes: 0

Views: 149

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269624

On the ?strptime help page it says that %V is Accepted but ignored on input . Instead use the ISOweek package to convert tt to Date class and then double check by converting back with the "%Y-%V-%u" format given in the question (which uses %V as output rather than input so it should work). Note that ISOweek2date expects that the week number is prefaced with a W so insert that using sub.

library(ISOweek)
date <- ISOweek2date(sub("-", "-W", tt))

# converting date back to ISOweek using format in question should give back tt
identical(format(date, "%Y-%V-%u"), tt)
## [1] TRUE

Upvotes: 1

jay.sf
jay.sf

Reputation: 72828

Using strptime reveals the issue.

strptime(tt, "%Y-%U-%u", tz="UTC")
# [1] "2009-06-15 UTC" "2001-03-12 UTC" "2000-04-17 UTC"
# [4] "2013-05-06 UTC" "2015-05-25 UTC" NA              
# [7] NA              
# Warning messages:
# 1: In strptime(tt, "%Y-%U-%u", tz = "UTC") :
#   (0-based) yday 368 in year 2015 is invalid
# 2: In strptime(tt, "%Y-%U-%u", tz = "UTC") :
#   (0-based) yday 368 in year 2009 is invalid

Upvotes: 0

Related Questions