How to Parse Year + Week Number in R?

前端 未结 3 2061
天涯浪人
天涯浪人 2020-11-29 08:24

Is there a good way to get a year + week number converted a date in R? I have tried the following:

> as.POSIXct(\"2008 41\", format=\"%Y %U\")
[1] \"2008-         


        
3条回答
  •  孤街浪徒
    2020-11-29 08:58

    Day-of-week == zero in the POSIXlt DateTimesClasses system is Sunday. Not exactly Biblical and not in agreement with the R indexing that starts at "1" convention either, but that's what it is. Week zero is the first (partial) week in the year. Week one (but day of week zero) starts with the first Sunday. And all the other sequence types in POSIXlt have 0 as their starting point. It kind of interesting to see what coercing the list elements of POSIXlt objects do. The only way you can actually change a POSIXlt date is to alter the $year, the $mon or the $mday elements. The others seem to be epiphenomena.

      today <- as.POSIXlt(Sys.Date())
      today  # Tuesday
    #[1] "2012-02-21 UTC"
         today$wday <- 0  # attempt to make it Sunday
         today
    # [1] "2012-02-21 UTC"   The attempt fails
     today$mday <- 19
     today
    #[1] "2012-02-19 UTC"   Success
    

提交回复
热议问题