【问题标题】:How to find week of month and year from datetime in r script [duplicate]如何从r脚本中的日期时间查找月份和年份的星期[重复]
【发布时间】:2016-10-10 14:07:01
【问题描述】:

如何从 R 脚本中的日期时间找到 week_of_month 和 week_of_year ?

例如:日期时间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

【问题讨论】:

标签: r datetime


【解决方案1】:

您可以使用基本 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> 

【讨论】:

  • 它给出了week of the yearweek of the month 呢?
  • 也许是plt$mday %/% 7 ?您可能需要对有关一周开始的通用规则进行偏移或调整。
猜你喜欢
  • 2021-11-04
  • 2021-12-16
  • 1970-01-01
  • 2019-06-30
  • 2016-02-10
  • 2018-10-15
  • 2021-10-02
  • 1970-01-01
相关资源
最近更新 更多