as.Date produces unexpected result in a sequence of week-based dates

前端 未结 3 1548
难免孤独
难免孤独 2020-12-11 16:55

I am working on the transformation of week based dates to month based dates.

When checking my work, I found the following problem in my data which is the result of a

3条回答
  •  不知归路
    2020-12-11 17:05

    As @lmo said in the comments, %u stands for the weekdays as a decimal number (1–7, with Monday as 1) and %U stands for the week of the year as decimal number (00–53) using Sunday as the first day. Thus, as.Date("2016-50-7", format = "%Y-%U-%u") will result in "2016-12-11".

    However, if that should give "2016-12-18", then you should use a week format that has also Monday as starting day. According to the documentation of ?strptime you would expect that the format "%Y-%V-%u" thus gives the correct output, where %V stands for the week of the year as decimal number (01–53) with monday as the first day.

    Unfortunately, it doesn't:

    > as.Date("2016-50-7", format = "%Y-%V-%u")
    [1] "2016-01-18"
    

    However, at the end of the explanation of %V it sais "Accepted but ignored on input" meaning that it won't work.

    You can circumvent this behavior as follows to get the correct dates:

    # create a vector of dates
    d <- c("2016-50-4","2016-50-5","2016-50-6","2016-50-7", "2016-51-1")
    
    # convert to the correct dates
    as.Date(paste0(substr(d,1,8), as.integer(substring(d,9))-1), "%Y-%U-%w") + 1
    

    which gives:

    [1] "2016-12-15" "2016-12-16" "2016-12-17" "2016-12-18" "2016-12-19"
    

提交回复
热议问题