如何仅将时间序列数据组织到数据框的对角线上?
How can I organize time series data onto the diagonal of a data frame ONLY?
如果我有这样的月度时间序列:
xts <- ts(c(1:48), frequency = 12)
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 1 2 3 4 5 6 7 8 9 10 11 12
2 13 14 15 16 17 18 19 20 21 22 23 24
3 25 26 27 28 29 30 31 32 33 34 35 36
4 37 38 39 40 41 42 43 44 45 46 47 48
如何将值组织为仅在 'diagonal' 上,而在其他任何地方都为零?如下所示(此格式适用于 xts
中的所有值):
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 1 0 0 0 0 0 0 0 0 0 0 0
2 0 2 0 0 0 0 0 0 0 0 0 0
3 0 0 3 0 0 0 0 0 0 0 0 0
4 0 0 0 4 0 0 0 0 0 0 0 0
5 0 0 0 0 5 0 0 0 0 0 0 0
6 0 0 0 0 0 6 0 0 0 0 0 0
7 0 0 0 0 0 0 7 0 0 0 0 0
8 0 0 0 0 0 0 0 8 0 0 0 0
9 0 0 0 0 0 0 0 0 9 0 0 0
10 0 0 0 0 0 0 0 0 0 10 0 0
11 0 0 0 0 0 0 0 0 0 0 11 0
12 0 0 0 0 0 0 0 0 0 0 0 12
13 13 0 0 0 0 0 0 0 0 0 0 0
14 0 14 0 0 0 0 0 0 0 0 0 0
15 0 0 15 0 0 0 0 0 0 0 0 0
16 0 0 0 16 0 0 0 0 0 0 0 0
17 0 0 0 0 17 0 0 0 0 0 0 0
18 0 0 0 0 0 18 0 0 0 0 0 0
19 0 0 0 0 0 0 19 0 0 0 0 0
20 0 0 0 0 0 0 0 20 0 0 0 0
21 0 0 0 0 0 0 0 0 21 0 0 0
22 0 0 0 0 0 0 0 0 0 22 0 0
23 0 0 0 0 0 0 0 0 0 0 23 0
24 0 0 0 0 0 0 0 0 0 0 0 24
我正在使用需要这种格式的建模包,并且在 'Matrix' 和 'diag' 等包中找到解决方案时运气不佳,它们似乎没有设置以这种方式排列数据。
通过行绑定诊断解决。
rbind(diag(1:12,12,12), diag(13:24, 12,12), diag(25:36, 12,12 ), diag(37:48,12,12))
如果我有这样的月度时间序列:
xts <- ts(c(1:48), frequency = 12)
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 1 2 3 4 5 6 7 8 9 10 11 12
2 13 14 15 16 17 18 19 20 21 22 23 24
3 25 26 27 28 29 30 31 32 33 34 35 36
4 37 38 39 40 41 42 43 44 45 46 47 48
如何将值组织为仅在 'diagonal' 上,而在其他任何地方都为零?如下所示(此格式适用于 xts
中的所有值):
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 1 0 0 0 0 0 0 0 0 0 0 0
2 0 2 0 0 0 0 0 0 0 0 0 0
3 0 0 3 0 0 0 0 0 0 0 0 0
4 0 0 0 4 0 0 0 0 0 0 0 0
5 0 0 0 0 5 0 0 0 0 0 0 0
6 0 0 0 0 0 6 0 0 0 0 0 0
7 0 0 0 0 0 0 7 0 0 0 0 0
8 0 0 0 0 0 0 0 8 0 0 0 0
9 0 0 0 0 0 0 0 0 9 0 0 0
10 0 0 0 0 0 0 0 0 0 10 0 0
11 0 0 0 0 0 0 0 0 0 0 11 0
12 0 0 0 0 0 0 0 0 0 0 0 12
13 13 0 0 0 0 0 0 0 0 0 0 0
14 0 14 0 0 0 0 0 0 0 0 0 0
15 0 0 15 0 0 0 0 0 0 0 0 0
16 0 0 0 16 0 0 0 0 0 0 0 0
17 0 0 0 0 17 0 0 0 0 0 0 0
18 0 0 0 0 0 18 0 0 0 0 0 0
19 0 0 0 0 0 0 19 0 0 0 0 0
20 0 0 0 0 0 0 0 20 0 0 0 0
21 0 0 0 0 0 0 0 0 21 0 0 0
22 0 0 0 0 0 0 0 0 0 22 0 0
23 0 0 0 0 0 0 0 0 0 0 23 0
24 0 0 0 0 0 0 0 0 0 0 0 24
我正在使用需要这种格式的建模包,并且在 'Matrix' 和 'diag' 等包中找到解决方案时运气不佳,它们似乎没有设置以这种方式排列数据。
通过行绑定诊断解决。
rbind(diag(1:12,12,12), diag(13:24, 12,12), diag(25:36, 12,12 ), diag(37:48,12,12))