Заполните еженедельный фрейм данных количеством для ежемесячного фрейма данных - PullRequest
0 голосов
/ 31 марта 2020

Мне нужно заполнить еженедельный дф с суммой ежемесячного дф. Каждый месяц одно и то же?

df1<-    date
1   2013-04-26
2   2013-05-03
3   2013-05-10
4   2013-05-17
5   2013-05-24
6   2013-05-31
7   2013-06-07
8   2013-06-14
9   2013-06-21
10  2013-06-28
11  2013-07-05
12  2013-07-12
13  2013-07-19
14  2013-07-26
15  2013-08-02
16  2013-08-09
17  2013-08-16
18  2013-08-23
19  2013-08-30
20  2013-09-06
21  2013-09-13
22  2013-09-20
23  2013-09-27
24  2013-10-04
25  2013-10-11
26  2013-10-18
27  2013-10-25
28  2013-11-01
29  2013-11-08
30  2013-11-15
31  2013-11-22
32  2013-11-29
33  2013-12-06
34  2013-12-13
35  2013-12-20
36  2013-12-27
37  2014-01-03
38  2014-01-10
39  2014-01-17
40  2014-01-24
41  2014-01-31
42  2014-02-07
43  2014-02-14
44  2014-02-21
45  2014-02-28
46  2014-03-07
47  2014-03-14
48  2014-03-21
49  2014-03-28
50  2014-04-04
51  2014-04-11
52  2014-04-18
53  2014-04-25
54  2014-05-02
55  2014-05-09
56  2014-05-16
57  2014-05-23
58  2014-05-30
59  2014-06-06
60  2014-06-13
61  2014-06-20
62  2014-06-27
63  2014-07-04
64  2014-07-11
65  2014-07-18
66  2014-07-25
67  2014-08-01
68  2014-08-08
69  2014-08-15
70  2014-08-22
71  2014-08-29
72  2014-09-05
73  2014-09-12
74  2014-09-19
75  2014-09-26
76  2014-10-03
77  2014-10-10
78  2014-10-17
79  2014-10-24
80  2014-10-31
81  2014-11-07
82  2014-11-14
83  2014-11-21
84  2014-11-28
85  2014-12-05
86  2014-12-12
87  2014-12-19
88  2014-12-26
89  2015-01-02
90  2015-01-09
91  2015-01-16
92  2015-01-23
93  2015-01-30
94  2015-02-06
95  2015-02-13
96  2015-02-20
97  2015-02-27
98  2015-03-06
99  2015-03-13
100 2015-03-20
101 2015-03-27
102 2015-04-03
103 2015-04-10
104 2015-04-17
105 2015-04-24
106 2015-05-01
107 2015-05-08
108 2015-05-15
109 2015-05-22
110 2015-05-29
111 2015-06-05
112 2015-06-12
113 2015-06-19
114 2015-06-26
115 2015-07-03
116 2015-07-10
117 2015-07-17
118 2015-07-24
119 2015-07-31
120 2015-08-07
121 2015-08-14
122 2015-08-21
123 2015-08-28
124 2015-09-04
125 2015-09-11
126 2015-09-18
127 2015-09-25
128 2015-10-02
129 2015-10-09
130 2015-10-16
131 2015-10-23
132 2015-10-30
133 2015-11-06
134 2015-11-13
135 2015-11-20
136 2015-11-27
137 2015-12-04
138 2015-12-11
139 2015-12-18
140 2015-12-25
141 2016-01-01
142 2016-01-08
143 2016-01-15
144 2016-01-22
145 2016-01-29
146 2016-02-05
147 2016-02-12
148 2016-02-19
149 2016-02-26
150 2016-03-04
151 2016-03-11
152 2016-03-18
153 2016-03-25
154 2016-04-01
155 2016-04-08
156 2016-04-15
157 2016-04-22
158 2016-04-29
159 2016-05-06
160 2016-05-13
161 2016-05-20
162 2016-05-27
163 2016-06-03
164 2016-06-10
165 2016-06-17
166 2016-06-24
167 2016-07-01
168 2016-07-08
169 2016-07-15
170 2016-07-22
171 2016-07-29
172 2016-08-05
173 2016-08-12
174 2016-08-19
175 2016-08-26
176 2016-09-02
177 2016-09-09
178 2016-09-16
179 2016-09-23
180 2016-09-30
181 2016-10-07
182 2016-10-14
183 2016-10-21
184 2016-10-28
185 2016-11-04
186 2016-11-11
187 2016-11-18
188 2016-11-25
189 2016-12-02
190 2016-12-09
191 2016-12-16
192 2016-12-23
193 2016-12-30
194 2017-01-06
195 2017-01-13
196 2017-01-20
197 2017-01-27
198 2017-02-03
199 2017-02-10
200 2017-02-17
201 2017-02-24
202 2017-03-03
203 2017-03-10
204 2017-03-17
205 2017-03-24
206 2017-03-31
207 2017-04-07
208 2017-04-14
209 2017-04-21
210 2017-04-28
211 2017-05-05
212 2017-05-12
213 2017-05-19
214 2017-05-26
215 2017-06-02
216 2017-06-09
217 2017-06-16
218 2017-06-23
219 2017-06-30
220 2017-07-07
221 2017-07-14
222 2017-07-21
223 2017-07-28
224 2017-08-04
225 2017-08-11
226 2017-08-18
227 2017-08-25
228 2017-09-01
229 2017-09-08
230 2017-09-15
231 2017-09-22
232 2017-09-29
233 2017-10-06
234 2017-10-13
235 2017-10-20
236 2017-10-27
237 2017-11-03
238 2017-11-10
239 2017-11-17
240 2017-11-24
241 2017-12-01
242 2017-12-08
243 2017-12-15
244 2017-12-22
245 2017-12-29
246 2018-01-05
247 2018-01-12
248 2018-01-19
249 2018-01-26
250 2018-02-02
251 2018-02-09
252 2018-02-16
253 2018-02-23
254 2018-03-02
255 2018-03-09
256 2018-03-16
257 2018-03-23
258 2018-03-30
259 2018-04-06
260 2018-04-13
261 2018-04-20
262 2018-04-27
263 2018-05-04
264 2018-05-11
265 2018-05-18
266 2018-05-25
267 2018-06-01
268 2018-06-08
269 2018-06-15
270 2018-06-22
271 2018-06-29
272 2018-07-06
273 2018-07-13
274 2018-07-20
275 2018-07-27
276 2018-08-03
277 2018-08-10
278 2018-08-17
279 2018-08-24
280 2018-08-31
281 2018-09-07
282 2018-09-14
283 2018-09-21
284 2018-09-28
285 2018-10-05
286 2018-10-12
287 2018-10-19
288 2018-10-26
289 2018-11-02
290 2018-11-09
291 2018-11-16
292 2018-11-23
293 2018-11-30
294 2018-12-07
295 2018-12-14
296 2018-12-21
297 2018-12-28
298 2019-01-04
299 2019-01-11
300 2019-01-18
301 2019-01-25
302 2019-02-01
303 2019-02-08
304 2019-02-15
305 2019-02-22
306 2019-03-01
307 2019-03-08
308 2019-03-15
309 2019-03-22
310 2019-03-29
311 2019-04-05
312 2019-04-12
313 2019-04-19
314 2019-04-26
315 2019-05-03
316 2019-05-10
317 2019-05-17
318 2019-05-24
319 2019-05-31
320 2019-06-07
321 2019-06-14
322 2019-06-21
323 2019-06-28
324 2019-07-05
325 2019-07-12
326 2019-07-19
327 2019-07-26
328 2019-08-02
329 2019-08-09
330 2019-08-16
331 2019-08-23
332 2019-08-30
333 2019-09-06
334 2019-09-13
335 2019-09-20
336 2019-09-27
337 2019-10-04
338 2019-10-11
339 2019-10-18
340 2019-10-25
341 2019-11-01
342 2019-11-08
343 2019-11-15
344 2019-11-22
345 2019-11-29
346 2019-12-06
347 2019-12-13
348 2019-12-20
349 2019-12-27
350 2020-01-03
351 2020-01-10
352 2020-01-17
353 2020-01-24
354 2020-01-31
355 2020-02-07
356 2020-02-14
357 2020-02-21
358 2020-02-28
359 2020-03-06
360 2020-03-13
361 2020-03-20
362 2020-03-27
> 
df2 <-
     dates     amount
