如何從R腳本中的datetime找到week_of_month和week_of_year?如何查找從r腳本中的datetime開始的月份和年份
示例:日期時間2016-09-27 00:00:07.477
,week_of_month is 5
和week_of_year is 39
我從weekdays.Date("2016-09-27 00:00:07.477")
越來越day_of month
。
同樣我想要得到week_of_month
和week_of_year
。
如何從R腳本中的datetime找到week_of_month和week_of_year?如何查找從r腳本中的datetime開始的月份和年份
示例:日期時間2016-09-27 00:00:07.477
,week_of_month is 5
和week_of_year is 39
我從weekdays.Date("2016-09-27 00:00:07.477")
越來越day_of month
。
同樣我想要得到week_of_month
和week_of_year
。
你可以完成大部分與基礎R:
R> dt <- as.POSIXct("2016-09-27 00:00:07.477") ## ISO8601 parsing
R> dt
[1] "2016-09-27 00:00:07.476 CDT"
R> plt <- as.POSIXlt(dt)
R> plt$mday # day of the month
[1] 27
R> plt$yday # day of the year
[1] 270
R> plt$wday # day of the week
[1] 2
R>
對於一些你需要看看help(strptime)
R> format(plt, "%U") # week of the year
[1] "39"
R>
R> format(plt, "%V") # alternate definition
[1] "39"
R>
它給出了「一年中的一週」。 '月份的星期幾'是什麼? – Gaurav
也許'plt $ mday%/%7'?您可能需要針對一週開始的常見規則進行抵消或調整。 –
你讀過嗎? http://stackoverflow.com/questions/27116645/r-week-function-returns-unexpected-values和http://stackoverflow.com/questions/25199851/r-how-to-get-the-week-number-月份爲 –
從此鏈接獲得解決方案http://stackoverflow.com/a/25202142/1448357 for'week_of_month。和格式化日期'%U'給出week_of_year。 – Gaurav