as.Date在以周为基础的日期序列中产生意外结果 [英] as.Date produces unexpected result in a sequence of week-based dates

查看:106
本文介绍了as.Date在以周为基础的日期序列中产生意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将基于星期的日期转换为基于月的日期.

I am working on the transformation of week based dates to month based dates.

在检查我的工作时,我在数据中发现以下问题,这是由于简单调用as.Date()

When checking my work, I found the following problem in my data which is the result of a simple call to as.Date()

as.Date("2016-50-4", format = "%Y-%U-%u")
as.Date("2016-50-5", format = "%Y-%U-%u")
as.Date("2016-50-6", format = "%Y-%U-%u")
as.Date("2016-50-7", format = "%Y-%U-%u") # this is the problem

前面的代码在前三行中产生正确的日期:

The previous code yields correct date for the first 3 lines:

"2016-12-15"
"2016-12-16"
"2016-12-17"  

最后一行代码可以追溯到1周:

The last line of code however, goes back 1 week:

 "2016-12-11"

任何人都可以解释这里发生了什么吗?

Can anybody explain what is happening here?

推荐答案

使用一年中的一周可能会变得非常棘手.您可以尝试使用ISOweek包转换日期:

Working with week of the year can become very tricky. You may try to convert the dates using the ISOweek package:

# create date strings in the format given by the OP
wd <- c("2016-50-4","2016-50-5","2016-50-6","2016-50-7", "2016-51-1", "2016-52-7")
# convert to "normal" dates
ISOweek::ISOweek2date(stringr::str_replace(wd, "-", "-W"))

结果

#[1] "2016-12-15" "2016-12-16" "2016-12-17" "2016-12-18" "2016-12-19" "2017-01-01"

属于类Date.

请注意,基于ISO周的日期格式为yyyy-Www-d,其中大写字母W在周号之前.要将其与标准的基于月的日期格式yyyy-mm-dd区别开来是必需的.

Note that the ISO week-based date format is yyyy-Www-d with a capital W preceeding the week number. This is required to distinguish it from the standard month-based date format yyyy-mm-dd.

因此,为了使用ISOweek2date()转换OP提供的日期字符串,有必要在第一个连字符后插入W,这是通过在每个字符串中用-W替换第一个-来实现的

So, in order to convert the date strings provided by the OP using ISOweek2date() it is necessary to insert a W after the first hyphen which is accomplished by replacing the first - by -W in each string.

还请注意,ISO星期从星期一开始,并且星期几从1到7编号.属于ISO星期的年份可能与日历年不同.从上面的示例日期中可以看到这一点,其中基于星期的日期2016-W52-7转换为2017-01-01.

Also note that ISO weeks start on Monday and the days of the week are numbered 1 to 7. The year which belongs to an ISO week may differ from the calendar year. This can be seen from the sample dates above where the week-based date 2016-W52-7 is converted to 2017-01-01.

早在2011年,%G%g%u%V格式规范在Windows版本的R中不适用于strptime().这很烦人,因为我必须每周准备报告,包括每周比较.我花了几个小时找到解决ISO周,ISO工作日和ISO年的解决方案.最后,我最终创建了ISOweek程序包,并在CRAN上发布.如今,该软件包仍然有其优点,因为上述格式在输入时被忽略(有关详细信息,请参见?strptime).

Back in 2011, the %G, %g, %u, and %V format specifications weren't available to strptime() in the Windows version of R. This was annoying as I had to prepare weekly reports including week-on-week comparisons. I spent hours to find a solution for dealing with ISO weeks, ISO weekdays, and ISO years. Finally, I ended up creating the ISOweek package and publishing it on CRAN. Today, the package still has its merits as the aforementioned formats are ignored on input (see ?strptime for details).

这篇关于as.Date在以周为基础的日期序列中产生意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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