使用列转换表作为r

时间:2017-10-05 07:37:56

标签: r timestamp time-series data-conversion

> data
             timestamps   diff     time    date
1   2017-09-01 00:00:00    0.000 00:00:00   01
2   2017-09-01 01:00:00    0.000 01:00:00   01
3   2017-09-01 02:00:00    0.000 02:00:00   01
4   2017-09-01 03:00:00    0.000 03:00:00   01
5   2017-09-01 04:00:00    0.000 04:00:00   01
6   2017-09-01 05:00:00    0.000 05:00:00   01
7   2017-09-01 06:00:00    0.000 06:00:00   01
8   2017-09-01 07:00:00    1.048 07:00:00   01
9   2017-09-01 08:00:00    4.932 08:00:00   01
10  2017-09-01 09:00:00    6.220 09:00:00   01
11  2017-09-01 10:00:00    4.296 10:00:00   01
12  2017-09-01 11:00:00    3.968 11:00:00   01
13  2017-09-01 12:00:00    4.440 12:00:00   01
14  2017-09-01 13:00:00    4.688 13:00:00   01
15  2017-09-01 14:00:00    4.784 14:00:00   01
16  2017-09-01 15:00:00    6.452 15:00:00   01
17  2017-09-01 16:00:00    4.952 16:00:00   01
18  2017-09-01 17:00:00    6.096 17:00:00   01
19  2017-09-01 18:00:00    5.120 18:00:00   01
20  2017-09-01 19:00:00    6.356 19:00:00   01
21  2017-09-01 20:00:00    3.324 20:00:00   01
22  2017-09-01 21:00:00    0.000 21:00:00   01
23  2017-09-01 22:00:00    0.000 22:00:00   01
24  2017-09-01 23:00:00    0.000 23:00:00   01
25  2017-09-02 00:00:00    0.532 00:00:00   02
26  2017-09-02 01:00:00    7.780 01:00:00   02
27  2017-09-02 02:00:00    6.944 02:00:00   02
28  2017-09-02 03:00:00    6.880 03:00:00   02
29  2017-09-02 04:00:00    7.736 04:00:00   02
30  2017-09-02 05:00:00    5.732 05:00:00   02
31  2017-09-02 06:00:00    6.316 06:00:00   02
32  2017-09-02 07:00:00    6.896 07:00:00   02
33  2017-09-02 08:00:00    6.440 08:00:00   02
34  2017-09-02 09:00:00    6.036 09:00:00   02
35  2017-09-02 10:00:00    6.020 10:00:00   02
36  2017-09-02 11:00:00    7.084 11:00:00   02
37  2017-09-02 12:00:00    4.692 12:00:00   02
38  2017-09-02 13:00:00    0.000 13:00:00   02
39  2017-09-02 14:00:00    0.000 14:00:00   02
40  2017-09-02 15:00:00    0.000 15:00:00   02
41  2017-09-02 16:00:00    0.000 16:00:00   02
42  2017-09-02 17:00:00    0.000 17:00:00   02
43  2017-09-02 18:00:00    0.000 18:00:00   02
44  2017-09-02 19:00:00    0.000 19:00:00   02
45  2017-09-02 20:00:00    0.000 20:00:00   02
46  2017-09-02 21:00:00    0.000 21:00:00   02
47  2017-09-02 22:00:00    0.000 22:00:00   02
48  2017-09-02 23:00:00    0.000 23:00:00   02
49  2017-09-03 00:00:00    0.000 00:00:00   03
50  2017-09-03 01:00:00    0.000 01:00:00   03
51  2017-09-03 02:00:00    0.000 02:00:00   03
52  2017-09-03 03:00:00    0.000 03:00:00   03
53  2017-09-03 04:00:00    0.000 04:00:00   03
54  2017-09-03 05:00:00    0.000 05:00:00   03
55  2017-09-03 06:00:00    0.000 06:00:00   03
56  2017-09-03 07:00:00    0.000 07:00:00   03
57  2017-09-03 08:00:00    2.080 08:00:00   03
58  2017-09-03 09:00:00    5.388 09:00:00   03
59  2017-09-03 10:00:00    5.588 10:00:00   03
60  2017-09-03 11:00:00    8.064 11:00:00   03
61  2017-09-03 12:00:00    8.000 12:00:00   03
62  2017-09-03 13:00:00    7.108 13:00:00   03
63  2017-09-03 14:00:00    7.568 14:00:00   03
64  2017-09-03 15:00:00    7.276 15:00:00   03
65  2017-09-03 16:00:00    7.316 16:00:00   03
66  2017-09-03 17:00:00    8.860 17:00:00   03
67  2017-09-03 18:00:00    7.508 18:00:00   03
68  2017-09-03 19:00:00    7.368 19:00:00   03
69  2017-09-03 20:00:00    7.580 20:00:00   03
70  2017-09-03 21:00:00    7.132 21:00:00   03
71  2017-09-03 22:00:00    8.672 22:00:00   03
72  2017-09-03 23:00:00    6.588 23:00:00   03
73  2017-09-04 00:00:00    7.676 00:00:00   04
74  2017-09-04 01:00:00    7.204 01:00:00   04
75  2017-09-04 02:00:00    6.624 02:00:00   04
76  2017-09-04 03:00:00    3.644 03:00:00   04
77  2017-09-04 04:00:00    0.000 04:00:00   04
78  2017-09-04 05:00:00    0.000 05:00:00   04
79  2017-09-04 06:00:00    0.000 06:00:00   04
80  2017-09-04 07:00:00    0.000 07:00:00   04
81  2017-09-04 08:00:00    0.000 08:00:00   04
82  2017-09-04 09:00:00    0.000 09:00:00   04
83  2017-09-04 10:00:00    0.000 10:00:00   04
84  2017-09-04 11:00:00    0.000 11:00:00   04
85  2017-09-04 12:00:00    0.000 12:00:00   04
86  2017-09-04 13:00:00    0.000 13:00:00   04
87  2017-09-04 14:00:00    0.000 14:00:00   04
88  2017-09-04 15:00:00    0.000 15:00:00   04
89  2017-09-04 16:00:00    0.000 16:00:00   04
90  2017-09-04 17:00:00    0.000 17:00:00   04
91  2017-09-04 18:00:00    0.000 18:00:00   04
92  2017-09-04 19:00:00    0.000 19:00:00   04
93  2017-09-04 20:00:00    0.000 20:00:00   04
94  2017-09-04 21:00:00    0.000 21:00:00   04
95  2017-09-04 22:00:00    0.000 22:00:00   04
96  2017-09-04 23:00:00    0.000 23:00:00   04
97  2017-09-05 00:00:00    0.000 00:00:00   05
98  2017-09-05 01:00:00    0.000 01:00:00   05
99  2017-09-05 02:00:00    0.000 02:00:00   05
100 2017-09-05 03:00:00    0.000 03:00:00   05
101 2017-09-05 04:00:00    0.000 04:00:00   05
102 2017-09-05 05:00:00    0.000 05:00:00   05
103 2017-09-05 06:00:00    0.000 06:00:00   05
104 2017-09-05 07:00:00    0.000 07:00:00   05
105 2017-09-05 08:00:00    0.000 08:00:00   05
106 2017-09-05 09:00:00    0.000 09:00:00   05
107 2017-09-05 10:00:00    0.000 10:00:00   05
108 2017-09-05 11:00:00    0.000 11:00:00   05
109 2017-09-05 12:00:00    0.000 12:00:00   05
110 2017-09-05 13:00:00    0.000 13:00:00   05
111 2017-09-05 14:00:00    0.000 14:00:00   05
112 2017-09-05 15:00:00    0.000 15:00:00   05
113 2017-09-05 16:00:00    0.000 16:00:00   05
114 2017-09-05 17:00:00    0.000 17:00:00   05
115 2017-09-05 18:00:00    0.000 18:00:00   05
116 2017-09-05 19:00:00    0.000 19:00:00   05
117 2017-09-05 20:00:00    0.000 20:00:00   05
118 2017-09-05 21:00:00    0.000 21:00:00   05
119 2017-09-05 22:00:00    0.000 22:00:00   05
120 2017-09-05 23:00:00    0.000 23:00:00   05
121 2017-09-06 00:00:00    0.000 00:00:00   06
122 2017-09-06 01:00:00    0.000 01:00:00   06
123 2017-09-06 02:00:00    0.000 02:00:00   06
124 2017-09-06 03:00:00    0.000 03:00:00   06
125 2017-09-06 04:00:00    0.000 04:00:00   06
126 2017-09-06 05:00:00    0.000 05:00:00   06
127 2017-09-06 06:00:00    0.000 06:00:00   06
128 2017-09-06 07:00:00    0.000 07:00:00   06
129 2017-09-06 08:00:00    0.000 08:00:00   06
130 2017-09-06 09:00:00    0.000 09:00:00   06
131 2017-09-06 10:00:00    6.164 10:00:00   06
132 2017-09-06 11:00:00    6.780 11:00:00   06
133 2017-09-06 12:00:00    8.212 12:00:00   06
134 2017-09-06 13:00:00    5.620 13:00:00   06
135 2017-09-06 14:00:00    4.916 14:00:00   06
136 2017-09-06 15:00:00    1.828 15:00:00   06
137 2017-09-06 16:00:00    6.540 16:00:00   06
138 2017-09-06 17:00:00    7.704 17:00:00   06
139 2017-09-06 18:00:00    6.272 18:00:00   06
140 2017-09-06 19:00:00    4.656 19:00:00   06
141 2017-09-06 20:00:00    5.840 20:00:00   06
142 2017-09-06 21:00:00    6.648 21:00:00   06
143 2017-09-06 22:00:00    8.112 22:00:00   06
144 2017-09-06 23:00:00    7.392 23:00:00   06
145 2017-09-07 00:00:00    7.888 00:00:00   07
146 2017-09-07 01:00:00    7.680 01:00:00   07
147 2017-09-07 02:00:00    8.112 02:00:00   07
148 2017-09-07 03:00:00    7.408 03:00:00   07
149 2017-09-07 04:00:00    8.048 04:00:00   07
150 2017-09-07 05:00:00    8.560 05:00:00   07
151 2017-09-07 06:00:00    5.680 06:00:00   07
152 2017-09-07 07:00:00    8.048 07:00:00   07
153 2017-09-07 08:00:00    7.744 08:00:00   07
154 2017-09-07 09:00:00    7.896 09:00:00   07
155 2017-09-07 10:00:00    7.424 10:00:00   07
156 2017-09-07 11:00:00    8.800 11:00:00   07
157 2017-09-07 12:00:00    8.536 12:00:00   07
158 2017-09-07 13:00:00    7.176 13:00:00   07
159 2017-09-07 14:00:00    7.888 14:00:00   07
160 2017-09-07 15:00:00    6.768 15:00:00   07
161 2017-09-07 16:00:00    5.952 16:00:00   07
162 2017-09-07 17:00:00    6.656 17:00:00   07
163 2017-09-07 18:00:00    7.560 18:00:00   07
164 2017-09-07 19:00:00    5.960 19:00:00   07
165 2017-09-07 20:00:00    7.536 20:00:00   07
166 2017-09-07 21:00:00    5.704 21:00:00   07
167 2017-09-07 22:00:00    4.200 22:00:00   07
168 2017-09-07 23:00:00    6.000 23:00:00   07
169 2017-09-08 00:00:00    5.392 00:00:00   08
170 2017-09-08 01:00:00    7.376 01:00:00   08
171 2017-09-08 02:00:00    8.000 02:00:00   08
172 2017-09-08 03:00:00    5.824 03:00:00   08
173 2017-09-08 04:00:00    7.136 04:00:00   08
174 2017-09-08 05:00:00    7.496 05:00:00   08
175 2017-09-08 06:00:00    8.528 06:00:00   08
176 2017-09-08 07:00:00    7.576 07:00:00   08
177 2017-09-08 08:00:00    5.616 08:00:00   08
178 2017-09-08 09:00:00    7.312 09:00:00   08
179 2017-09-08 10:00:00    8.648 10:00:00   08
180 2017-09-08 11:00:00    7.488 11:00:00   08
181 2017-09-08 12:00:00    7.000 12:00:00   08
182 2017-09-08 13:00:00    6.008 13:00:00   08
183 2017-09-08 14:00:00    8.320 14:00:00   08
184 2017-09-08 15:00:00    6.944 15:00:00   08
185 2017-09-08 16:00:00    9.144 16:00:00   08
186 2017-09-08 17:00:00    7.184 17:00:00   08
187 2017-09-08 18:00:00    7.800 18:00:00   08
188 2017-09-08 19:00:00    7.800 19:00:00   08
189 2017-09-08 20:00:00    8.960 20:00:00   08
190 2017-09-08 21:00:00    6.040 21:00:00   08
191 2017-09-08 22:00:00    7.104 22:00:00   08
192 2017-09-08 23:00:00    8.312 23:00:00   08
193 2017-09-09 00:00:00    5.648 00:00:00   09
194 2017-09-09 01:00:00    7.992 01:00:00   09
195 2017-09-09 02:00:00    8.360 02:00:00   09
196 2017-09-09 03:00:00    6.792 03:00:00   09
197 2017-09-09 04:00:00    9.120 04:00:00   09
198 2017-09-09 05:00:00    5.816 05:00:00   09
199 2017-09-09 06:00:00    6.408 06:00:00   09
200 2017-09-09 07:00:00    8.992 07:00:00   09
201 2017-09-09 08:00:00    7.576 08:00:00   09
202 2017-09-09 09:00:00    8.456 09:00:00   09
203 2017-09-09 10:00:00    5.984 10:00:00   09
204 2017-09-09 11:00:00    8.680 11:00:00   09
205 2017-09-09 12:00:00    7.896 12:00:00   09
206 2017-09-09 13:00:00    5.184 13:00:00   09
207 2017-09-09 14:00:00    7.288 14:00:00   09
208 2017-09-09 15:00:00    7.840 15:00:00   09
209 2017-09-09 16:00:00    9.024 16:00:00   09
210 2017-09-09 17:00:00    6.968 17:00:00   09
211 2017-09-09 18:00:00    8.224 18:00:00   09
212 2017-09-09 19:00:00    7.720 19:00:00   09
213 2017-09-09 20:00:00    7.728 20:00:00   09
214 2017-09-09 21:00:00    8.152 21:00:00   09
215 2017-09-09 22:00:00    8.400 22:00:00   09
216 2017-09-09 23:00:00    5.872 23:00:00   09
217 2017-09-10 00:00:00    5.104 00:00:00   10
218 2017-09-10 01:00:00    5.520 01:00:00   10
219 2017-09-10 02:00:00    8.712 02:00:00   10
220 2017-09-10 03:00:00    7.808 03:00:00   10
221 2017-09-10 04:00:00    8.688 04:00:00   10
222 2017-09-10 05:00:00    8.312 05:00:00   10
223 2017-09-10 06:00:00    6.688 06:00:00   10
224 2017-09-10 07:00:00    5.200 07:00:00   10
225 2017-09-10 08:00:00    8.304 08:00:00   10
226 2017-09-10 09:00:00    4.072 09:00:00   10
227 2017-09-10 10:00:00    9.144 10:00:00   10
228 2017-09-10 11:00:00    6.576 11:00:00   10
229 2017-09-10 12:00:00    7.248 12:00:00   10
230 2017-09-10 13:00:00    6.104 13:00:00   10
231 2017-09-10 14:00:00    6.408 14:00:00   10
232 2017-09-10 15:00:00    9.224 15:00:00   10
233 2017-09-10 19:00:00    9.000 19:00:00   10
234 2017-09-10 21:00:00    7.944 21:00:00   10
235 2017-09-10 22:00:00    8.736 22:00:00   10
246 2017-09-10 23:00:00    8.904 23:00:00   10

我有一个包含时间戳和差异值的数据。我想转换这个表,使得日期是列(1,2,3 ..)和时间值作为行(00:00:00,01:00:00,02:00:00 ......)。所以我分开了日期和时间。但我被困在如何转换这个。我希望我的桌子像这样

  time    1       2     3
0:00:00   0      0.532  0
1:00:00   0      7.78   0
2:00:00   0      6.944  0
3:00:00   0      6.88   0
4:00:00   0      7.736  0
5:00:00   0      5.732  0
6:00:00   0      6.316  0
7:00:00   1.048  6.896  0
8:00:00   4.932  6.44   2.08
9:00:00   6.22   6.036  5.388
10:00:00  4.296  6.02   5.588
11:00:00  3.968  7.084  8.064

应该注意的是,在某些日期会丢失几个小时(例如,第10天缺少16,17,19,20小时)。非常感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:0)

library(dplyr)
df2 <- spread(df, day, diff)