根据指定月份创建两个数据框

Create two data frames based on specified months

我有一个年度数据集,我想获得一个只有冬季月份(12、01、02)和夏季月份(06、07、08)的数据集。我该怎么做?

#Example data set
library(lubridate)
date <- rep_len(seq(dmy("26-12-2010"), dmy("20-12-2011"), by = "days"), 500)
df <- data.frame(date = date,
                 x = runif(length(date), min = 60000, max = 80000),
                 y = runif(length(date), min = 800000, max = 900000))

您可以使用 month():

提取月份
df$month <- month(df$date)

然后进行筛选:

summer <- df %>% filter(month %in% 6:8)
winter <- df %>% filter(month %in% c(12,1,2))

您可以像下面那样尝试subset

lapply(
  list(winter = c(1, 2, 12), summer = 6:8),
  function(v) {
    subset(df, month(date) %in% v)
  }
)

这给出了

$winter
          date        x        y
1   2010-12-26 79775.09 865776.3
2   2010-12-27 79903.43 895459.3
3   2010-12-28 66428.75 895675.4
4   2010-12-29 63440.36 891036.3
5   2010-12-30 67441.57 847592.4
6   2010-12-31 70778.98 836922.6
7   2011-01-01 76224.90 838946.9
8   2011-01-02 73973.69 894077.8
9   2011-01-03 77999.07 868630.9
10  2011-01-04 74465.17 841696.4
11  2011-01-05 65101.00 848032.2
12  2011-01-06 66173.43 874708.6
13  2011-01-07 74936.75 873492.1
14  2011-01-08 64104.92 847118.1
15  2011-01-09 65719.67 841139.0
16  2011-01-10 77977.42 840207.9
17  2011-01-11 63359.60 867130.6
18  2011-01-12 62871.93 869074.6
19  2011-01-13 74669.53 805893.9
20  2011-01-14 70856.71 846020.1
21  2011-01-15 70848.13 850554.7
22  2011-01-16 74063.04 815717.0
23  2011-01-17 73029.69 830285.2
24  2011-01-18 75224.14 820021.5
25  2011-01-19 65615.75 811691.7
26  2011-01-20 79681.82 885131.4
27  2011-01-21 79273.43 833977.8
28  2011-01-22 60561.80 806178.0
29  2011-01-23 74941.34 864505.2
30  2011-01-24 67335.53 876368.8
31  2011-01-25 67081.05 853678.7
32  2011-01-26 60387.93 850244.6
33  2011-01-27 75177.88 875599.7
34  2011-01-28 63152.37 888602.2
35  2011-01-29 62245.51 882662.2
36  2011-01-30 74276.27 850088.3
37  2011-01-31 68904.38 879629.6
38  2011-02-01 60522.82 819287.4
39  2011-02-02 78394.90 837278.4
40  2011-02-03 75698.61 840719.8
41  2011-02-04 68502.19 809206.3
42  2011-02-05 62582.77 800201.0
43  2011-02-06 76374.36 838961.0
44  2011-02-07 74336.13 815854.9
45  2011-02-08 68243.75 887893.0
46  2011-02-09 63498.23 859047.9
47  2011-02-10 60215.60 845165.7
48  2011-02-11 66818.05 853270.5
49  2011-02-12 63787.44 805078.5
50  2011-02-13 71246.85 875341.1
51  2011-02-14 71762.59 868842.2
52  2011-02-15 65635.12 812793.5
53  2011-02-16 78519.78 861095.9
54  2011-02-17 74080.45 864969.2
55  2011-02-18 65529.12 834435.6
56  2011-02-19 74303.63 868890.3
57  2011-02-20 72174.36 856881.6
58  2011-02-21 68164.29 827670.6
59  2011-02-22 63346.35 891456.1
60  2011-02-23 60489.26 804604.0
61  2011-02-24 63094.12 890306.2
62  2011-02-25 71731.65 881748.7
63  2011-02-26 64369.72 832408.8
64  2011-02-27 60887.83 841756.9
65  2011-02-28 78290.20 850425.5
341 2011-12-01 66322.64 887622.7
342 2011-12-02 69132.70 814559.8
343 2011-12-03 64774.46 863527.7
344 2011-12-04 73846.85 844623.8
345 2011-12-05 68326.96 892576.5
346 2011-12-06 73795.97 861190.0
347 2011-12-07 78993.65 806906.9
348 2011-12-08 61018.94 851680.8
349 2011-12-09 72372.31 868957.0
350 2011-12-10 74933.04 848490.5
351 2011-12-11 73739.75 861606.4
352 2011-12-12 77489.48 847912.6
353 2011-12-13 75983.82 885264.9
354 2011-12-14 77651.12 826765.8
355 2011-12-15 79617.10 840859.2
356 2011-12-16 70971.79 879961.7
357 2011-12-17 75620.26 887369.2
358 2011-12-18 71589.68 853889.5
359 2011-12-19 61535.48 832817.4
360 2011-12-20 69375.86 857828.9
361 2010-12-26 63013.65 833387.5
362 2010-12-27 71125.47 815849.6
363 2010-12-28 61119.58 851012.4
364 2010-12-29 74730.83 803647.3
365 2010-12-30 60043.92 814247.8
366 2010-12-31 61746.29 817192.7
367 2011-01-01 78032.04 832398.9
368 2011-01-02 79483.16 835357.3
369 2011-01-03 66215.01 886650.1
370 2011-01-04 72839.88 835643.9
371 2011-01-05 73071.35 895071.5
372 2011-01-06 72245.64 848242.3
373 2011-01-07 77626.43 886741.5
374 2011-01-08 75629.93 831762.4
375 2011-01-09 66087.68 833546.7
376 2011-01-10 61080.26 816140.3
377 2011-01-11 68926.37 862494.0
378 2011-01-12 68520.35 818340.9
379 2011-01-13 72738.54 853922.8
380 2011-01-14 74894.05 819617.1
381 2011-01-15 71787.05 810401.0
382 2011-01-16 69257.07 833026.4
383 2011-01-17 64387.44 860156.8
384 2011-01-18 69069.59 869040.9
385 2011-01-19 75351.08 820488.6
386 2011-01-20 65164.80 841502.5
387 2011-01-21 74146.83 814443.0
388 2011-01-22 75574.51 820755.8
389 2011-01-23 67730.03 876592.9
390 2011-01-24 66171.21 848162.7
391 2011-01-25 62215.64 823949.4
392 2011-01-26 69597.81 879640.1
393 2011-01-27 74734.84 844273.1
394 2011-01-28 70354.44 816995.0
395 2011-01-29 78465.70 816509.9
396 2011-01-30 60289.17 896865.0
397 2011-01-31 78325.73 852230.7
398 2011-02-01 71592.19 852253.3
399 2011-02-02 76171.38 868876.3
400 2011-02-03 68671.18 878808.0
401 2011-02-04 66290.00 869235.8
402 2011-02-05 68176.52 887943.0
403 2011-02-06 65951.78 814113.0
404 2011-02-07 62402.90 826524.5
405 2011-02-08 65567.44 889581.2
406 2011-02-09 65511.18 835391.0
407 2011-02-10 66805.61 893227.8
408 2011-02-11 60366.82 832712.5
409 2011-02-12 75217.91 864539.2
410 2011-02-13 68943.30 852134.3
411 2011-02-14 72023.56 856256.7
412 2011-02-15 62097.87 849881.3
413 2011-02-16 60634.02 853082.5
414 2011-02-17 63692.54 838313.7
415 2011-02-18 60628.02 860162.1
416 2011-02-19 78776.37 826318.1
417 2011-02-20 69817.83 864669.9
418 2011-02-21 76049.65 866605.6
419 2011-02-22 71191.87 855441.5
420 2011-02-23 62197.20 830821.2
421 2011-02-24 60210.28 874338.7
422 2011-02-25 68646.22 884705.0
423 2011-02-26 64142.44 836852.3
424 2011-02-27 76125.80 844124.3
425 2011-02-28 70219.88 887062.4

