将日历周转换为日期 [英] convert calendar weeks into daily dates
问题描述
我得到了一个包含两列的列表,即2015年的日历周和一个值:
I got a list with two columns, calendar weeks for 2015 and a value:
calender week Value
KW 1 14000
KW 2 24000
有 - 不幸的是 - 文件中没有更多信息我收到了。然而,我需要分析的是一个每日时间系列,因此我需要将KW 1等转换成一个具有相应日期的列(注意:日历周总是从星期一开始!):
There is - unfortunately - no more information in the file I received. What I need for analysis, though, is a daily time series, hence I need to convert KW 1 etc into a column with the respective dates (Note: the calendar week always starts with Monday!):
calender week date Value
KW 1 29-12-13 2000
KW 1 30-12-13 2000
KW 1 31-12-13 2000
KW 1 01-01-14 2000
KW 1 02-01-14 2000
KW 1 03-01-14 2000
KW 1 04-01-14 2000
KW 2 05-01-14 3000
KW 2 06-01-14 3000
KW 2 07-01-14 3000
KW 2 08-01-14 3000
KW 2 09-01-14 3000
KW 2 10-01-14 3000
KW 2 11-01-14 3000
并且该值简单地除以7(=日历周中的天数)。
And the value is simply devided by 7 (= number of days in a calendar week).
推荐答案
lubridate
帮你。
似乎这个年份在你的例子中不是变量,所以我假设所有的日期都在2014年(或者最后一个日期) 2013年的几天,从第1周开始12月30日)。如果您不熟悉 lubridate
,以下内容将包含许多您不知道的功能。使用?
获得关于它们的帮助(例如?ymd
)。
It seems that the year is not variable in your example, so I assume that all the dates are in 2014 (or in the last few days of 2013, since week 1 starts on December 30th). If you are not familiar with lubridate
, the following will contain many functions that are unknown to you. Use ?
to get help about them (e.g. ?ymd
).
第一步是获取一年中第一个星期的星期一。当然,你可以查找它,但 lubridate
可用于计算:
The first step is to get the Monday of the first week of the year. Of course, you could look it up, but lubridate
can be used to calculate it:
library(lubridate)
start_date <- ymd("20140201")
week(start_date) <- 1
wday(start_date) <- "Monday"
start_date
## [1] "2013-12-30 UTC"
这首先选择2014年的任意一天,然后将星期设置为1,将星期设置为星期一。现在我可以通过添加适当的周数获取任意日历周的第一天:
This first picks an arbitrary day in 2014 and then sets the week to 1 and the weekday to Monday. Now I can get the first day of any calendar week by adding the appropriate number of weeks:
start_date + weeks(2)
## [1] "2014-01-13 UTC"
数据集为三周:
data <- data.frame(week = paste("KW", 1:3), value = c(14000, 21000, 28000))
data
## week value
## 1 KW 1 14000
## 2 KW 2 21000
## 3 KW 3 28000
转换为所需格式的操作如下:
And the conversion to your desired format works as follows:
weeks <- rep(data$week, each = 7)
weeks_num = as.numeric(gsub("KW *", "", weeks))
intervals <- weeks(weeks_num - 1) + days(0:6)
dates <- as.Date(start_date + intervals)
values <- rep(data$value, each = 7)/7
new_data <- data.frame(week = weeks, date = dates, value = values)
new_data
## week date value
## 1 KW 1 2013-12-30 2000
## 2 KW 1 2013-12-31 2000
## 3 KW 1 2014-01-01 2000
## 4 KW 1 2014-01-02 2000
## 5 KW 1 2014-01-03 2000
## 6 KW 1 2014-01-04 2000
## 7 KW 1 2014-01-05 2000
## 8 KW 2 2014-01-06 3000
## 9 KW 2 2014-01-07 3000
## 10 KW 2 2014-01-08 3000
## 11 KW 2 2014-01-09 3000
## 12 KW 2 2014-01-10 3000
## 13 KW 2 2014-01-11 3000
## 14 KW 2 2014-01-12 3000
## 15 KW 3 2014-01-13 4000
## 16 KW 3 2014-01-14 4000
## 17 KW 3 2014-01-15 4000
## 18 KW 3 2014-01-16 4000
## 19 KW 3 2014-01-17 4000
## 20 KW 3 2014-01-18 4000
## 21 KW 3 2014-01-19 4000
这个工作原理如下:
-
首先,我准备日期。每个日历周重复七次(每天一次)。然后删除KW零件,将星期转换为数字。之后,我使用
lubridate
函数weeks()
和days()
来构造所有所需的时间间隔,因为start_date
。
First I prepare the dates. Each calendar week is repeated seven times (once for each day). Then the "KW " part is removed and the weeks are converted to numeric. Afterwards, I use the
lubridate
functionsweeks()
anddays()
to construct all the required time intervals sincestart_date
.
最后,我将结果放到一个新的数据框架中。
Finally, I put the results into a new data frame.
最后一句话:这是一个复杂的解决方案。如果你确定,没有一周会丢失,更容易简单地产生一系列日期,甚至不考虑日历周的列如下:
A final remark: This is a complicated solution. If you are certain, that no week will be missing, it is easier to simply produce a series of dates without even considering the column for the calendar weeks as follows:
dates <- as.Date(start_date + days(0:(7*nrow(data) - 1)))
identical(new_data$date, dates)
## [1] TRUE
所以这里我只需要计算周数 nrows()
,并产生一个时间间隔序列 days()
,然后添加到 start_date
。
然而,如果日历周可能丢失,您应该使用上面更一般的解决方案。
So here I simply count the number of weeks with nrows()
, and produce a sequence of time intervals with days()
that I then add to start_date
.
However, if calendar weeks could be missing, you should use the more general solution above.
这篇关于将日历周转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!