怎么把yearweek转换成yearmonthday? [英] How to convert from yearweek to yearmonthday?

查看:146
本文介绍了怎么把yearweek转换成yearmonthday?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个日期向量:

dput(date)

c("1981035", "1981036", "1981037", "1981038", "1981039", "1981040",
  "1981041", "1981042", "1981043", "1981044", "1981045", "1981046",
  "1981047", "1981048", "1981049", "1981050", "1981051", "1981052",
  "1982001", "1982002", "1982003", "1982004", "1982005", "1982006",
  "1982007", "1982008", "1982009", "1982010", "1982011", "1982012",
  "1982013", "1982014", "1982015", "1982016", "1982017", "1982018",
  "1982019", "1982020", "1982021", "1982022", "1982023", "1982024",
  "1982025", "1982026", "1982027", "1982028", "1982029", "1982030",
  "1982031", "1982032", "1982033", "1982034", "1982035", "1982036",
  "1982037")

以年周的形式给出[第1周涵盖当年的1至7 ]。我想将其转换为年月日格式,并尝试了以下操作,但没有成功:

It is given as yearweek [week 1 covers day-of-the-year 1 to 7]. I want to convert this to format year-month-day and tried the following, but it didn't work:

as.Date(date, "%Y%U")


推荐答案

您必须为其分配一周中的一天。否则,它不能转换为特定日期,因为它是指日期范围。使用%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')

这篇关于怎么把yearweek转换成yearmonthday?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