$summer
          date        x        y
158 2011-06-01 72434.00 885655.1
159 2011-06-02 68358.41 818394.2
160 2011-06-03 62973.74 850547.5
161 2011-06-04 63678.50 835556.1
162 2011-06-05 68507.21 882449.6
163 2011-06-06 67782.32 848372.9
164 2011-06-07 61145.06 823191.3
165 2011-06-08 60110.24 827016.3
166 2011-06-09 77659.98 882299.4
167 2011-06-10 66110.95 802583.6
168 2011-06-11 79249.54 840592.6
169 2011-06-12 73515.86 859603.5
170 2011-06-13 77085.95 852874.0
171 2011-06-14 68655.92 868257.1
172 2011-06-15 78511.93 850781.3
173 2011-06-16 75087.54 855437.2
174 2011-06-17 65540.87 884124.0
175 2011-06-18 73730.20 831251.0
176 2011-06-19 78426.95 803594.8
177 2011-06-20 74814.92 881599.4
178 2011-06-21 73450.12 877857.7
179 2011-06-22 74882.05 885937.9
180 2011-06-23 62267.12 806571.9
181 2011-06-24 60821.09 800880.6
182 2011-06-25 67462.41 859168.1
183 2011-06-26 70033.19 886961.5
184 2011-06-27 79007.68 844629.2
185 2011-06-28 78969.13 803757.5
186 2011-06-29 72892.37 841029.1
187 2011-06-30 66308.82 836521.6
188 2011-07-01 70647.32 847658.5
189 2011-07-02 73493.09 813367.8
190 2011-07-03 77143.94 847945.6
191 2011-07-04 71140.21 826431.3
192 2011-07-05 61887.86 848993.3
193 2011-07-06 74224.81 890606.1
194 2011-07-07 76704.43 850099.7
195 2011-07-08 67855.99 870350.7
196 2011-07-09 63894.54 805791.0
197 2011-07-10 65167.91 868931.1
198 2011-07-11 60742.93 879879.5
199 2011-07-12 70573.67 801885.5
200 2011-07-13 71815.25 803681.5
201 2011-07-14 71646.26 857782.2
202 2011-07-15 77940.89 879003.6
203 2011-07-16 68529.68 873526.1
204 2011-07-17 64751.65 826853.8
205 2011-07-18 70250.82 880536.6
206 2011-07-19 61330.17 842493.3
207 2011-07-20 73191.85 816521.5
208 2011-07-21 74692.36 860404.8
209 2011-07-22 64376.40 882576.0
210 2011-07-23 63796.33 878095.1
211 2011-07-24 77455.79 808147.7
212 2011-07-25 65837.34 807075.8
213 2011-07-26 79722.47 895896.0
214 2011-07-27 74582.92 809029.3
215 2011-07-28 78603.79 821225.1
216 2011-07-29 75035.20 843920.4
217 2011-07-30 66495.28 877787.1
218 2011-07-31 67508.14 885140.8
219 2011-08-01 60683.20 862472.4
220 2011-08-02 77896.54 838804.1
221 2011-08-03 70432.23 814375.5
222 2011-08-04 66896.05 888167.3
223 2011-08-05 76212.89 887374.2
224 2011-08-06 75836.48 833862.4
225 2011-08-07 75634.94 851331.3
226 2011-08-08 70640.78 849393.5
227 2011-08-09 62187.92 879343.8
228 2011-08-10 77766.79 891841.5
229 2011-08-11 62656.45 858636.0
230 2011-08-12 76595.49 882348.0
231 2011-08-13 71063.95 880743.9
232 2011-08-14 71145.42 828157.3
233 2011-08-15 74687.38 850864.9
234 2011-08-16 67769.13 829522.9
235 2011-08-17 76173.00 886762.4
236 2011-08-18 77042.09 871336.7
237 2011-08-19 75857.24 836235.1
238 2011-08-20 68339.67 899703.0
239 2011-08-21 63330.57 870100.3
240 2011-08-22 74496.82 823747.9
241 2011-08-23 61893.16 864749.7
242 2011-08-24 61137.48 834395.2
243 2011-08-25 60441.65 880062.6
244 2011-08-26 73045.75 832502.8
245 2011-08-27 73863.94 826728.6
246 2011-08-28 63701.98 806978.1
247 2011-08-29 75923.93 857051.7
248 2011-08-30 75815.98 800417.1
249 2011-08-31 72630.17 828090.2