【问题标题】:makedate from lubridate with year, month, and week in R [duplicate]来自 lubridate 的 makedate 与 R 中的年、月和周 [重复]
【发布时间】:2017-09-13 20:28:31
【问题描述】:

我有包含年、月、周列的数据。如何将其转换为带有lubridate 的日期。文档中的示例使用年、月和日。

让我们假设以下数据

year month week
2017  1     1
2017  2     35
2017  3     50

我该如何使用。 makedate() 有以上数据吗?

【问题讨论】:

  • 您提供的信息相互矛盾。第 35 周永远不会在第 2 个月。请编辑您的输入数据。

标签: r date lubridate


【解决方案1】:

使用基础POSIXct 的潜在解决方案。

df$complete_date <- as.Date(paste(df$year, df$week, 1, sep="-"), "%Y-%U-%u")

这会产生预期的结果吗?

  year month week complete_date
1 2017     1    1 2017-01-02
2 2017     2   35 2017-08-28
3 2017     3   50 2017-12-11

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多