1  2013-06-19   0.150
2  2013-07-17   0.150
3  2013-08-14   0.150
4  2013-09-18   0.150
5  2013-10-16   0.150
6  2013-11-13   0.150
7  2013-12-18   0.150
8  2014-01-15   0.150
9  2014-02-12   0.150
10 2014-03-12   0.150
11 2014-04-15   0.150
12 2014-05-14   0.150
13 2014-06-18   0.150
14 2014-07-16   0.150
15 2014-08-13   0.150
16 2014-09-17   0.150
17 2014-10-15   0.150
18 2014-11-12   0.150
19 2014-12-17   0.253
20 2015-01-14   0.150
21 2015-02-11   0.150
22 2015-03-11   0.150
23 2015-04-15   0.150
24 2015-05-13   0.150
25 2015-06-17   0.150
26 2015-07-15   0.150
27 2015-08-12   0.150
28 2015-09-16   0.150
29 2015-10-14   0.150
30 2015-11-10   0.150
31 2015-12-16   0.237
32 2016-01-13   0.150
33 2016-02-10   0.150
34 2016-03-16   0.150
35 2016-04-13   0.150
36 2016-05-11   0.150
37 2016-06-15   0.150
38 2016-07-13   0.150
39 2016-08-10   0.150
40 2016-09-14   0.150
41 2016-10-12   0.150
42 2016-11-09   0.150
43 2016-12-14   0.159
44 2017-01-11   0.150
45 2017-02-15   0.150
46 2017-03-15   0.150
47 2017-04-19   0.150
48 2017-05-10   0.150
49 2017-06-14   0.150
50 2017-07-12   0.150
51 2017-08-16   0.150
52 2017-09-13   0.150
53 2017-10-11   0.150
54 2017-11-15   0.150
55 2017-12-13   0.190
56 2018-01-17   0.150
57 2018-02-14   0.150
58 2018-03-14   0.150
59 2018-04-18   0.150
60 2018-05-16   0.150
61 2018-06-13   0.150
62 2018-07-11   0.150
63 2018-08-15   0.150
64 2018-09-19   0.150
65 2018-10-10   0.150
66 2018-11-14   0.150
67 2018-12-12   0.150
68 2019-01-16   0.150
69 2019-02-13   0.150
70 2019-03-13   0.150
71 2019-04-17   0.150
72 2019-05-15   0.150
73 2019-06-12   0.150
74 2019-07-17   0.150
75 2019-08-14   0.150
76 2019-09-18   0.150
77 2019-10-16   0.150
78 2019-11-13   0.150
79 2019-12-11   0.044
80 2020-01-15   0.150
81 2020-02-12   0.150
82 2020-03-11   0.150

