【发布时间】:2021-04-26 07:50:59
【问题描述】:
我有一个每周需要的时间序列,但我目前拥有每季度的数据。例如:
R> test
Quarter week totA totB totC totD
1 1 2015-12-28 1745 1720 11 1714
2 2 2016-03-28 1736 1718 7 1710
3 3 2016-06-27 1777 1768 5 1750
4 4 2016-09-26 1833 1815 13 1795
5 1 2016-12-26 1708 1697 6 1677
R>
我想要的是每周获得信息,每个总数(totA 到 totD)需要除以下一个季度之前的周数(即 13,因为有 13 2016 年季度中的周数 - 但如果有一年有 53 周(例如 2015 年),则偶尔可能是 14 周),因此季度总数相同。因此,根据上面的示例,前 26 周变为:
1 1 2015-12-28 134.9 132.3 0.8462 131.8
2 2 2016-01-04 134.9 132.3 0.8462 131.8
3 3 2016-01-11 134.9 132.3 0.8462 131.8
4 4 2016-01-18 134.9 132.3 0.8462 131.8
5 5 2016-01-25 134.9 132.3 0.8462 131.8
6 6 2016-02-01 134.9 132.3 0.8462 131.8
7 7 2016-02-08 134.9 132.3 0.8462 131.8
8 8 2016-02-15 134.9 132.3 0.8462 131.8
9 9 2016-02-22 134.9 132.3 0.8462 131.8
10 10 2016-02-29 134.9 132.3 0.8462 131.8
11 11 2016-03-07 134.9 132.3 0.8462 131.8
12 12 2016-03-14 134.9 132.3 0.8462 131.8
13 13 2016-03-21 134.9 132.3 0.8462 131.8
14 14 2016-03-28 133.5 132.2 0.5385 131.5
15 15 2016-04-04 133.5 132.2 0.5385 131.5
16 16 2016-04-11 133.5 132.2 0.5385 131.5
17 17 2016-04-18 133.5 132.2 0.5385 131.5
18 18 2016-04-25 133.5 132.2 0.5385 131.5
19 19 2016-05-02 133.5 132.2 0.5385 131.5
20 20 2016-05-09 133.5 132.2 0.5385 131.5
21 21 2016-05-16 133.5 132.2 0.5385 131.5
22 22 2016-05-23 133.5 132.2 0.5385 131.5
23 23 2016-05-30 133.5 132.2 0.5385 131.5
24 24 2016-06-06 133.5 132.2 0.5385 131.5
25 25 2016-06-13 133.5 132.2 0.5385 131.5
26 26 2016-06-20 133.5 132.2 0.5385 131.5
R>
这是使用以下方法获得的:
rbind(
data.frame(Week_number=c(1:13),
Week_commencing=seq(as.Date("2015-12-28"), by=7, len=13),
totA=rep(1754/13,13),
totB=rep(1720/13,13),
totC=rep(11/13,13),
totD=rep(1714/13,13)
),
data.frame(Week_number=c(14:26),
Week_commencing=seq(as.Date("2016-03-28"), by=7, len=13),
totA=rep(1736/13,13),
totB=rep(1718/13,13),
totC=rep(7/13,13),
totD=rep(1710/13,13)
)
)
But there's clearly a better way of doing it rather than manually... The data set is, of course, much larger!
I've tried a few things, but other than creating a sequence of weeks and then filling it in manually as above, I'm going around in circles. I'm sure there's a way to do it in the tidyverse, but I can't figure out how (most of my R is self-taught, and from before when tidyverse was available). Any help would be appreciated!
【问题讨论】:
-
我无法理解您的需求。如果您可以提供有关不同列是什么的更多信息,例如,您可能会得到更好的答案。现有的
week列显示了什么。提供所需输出的示例也将大有帮助! -
谢谢。我试图添加更多说明。现有的“周”只是本季度的第一个星期一,但我需要一年中每一周的数据。
标签: r time-series imputation