2016-10-10 35 views
0

如何从R脚本中的datetime找到week_of_month和week_of_year?如何查找从r脚本中的datetime开始的月份和年份

示例:日期时间2016-09-27 00:00:07.477week_of_month is 5week_of_year is 39

我从weekdays.Date("2016-09-27 00:00:07.477")越来越day_of month

同样我想要得到week_of_monthweek_of_year

+4

你读过吗? http://stackoverflow.com/questions/27116645/r-week-function-returns-unexpected-values和http://stackoverflow.com/questions/25199851/r-how-to-get-the-week-number-月份为 –

+0

从此链接获得解决方案http://stackoverflow.com/a/25202142/1448357 for'week_of_month。和格式化日期'%U'给出week_of_year。 – Gaurav

回答

5

你可以完成大部分与基础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> 
+0

它给出了“一年中的一周”。 '月份的星期几'是什么? – Gaurav

+0

也许'plt $ mday%/%7'?您可能需要针对一周开始的常见规则进行抵消或调整。 –