Ответы [ 2 ]

0 голосов
/ 31 марта 2020

С tidyverse и lubridate вы можете сделать следующее.

Я конвертировал даты с as.Date на случай, если это не было сделано с исходными данными. Затем для каждой даты в обоих фреймах данных будут созданы столбцы month и year для объединения двух фреймов данных (для общих month и year).

library(tidyverse)
library(lubridate)

df1$date <- as.Date(df1$date, format = "%Y-%m-%d")
df2$dates <- as.Date(df2$dates, format = "%Y-%m-%d")

df1 %>%
  mutate(month = month(date), year = year(date)) %>%
  left_join(df2 %>% mutate(month = month(dates), year = year(dates)), by = c("month", "year"))
0 голосов
/ 31 марта 2020

Решение Base R:

setNames(Reduce(function(x, y) {
  merge(x, y, by = "month_year", all.x = TRUE)
},
lapply(list(df1, within(
  df2, {
    date = dates
    rm(dates)
  }
)),
function(x) {
  x$month_year <- as.Date(strftime(x$date, "%Y-%m-01"), "%Y-%m-%d")
  return(x)
}))[, c("date.x", "amount")], c("date", "amount"))

Данные:

df1 <- structure(list(date = as.Date(c("2013-04-26", "2013-05-03", 
"2013-05-10", "2013-05-17", "2013-05-24", "2013-05-31", "2013-06-07", 
"2013-06-14", "2013-06-21", "2013-06-28", "2013-07-05", "2013-07-12", 
"2013-07-19", "2013-07-26", "2013-08-02", "2013-08-09", "2013-08-16", 
"2013-08-23", "2013-08-30", "2013-09-06", "2013-09-13", "2013-09-20", 
"2013-09-27", "2013-10-04", "2013-10-11", "2013-10-18", "2013-10-25", 
"2013-11-01", "2013-11-08", "2013-11-15", "2013-11-22", "2013-11-29", 
"2013-12-06", "2013-12-13", "2013-12-20", "2013-12-27", "2014-01-03", 
"2014-01-10", "2014-01-17", "2014-01-24", "2014-01-31", "2014-02-07", 
"2014-02-14", "2014-02-21", "2014-02-28", "2014-03-07", "2014-03-14", 
"2014-03-21", "2014-03-28", "2014-04-04", "2014-04-11", "2014-04-18", 
"2014-04-25", "2014-05-02", "2014-05-09", "2014-05-16", "2014-05-23", 
"2014-05-30", "2014-06-06", "2014-06-13", "2014-06-20", "2014-06-27", 
"2014-07-04", "2014-07-11", "2014-07-18", "2014-07-25", "2014-08-01", 
"2014-08-08", "2014-08-15", "2014-08-22", "2014-08-29", "2014-09-05", 
"2014-09-12", "2014-09-19", "2014-09-26", "2014-10-03", "2014-10-10", 
"2014-10-17", "2014-10-24", "2014-10-31", "2014-11-07", "2014-11-14", 
"2014-11-21", "2014-11-28", "2014-12-05", "2014-12-12", "2014-12-19", 
"2014-12-26", "2015-01-02", "2015-01-09", "2015-01-16", "2015-01-23", 
"2015-01-30", "2015-02-06", "2015-02-13", "2015-02-20", "2015-02-27", 
"2015-03-06", "2015-03-13", "2015-03-20", "2015-03-27", "2015-04-03", 
"2015-04-10", "2015-04-17", "2015-04-24", "2015-05-01", "2015-05-08", 
"2015-05-15", "2015-05-22", "2015-05-29", "2015-06-05", "2015-06-12", 
"2015-06-19", "2015-06-26", "2015-07-03", "2015-07-10", "2015-07-17", 
"2015-07-24", "2015-07-31", "2015-08-07", "2015-08-14", "2015-08-21", 
"2015-08-28", "2015-09-04", "2015-09-11", "2015-09-18", "2015-09-25", 
"2015-10-02", "2015-10-09", "2015-10-16", "2015-10-23", "2015-10-30", 
"2015-11-06", "2015-11-13", "2015-11-20", "2015-11-27", "2015-12-04", 
"2015-12-11", "2015-12-18", "2015-12-25", "2016-01-01", "2016-01-08", 
"2016-01-15", "2016-01-22", "2016-01-29", "2016-02-05", "2016-02-12", 
"2016-02-19", "2016-02-26", "2016-03-04", "2016-03-11", "2016-03-18", 
"2016-03-25", "2016-04-01", "2016-04-08", "2016-04-15", "2016-04-22", 
"2016-04-29", "2016-05-06", "2016-05-13", "2016-05-20", "2016-05-27", 
"2016-06-03", "2016-06-10", "2016-06-17", "2016-06-24", "2016-07-01", 
"2016-07-08", "2016-07-15", "2016-07-22", "2016-07-29", "2016-08-05", 
"2016-08-12", "2016-08-19", "2016-08-26", "2016-09-02", "2016-09-09", 
"2016-09-16", "2016-09-23", "2016-09-30", "2016-10-07", "2016-10-14", 
"2016-10-21", "2016-10-28", "2016-11-04", "2016-11-11", "2016-11-18", 
"2016-11-25", "2016-12-02", "2016-12-09", "2016-12-16", "2016-12-23", 
"2016-12-30", "2017-01-06", "2017-01-13", "2017-01-20", "2017-01-27", 
"2017-02-03", "2017-02-10", "2017-02-17", "2017-02-24", "2017-03-03", 
"2017-03-10", "2017-03-17", "2017-03-24", "2017-03-31", "2017-04-07", 
"2017-04-14", "2017-04-21", "2017-04-28", "2017-05-05", "2017-05-12", 
"2017-05-19", "2017-05-26", "2017-06-02", "2017-06-09", "2017-06-16", 
"2017-06-23", "2017-06-30", "2017-07-07", "2017-07-14", "2017-07-21", 
"2017-07-28", "2017-08-04", "2017-08-11", "2017-08-18", "2017-08-25", 
"2017-09-01", "2017-09-08", "2017-09-15", "2017-09-22", "2017-09-29", 
"2017-10-06", "2017-10-13", "2017-10-20", "2017-10-27", "2017-11-03", 
"2017-11-10", "2017-11-17", "2017-11-24", "2017-12-01", "2017-12-08", 
"2017-12-15", "2017-12-22", "2017-12-29", "2018-01-05", "2018-01-12", 
"2018-01-19", "2018-01-26", "2018-02-02", "2018-02-09", "2018-02-16", 
"2018-02-23", "2018-03-02", "2018-03-09", "2018-03-16", "2018-03-23", 
"2018-03-30", "2018-04-06", "2018-04-13", "2018-04-20", "2018-04-27", 
"2018-05-04", "2018-05-11", "2018-05-18", "2018-05-25", "2018-06-01", 
"2018-06-08", "2018-06-15", "2018-06-22", "2018-06-29", "2018-07-06", 
"2018-07-13", "2018-07-20", "2018-07-27", "2018-08-03", "2018-08-10", 
"2018-08-17", "2018-08-24", "2018-08-31", "2018-09-07", "2018-09-14", 
"2018-09-21", "2018-09-28", "2018-10-05", "2018-10-12", "2018-10-19", 
"2018-10-26", "2018-11-02", "2018-11-09", "2018-11-16", "2018-11-23", 
"2018-11-30", "2018-12-07", "2018-12-14", "2018-12-21", "2018-12-28", 
"2019-01-04", "2019-01-11", "2019-01-18", "2019-01-25", "2019-02-01", 
"2019-02-08", "2019-02-15", "2019-02-22", "2019-03-01", "2019-03-08", 
"2019-03-15", "2019-03-22", "2019-03-29", "2019-04-05", "2019-04-12", 
"2019-04-19", "2019-04-26", "2019-05-03", "2019-05-10", "2019-05-17", 
"2019-05-24", "2019-05-31", "2019-06-07", "2019-06-14", "2019-06-21", 
"2019-06-28", "2019-07-05", "2019-07-12", "2019-07-19", "2019-07-26", 
"2019-08-02", "2019-08-09", "2019-08-16", "2019-08-23", "2019-08-30", 
"2019-09-06", "2019-09-13", "2019-09-20", "2019-09-27", "2019-10-04", 
"2019-10-11", "2019-10-18", "2019-10-25", "2019-11-01", "2019-11-08", 
"2019-11-15", "2019-11-22", "2019-11-29", "2019-12-06", "2019-12-13", 
"2019-12-20", "2019-12-27", "2020-01-03", "2020-01-10", "2020-01-17", 
"2020-01-24", "2020-01-31", "2020-02-07", "2020-02-14", "2020-02-21", 
"2020-02-28", "2020-03-06", "2020-03-13", "2020-03-20", "2020-03-27"
), "%Y-%m-%d")), class = "data.frame", row.names = c(NA, -362L))

