且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

怎么把yearweek转换成yearmonthday?

更新时间:2023-02-19 13:46:21

您必须为其分配一周中的一天。否则,它不能转换为特定日期,因为它是指日期范围。使用%w 选择第0天,即星期日,您可以使用下面的代码。

You have to assign a day of the week for them. Otherwise it cannot be converted to a specific date, since it refers to a range of dates. Choosing day 0 with %w, i.e. Sunday, you can use the code below.

as.Date(paste0(da, '0'), format = '%Y0%U%w')

注意:假定第五位不包含任何信息。

Note: This assumes the fifth digit contains no info. That seems odd to me, but is correct according to OP.

编辑:@Kath指出将数据视为%Y%可能更有意义w%U格式,因此您可以使用下面的简单代码实现相同的结果

@Kath pointed out it probably makes more sense to think of the data as being in %Y%w%U format, so you can achieve the same result with the simpler code below

as.Date(da, format = '%Y%w%U')