如何解读R中的年+周数? [英] How to Parse Year + Week Number in R?

查看:469
本文介绍了如何解读R中的年+周数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个很好的方法来获得一个年份+周数转换为R的日期?我已经尝试了以下内容:

 > as.POSIXct(2008 41,format =%Y%U)
[1]2008-02-21 EST
> as.POSIXct(2008 42,format =%Y%U)
[1]2008-02-21 EST

根据?strftime


%Y年份与世纪。请注意,虽然
原始公历日历中没有零,但ISO 8601:2004将其定义为有效
(解释为1BC):请参阅 http://en.wikipedia.org/wiki/0_(年)。注意
,该标准还表示,1582年之前的年历
应仅在有关各方的同意下使用。



%U Week的年份为十进制数(00-53),使用星期日作为
本周的第一天(通常与第一周的第1天的
的第一个星期日)。



解决方案

这有点像另一个问题你可能以前见过。 :)



关键问题是:每周应该指定几个星期?这是星期的第一天吗?最后?这太模糊了我不知道第一周是一年的第一天还是一年的第七天,或者可能是一年的第一个星期天或星期一(这是一个频繁的解读)。 (而且更糟糕的是:这些通常看起来是0索引的,而不是1索引)。因此,需要指定一周中的枚举日。



例如,尝试这样:

  as.POSIXlt(2008 42 1,format =%Y%U%u )

%u 指标指定



附加说明:有关格式转换的各种选项,请参阅?strptime 。要注意几周的计数,因为这些可以在年底分拆,而 1 的日期是不明确的:是根据星期日或星期一还是从一年的第一天开始?所有这些都应在运行R代码的不同系统上进行指定和测试。我不确定Windows和POSIX系统对这些转化中的某些转换有相同的调整,因此我将再次测试并测试。


Is there a good way to get a year + week number converted a date in R? I have tried the following:

> as.POSIXct("2008 41", format="%Y %U")
[1] "2008-02-21 EST"
> as.POSIXct("2008 42", format="%Y %U")
[1] "2008-02-21 EST"

According to ?strftime:

%Y Year with century. Note that whereas there was no zero in the original Gregorian calendar, ISO 8601:2004 defines it to be valid (interpreted as 1BC): see http://en.wikipedia.org/wiki/0_(year). Note that the standard also says that years before 1582 in its calendar should only be used with agreement of the parties involved.

%U Week of the year as decimal number (00–53) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention.

解决方案

This is kinda like another question you may have seen before. :)

The key issue is: what day should a week number specify? Is it the first day of the week? The last? That's ambiguous. I don't know if week one is the first day of the year or the 7th day of the year, or possibly the first Sunday or Monday of the year (which is a frequent interpretation). (And it's worse than that: these generally appear to be 0-indexed, rather than 1-indexed.) So, an enumerated day of the week needs to be specified.

For instance, try this:

as.POSIXlt("2008 42 1", format = "%Y %U %u")

The %u indicator specifies the day of the week.

Additional note: See ?strptime for the various options for format conversion. It's important to be careful about the enumeration of weeks, as these can be split across the end of the year, and day 1 is ambiguous: is it specified based on a Sunday or Monday, or from the first day of the year? This should all be specified and tested on the different systems where the R code will run. I'm not certain that Windows and POSIX systems sing the same tune on some of these conversions, hence I'd test and test again.

这篇关于如何解读R中的年+周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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