df2 <- structure(list(dates = as.Date(c("2013-06-19", "2013-07-17", 
"2013-08-14", "2013-09-18", "2013-10-16", "2013-11-13", "2013-12-18", 
"2014-01-15", "2014-02-12", "2014-03-12", "2014-04-15", "2014-05-14", 
"2014-06-18", "2014-07-16", "2014-08-13", "2014-09-17", "2014-10-15", 
"2014-11-12", "2014-12-17", "2015-01-14", "2015-02-11", "2015-03-11", 
"2015-04-15", "2015-05-13", "2015-06-17", "2015-07-15", "2015-08-12", 
"2015-09-16", "2015-10-14", "2015-11-10", "2015-12-16", "2016-01-13", 
"2016-02-10", "2016-03-16", "2016-04-13", "2016-05-11", "2016-06-15", 
"2016-07-13", "2016-08-10", "2016-09-14", "2016-10-12", "2016-11-09", 
"2016-12-14", "2017-01-11", "2017-02-15", "2017-03-15", "2017-04-19", 
"2017-05-10", "2017-06-14", "2017-07-12", "2017-08-16", "2017-09-13", 
"2017-10-11", "2017-11-15", "2017-12-13", "2018-01-17", "2018-02-14", 
"2018-03-14", "2018-04-18", "2018-05-16", "2018-06-13", "2018-07-11", 
"2018-08-15", "2018-09-19", "2018-10-10", "2018-11-14", "2018-12-12", 
"2019-01-16", "2019-02-13", "2019-03-13", "2019-04-17", "2019-05-15", 
"2019-06-12", "2019-07-17", "2019-08-14", "2019-09-18", "2019-10-16", 
"2019-11-13", "2019-12-11", "2020-01-15", "2020-02-12", "2020-03-11"
), "%Y-%m-%d"), amount = c(0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.253, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.237, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.159, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.19, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.044, 0.15, 0.15, 0.15)), 
class = "data.frame", row.names = c(NA, -82L))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...