将周数转换为日期不断将周数添加到今天的日期

Convert week number to date keeps adding week number to today's date

抱歉,已多次询问此问题,但已发布的回复中 none 对我有用。我正在尝试使用 as.Date 函数将周数转换为日期或最好是月份名称。但是,无论我投入哪个星期,我都会得到相同的结果,我只是想不通为什么。感谢任何帮助

```R
as.Date(x = 03, format = "%W",origin = "2019-01-01")
# This results in "2019-09-16"

as.Date(x = 4, format = "%W",origin = "2019-01-01")
# This results in "2019-09-17"

as.Date(x = 12, format = "%w",origin = "2019-01-01")
# This results in "2019-09-25"

as.Date(x = 12, format = "%W",origin = "2018-01-01")
# This results in "2019-09-25"
```

即使我更改年份,它也会产生相同的输出。它似乎将今天的日期(13 日)添加到周数。最令人沮丧的是,这是一个旧代码,截至昨天

,它一直运行良好

来自帮助:"If the date string does not specify the date completely, the returned answer may be system-specific. The most common behavior is to assume that a missing year, month or day is the current one."

解决这个问题的最简单方法是指定日期然后转换:

 as.Date(x = paste(3, "0"), format = "%W %w")
[1] "2019-01-15"

 as.Date(x = paste(10, "0"), format = "%W %w")
[1] "2019-03-04"

如果您更改当前年份,您还需要指定年份。

as.Date(x = paste(10, "0 2018"), format = "%W %w %Y")
[1] "2018-03-04"

您可以在向量中替换上面示例中的 3 或 10。