时间:2017-08-17 23:53:38

标签: r lubridate

我有一系列约会,我正试图获得一年中的一周。这似乎是无辜的。

Order_Date = c("2017-06-10","2017-06-11","2017-06-12","2017-06-13",
               "2017-06-14","2017-06-15","2017-06-16")
strftime(as.POSIXlt(Order_Date) ,format="%W")
week(Order_Date)

06/10是星期六,因此06/10和06/11不可能是同一周。

关于如何获得这个的任何想法?

1 个答案:

答案 0 :(得分:2)

使用日期时间并不总是很简单。您的问题和SO成员的评论的说明。我加了几天才能说明问题。

library(lubridate)
Order_Date = c("2017-06-10","2017-06-11","2017-06-12","2017-06-13",
               "2017-06-14","2017-06-15","2017-06-16", "2017-06-17",
               "2017-06-18", "2017-06-19")
strftime(as.POSIXlt(Order_Date) ,format="%W") # week starts on Monday
#  [1] "23" "23" "24" "24" "24" "24" "24" "24" "24" "25"

strftime(as.POSIXlt(Order_Date) ,format="%U") # week starts on Sunday
#  [1] "23" "24" "24" "24" "24" "24" "24" "24" "25" "25"

week(Order_Date) # week starts on Sunday
#  [1] 23 24 24 24 24 24 24 24 25 25

在一个相关问题:here中,我使用了@UweBlock的ISOweek软件包来解决经过大量测试后年终问题。这是代码:

library(ISOweek)
library(stringr)
str_replace(ISOweek(Order_Date), "201.-W", "")  # week starts on Monday (and follows ISO 8601)
#  [1] "23" "23" "24" "24" "24" "24" "24" "24" "24" "25"

您可以使用?ISOweek获取更多信息。

请告诉我这是否是你想要的。