我有两个要合并/聚合在一起的数据框。第一个数据帧包含价格水平信息和日期。日期范围是星期一至星期日,所以一周7天。
第二个数据框包含股价信息,股价和日期,从星期一至星期五,即一周的5天。
日期列的min
是以下内容;
> min(price_info$WEEKref)
[1] "2008-01-14 CET"
> min(share_info$date)
[1] "2008-01-11"
日期列的max
是
> max(price_info$WEEKref)
[1] "2012-12-31 CET"
> max(share_info$date)
[1] "2012-12-28"
接近但不匹配,这是因为price_info
数据框每周7天,而share_info
则有5天的交易活动。
我想做的是将两个数据框连接在一起,以便星期匹配。
我尝试将mutate
中的dplyr
函数与lubridate
一起使用,例如; mutate(wk = week(WEEKref))
和mutate(yr = year(WEEKref))
,但每周7天和一周5天仍然存在一些问题。
一周中的7天来自可以在周六/周日获取的购买信息,但是股价信息只能从周一至周五获取。
数据1:
price_info <- structure(list(DOLLARS = c(107.92, 12.59, 41.93, 15.87, 11.98,
67.41, 64.71, 107.88, 206.85, 21.96, 131.78, 27.98, 9.9, 16.99,
23.38, 69.95, 19.96, 9.18, 35.95, 90.91, 19.98, 67.95, 18.36,
8.79, 13.99, 7.29, 6.45, 95.94, 23.37, 7.98, 5.6, 62.37, 328.65,
252.64, 8.99, 37.47, 83.94, 55.96, 90.86, 560.49, 16.58, 17.97,
8.99, 36.45, 15.98, 29.91, 16.17, 79.92, 125.86, 12.99, 10.78,
11.98, 19.9, 11.78, 50.94, 139.8, 128.73, 24.38, 9.99, 14.9,
36.28, 89.94, 9.39, 70.74, 456.01, 182.38, 8.36, 23.07, 18.57,
10.78, 42.32, 133.4, 17.99, 62.76, 11.78, 89.91, 5.79, 29.95,
13.98, 45.43, 83.96, 137.48, 27.45, 119.98, 13.45, 16.45, 26.37,
65.94, 93.9, 32.98, 21.98, 223, 17.98, 29.96, 120.89, 38.97,
38.43, 8.49, 29.98, 28.47, 43.74, 103.87, 11.88, 149.9, 29.9,
43.08, 28.95, 87.92, 98.31, 23.98, 38.39, 13.18, 36.33, 12.78,
5.69, 9.99, 53.91, 19.18, 13.86, 26.46, 5.98, 34.14, 13.5, 17.97,
264.76, 83.93, 233.19, 62.91, 118.68, 155.88, 35.97, 11.95, 39.16,
10.98, 11.38, 49.45, 24.87, 54.32, 769.08, 66.95, 61.45, 356.79,
19.08, 3.99, 39.9, 127.92, 48.95, 15.38, 9.58, 12.78, 167.84,
81.59, 37.73, 14.98, 45.63, 41.96, 986.53, 4.99, 59.96, 38.94,
96.73, 39.16, 11, 602.3, 7.49, 129.9, 13.98, 8.58, 33.96, 11.18,
26.97, 8.98, 11.38, 12.49, 153.89, 6.99, 12.58, 71.39, 47.96,
11.97, 7.99, 58.03, 35.97, 38.43, 47.96, 6.98, 29.16, 6.49, 5.19,
29.98, 9.16, 70.9, 5.99, 21.98, 39.13, 143.91, 1.89, 82.92, 11.98,
11.61, 19.35, 6.59, 10.36, 107.92, 59.94, 41.97, 20.07, 67.96,
39.92, 27.96, 10.99, 15.12, 14.76, 111.92, 19.95, 19.47, 43.95,
42.21, 9.79, 104.93, 17.49, 27.98, 672.49, 29.67, 14.98, 19.97,
287.84, 48.95, 27.45, 6.99, 2.79, 341.81, 4.29, 17.97, 44.76,
155.76, 49.76, 44.73, 14.99, 21.45, 16.99, 95.94, 40.47, 57.45,
51.92, 13.98, 92.9, 15.99, 25.98, 5.99, 5.29, 181.87, 62.01,
87.89, 17.92, 48.93, 89.94, 139.93, 11.99, 8.49, 37.45, 16.77,
51.92, 51.15, 49.95, 61.96, 32.58, 9.38, 170.39, 1444, 39.98,
11.99, 25.74, 19.47, 29.95, 23.96, 28.45, 5.29, 9.78, 23.97,
391.72, 20.19, 16.99, 22.47, 195.72, 17.99, 123.31, 139.9, 23.34,
35.98, 65.91, 48.93, 129.9, 143.91, 53.97, 644.57, 29.98, 30.32,
99.95, 7.79, 17.97, 13.98, 29.97, 8.37, 3.18, 25.95, 29.96, 5.29,
9.16, 18.98, 97.9, 31.96, 29.97, 151.85, 26.97, 389.74, 27.96,
368.7, 14.29, 11.58, 65.94, 7.99, 124.9, 20.98, 65.94, 119.95,
27.92, 44.97, 7.39, 1.99, 49.41, 8.39, 8.69, 56.72, 17.98, 1.65,
90.93, 20.98, 128.28, 175.84, 90.72, 25.96, 21.17, 60.39, 51.55,
16.98, 44.97, 84.95, 146.9, 47.92, 20.97, 637.71, 179.8, 75.92,
11.96, 7.98, 23.9, 39.92, 9.73, 21.96, 181.39, 47.97, 9.96, 262.15,
5.49, 12.38, 13.99, 48.51, 338.76, 29.97, 91.92, 1941.84, 25.96,
12.99, 8.99, 47.96, 8.99, 9.99, 15.99, 17.58, 28.17, 51.88, 12.38,
91.92, 87.43, 12.99, 73.43, 64.95, 23.97, 29.97, 19.47, 96.93,
13.99, 73.97, 148.41, 13.99, 74.69, 164.85, 23.96, 63.95, 27.92,
23.96, 14.98, 63.92, 6.98, 9.45, 9.58, 11.87, 80.43, 49.98, 137.94,
15.29, 64.9, 5.49, 79.4, 155.88, 17.59, 24.95, 98.91, 23.16,
179.82, 22.77, 13.98, 29.58, 39.8, 71.92, 59.95, 56.34, 27.38,
12.49, 74.95, 35.75, 21.99, 15.18, 3.38, 97.64, 13.52, 197.91,
26.45, 3.18, 168.87, 91.04, 23.96, 119.8, 234.96, 55.46, 8.49,
13.98, 29.97, 26.36, 13.65, 35.94, 87.95, 9.49, 7.49, 40.46,
59.9, 17.98, 44.95, 18.39, 21.98, 32.97, 44.97, 10.9, 14.99,
7.49, 7.49, 53.91, 58.95, 14.97, 125.82, 15.98, 13.17, 71.91,
503.79, 113.28, 5.79, 6.99, 30.95, 35.91, 1.39, 24.98, 6.59,
197.28, 52.44, 5.49, 29.85, 9.95, 42.4, 19.95, 89.94, 64.95,
13.49, 6.95, 46.95, 55.46, 111.39, 118.93, 12.98, 20.28, 459.54,
40.96, 39.83, 54.45, 18.98, 69.93, 65.9, 16.74, 32.98, 179.85,
83.48, 83.94, 28.45, 6.99, 28.45, 18.58, 13.99, 13.78, 11.99,
319.68, 69.95, 43.56, 11.83, 83.94, 13.99, 12.53, 83.88, 40.47,
16.47, 10.39, 47.8, 6.29, 21.98, 101.31, 28.95, 5.98, 5.99, 21.89,
10.58, 64.95, 35.37, 3.99, 111.84, 23.97, 73.89, 5.5, 194.9,
361.81, 16.99, 49.14, 10.99, 16.47, 54.95, 131.88, 11.94, 3.18,
23.67, 41.97, 54, 137.77, 321.96, 22.33, 7.99, 4.98, 10.99, 5.49,
13.98, 10.49, 10.49, 11.98, 12.99, 6.59, 20.97, 7.95, 14.99,
43.98, 6.99, 80.43, 2.58, 14.98, 8.29, 53.97, 29.98, 134.86,
25.98, 22.47, 22.99, 21.97, 35.94, 10.99, 5.79, 109.89, 12.99,
33.44, 10.98, 29.97, 11.59, 41.93, 203.83, 12.78, 33.98, 115.39,
75.96, 10.49, 319.8, 33.96, 59.92, 17.97, 10.99, 47.97, 65.94,
29.95, 32.97, 25.96, 41.16, 59.7, 32.98, 41.94, 283.36, 10.98,
125.93, 15.58, 11.12, 46.94, 11.99, 55.56, 11.98, 209.86, 69.93,
314.79, 7.49, 57.8, 6.49, 41.97, 71.94, 92.94, 6.99, 421.43,
14.69, 13.98, 301.07, 15.98, 21.87, 35.16, 22.47, 10.99, 14.99,
6.49, 102.24, 32.97, 13.5, 16.49, 24.76, 141.57, 9.99, 21.98,
79.92, 11.59, 5.56, 131.88, 17.97, 51.24, 19.77, 6.18, 10.16,
8.38, 17.97, 40.47, 635.62, 55.46, 127.92, 26.91, 65.97, 38.43,
7.99, 23.96, 142.85, 41.94, 6.99, 26.82, 23.98, 28.47, 8.49,
11.34, 15.66, 19.38, 28.98, 7.29, 111.14, 79.96, 56.34, 12.99,
6.49, 15.95, 19.74, 17.48, 7.99, 62.32, 22.58, 14.34, 71.92,
29.95, 37.8, 5.59, 17.98, 31.45, 33.98, 15.19, 20.37, 23.94,
115.35, 13.99, 47.94, 14.98, 239.85, 15.98, 16.98, 181.73, 65.97,
415.68, 74.97, 38.97, 55.93, 34.47, 67.41, 19.98, 161.72, 134.85,
63.96, 8.99, 27.96, 6.99, 22.99, 51.73, 15.99, 83.61, 39.98,
37.8, 12.98, 23.07, 7.79, 23.98, 95.22, 38.97, 15.12, 14.98,
35.98, 89.94, 40.42, 43.74, 32.88, 78.27, 33.84, 10.99, 3.98,
59.95, 1.89, 119.61, 3.99, 32.45, 64.95, 13.99, 4.29, 181.48,
20.99, 244.35, 55.92, 8.49, 8.79, 59.43, 108.68, 12.99, 7.49,
19.99, 31.47, 147.92, 9.36, 25.11, 99.16, 18.99, 5.99, 29.97,
13.99, 49.77, 7.45, 13.99, 15.9, 129.9, 57.28, 27.98, 26.98,
71.91, 315.5, 6.99, 6.99, 5.58, 22.41, 29.95, 239.76, 32.97,
16.98, 3.87, 186.87, 35.82, 7.99, 13.58, 215.82, 227.4, 20.79,
25.96, 17.99, 7.69, 23.98, 7.49, 94.95, 239.82, 1.89, 23.98,
102.06, 33.87, 29.95, 18.99, 37.98, 51.92, 254.83, 6.49, 13.47,
38.97, 31.41, 57.87, 21.98, 95.94, 125.91, 12.29, 119.92, 6.39,
23.98, 14.99, 17.98, 47.97, 69.9, 57.51, 45.96, 71.88, 53.91,
39.98, 12.29, 34.7, 6.99, 23.98, 13.58, 11.94, 43.98, 16.49,
5.39, 10.95, 29.96, 9.38, 139.8, 102.83, 27.98, 239.7, 31.98,
152.46, 11.99, 87.45, 9.39, 16.38, 6.99, 7.99, 41.97, 44.95,
8.99, 12.49, 17.97, 125.93, 7.99, 2.19, 7.49, 35.97, 77.94, 95.88,
25.98, 7.79, 59.95, 168.92, 1.99, 47.97, 229.77, 21.98, 9.39,
6.99, 22.47, 29.98, 241.98, 159.9, 31.96, 25.58, 31.47, 9.98,
19.98, 29.88, 34.95, 10.45, 125.93, 97.9, 69.19, 33.56, 6.79,
31.79, 13.98, 33.67, 164.88, 22.47, 13.99, 279.83, 34.47, 31.96,
41.97, 15.99, 59.95, 5.97, 95.92, 11.98, 27.96, 31.58, 7.96,
17.98, 26.99, 7.49, 2.59, 223.89, 247.35, 14.99, 49.95, 85.74,
26.76, 83.88, 35.97, 42.45, 71.96, 243.64, 515.57, 21.96, 14.97,
25.02, 13.12, 27.98, 29.96, 82.19, 63.92, 6.99, 26.37, 31.98,
27.96, 7.89, 41.94, 31.96, 75.88, 61.96, 188.36, 14.59, 37.73,
25.96, 103.92, 71.94, 12.99, 21.98, 111.92, 31.74, 9.99, 77.87,
95.83, 27.92, 6.49, 7.99, 129.87, 19.49, 12.45, 19.99, 83.16,
7.49, 138.58, 208.89, 7.19, 8.76, 13.98, 28.94, 32.97, 111.92,
17.98, 275.77, 34.93, 27.98, 13.98, 11.99, 15.98, 13.58, 11.3,
13.16), WEEKref = structure(c(1199660400, 1199660400, 1199660400,
1199660400, 1199660400, 1200265200, 1200265200, 1200265200, 1200870000,
1200870000, 1201474800, 1202079600, 1202079600, 1202684400, 1202684400,
1203289200, 1203289200, 1203289200, 1203289200, 1203289200, 1203894000,
1203894000, 1203894000, 1204498800, 1204498800, 1205103600, 1205103600,
1205103600, 1205103600, 1205708400, 1205708400, 1205708400, 1205708400,
1205708400, 1206313200, 1206313200, 1206313200, 1206313200, 1206313200,
1206914400, 1206914400, 1206914400, 1206914400, 1206914400, 1206914400,
1207519200, 1208124000, 1208728800, 1208728800, 1208728800, 1208728800,
1208728800, 1208728800, 1208728800, 1209333600, 1209333600, 1209333600,
1209333600, 1209938400, 1209938400, 1209938400, 1210543200, 1210543200,
1210543200, 1210543200, 1210543200, 1211148000, 1211148000, 1211148000,
1211148000, 1211752800, 1211752800, 1212357600, 1212357600, 1212357600,
1212962400, 1212962400, 1212962400, 1212962400, 1213567200, 1213567200,
1213567200, 1213567200, 1214172000, 1214776800, 1214776800, 1214776800,
1214776800, 1214776800, 1214776800, 1214776800, 1214776800, 1215381600,
1215381600, 1215381600, 1215986400, 1215986400, 1216591200, 1216591200,
1216591200, 1216591200, 1216591200, 1217196000, 1217196000, 1217196000,
1217196000, 1217800800, 1217800800, 1217800800, 1217800800, 1217800800,
1217800800, 1218405600, 1218405600, 1218405600, 1218405600, 1218405600,
1218405600, 1218405600, 1218405600, 1219010400, 1219010400, 1219010400,
1219010400, 1219010400, 1219010400, 1219010400, 1219010400, 1219615200,
1219615200, 1219615200, 1219615200, 1220220000, 1220220000, 1220824800,
1220824800, 1220824800, 1221429600, 1221429600, 1221429600, 1221429600,
1222639200, 1222639200, 1222639200, 1223244000, 1223244000, 1223244000,
1223244000, 1223848800, 1223848800, 1223848800, 1223848800, 1224453600,
1224453600, 1224453600, 1224453600, 1225062000, 1225062000, 1225062000,
1225062000, 1225666800, 1225666800, 1225666800, 1225666800, 1225666800,
1226271600, 1226271600, 1226271600, 1226271600, 1226271600, 1226876400,
1226876400, 1227481200, 1227481200, 1228086000, 1228086000, 1228690800,
1228690800, 1229295600, 1229295600, 1229295600, 1229900400, 1229900400,
1230505200, 1230505200, 1230505200, 1230505200, 1231110000, 1231110000,
1231714800, 1231714800, 1231714800, 1232319600, 1232924400, 1232924400,
1232924400, 1232924400, 1232924400, 1232924400, 1232924400, 1232924400,
1232924400, 1232924400, 1233529200, 1233529200, 1233529200, 1233529200,
1233529200, 1234134000, 1234134000, 1234738800, 1234738800, 1234738800,
1234738800, 1235343600, 1235343600, 1235343600, 1235343600, 1235343600,
1235948400, 1235948400, 1235948400, 1236553200, 1236553200, 1236553200,
1237158000, 1237158000, 1237762800, 1237762800, 1238364000, 1238364000,
1238364000, 1238968800, 1238968800, 1238968800, 1238968800, 1238968800,
1238968800, 1239573600, 1240178400, 1240178400, 1240178400, 1240178400,
1240783200, 1240783200, 1240783200, 1241388000, 1241388000, 1241992800,
1241992800, 1241992800, 1241992800, 1241992800, 1242597600, 1242597600,
1242597600, 1242597600, 1242597600, 1242597600, 1242597600, 1243202400,
1243202400, 1243202400, 1243202400, 1243202400, 1243202400, 1243807200,
1243807200, 1243807200, 1243807200, 1244412000, 1244412000, 1244412000,
1244412000, 1244412000, 1245016800, 1245016800, 1245016800, 1245621600,
1245621600, 1245621600, 1245621600, 1245621600, 1245621600, 1246226400,
1246226400, 1246226400, 1246226400, 1246226400, 1246226400, 1246226400,
1246226400, 1246226400, 1246226400, 1246831200, 1246831200, 1246831200,
1246831200, 1246831200, 1247436000, 1247436000, 1247436000, 1247436000,
1247436000, 1248040800, 1248040800, 1248040800, 1248040800, 1248040800,
1248040800, 1248040800, 1248040800, 1248645600, 1248645600, 1249250400,
1249250400, 1249250400, 1249250400, 1249855200, 1249855200, 1249855200,
1250460000, 1250460000, 1250460000, 1251064800, 1251064800, 1251064800,
1251064800, 1252274400, 1252274400, 1252274400, 1252274400, 1252274400,
1252274400, 1252879200, 1252879200, 1252879200, 1252879200, 1252879200,
1252879200, 1252879200, 1253484000, 1253484000, 1253484000, 1253484000,
1253484000, 1254088800, 1254088800, 1254088800, 1254088800, 1254693600,
1254693600, 1254693600, 1255298400, 1255298400, 1255298400, 1255903200,
1255903200, 1255903200, 1255903200, 1255903200, 1256511600, 1256511600,
1256511600, 1257116400, 1257721200, 1257721200, 1257721200, 1257721200,
1257721200, 1258326000, 1258326000, 1258326000, 1258930800, 1258930800,
1258930800, 1258930800, 1258930800, 1258930800, 1259535600, 1259535600,
1260140400, 1260745200, 1260745200, 1261350000, 1261350000, 1261350000,
1261350000, 1261954800, 1261954800, 1261954800, 1262559600, 1262559600,
1262559600, 1262559600, 1262559600, 1262559600, 1263164400, 1263164400,
1263164400, 1263769200, 1263769200, 1263769200, 1263769200, 1263769200,
1263769200, 1264374000, 1264374000, 1264978800, 1264978800, 1264978800,
1264978800, 1265583600, 1265583600, 1265583600, 1265583600, 1265583600,
1266188400, 1266188400, 1266793200, 1266793200, 1266793200, 1266793200,
1266793200, 1267398000, 1267398000, 1267398000, 1267398000, 1267398000,
1267398000, 1268002800, 1268002800, 1268002800, 1268002800, 1268002800,
1268607600, 1268607600, 1268607600, 1268607600, 1269212400, 1269212400,
1269212400, 1269212400, 1269212400, 1269212400, 1269813600, 1269813600,
1269813600, 1269813600, 1269813600, 1269813600, 1269813600, 1269813600,
1270418400, 1270418400, 1271023200, 1271023200, 1271023200, 1271023200,
1271628000, 1271628000, 1272232800, 1272232800, 1272232800, 1272837600,
1272837600, 1272837600, 1272837600, 1273442400, 1273442400, 1273442400,
1273442400, 1273442400, 1273442400, 1273442400, 1274047200, 1274047200,
1274652000, 1274652000, 1274652000, 1274652000, 1274652000, 1274652000,
1275256800, 1275256800, 1275256800, 1275256800, 1275256800, 1275861600,
1275861600, 1275861600, 1275861600, 1275861600, 1275861600, 1275861600,
1275861600, 1276466400, 1276466400, 1276466400, 1276466400, 1276466400,
1277071200, 1277071200, 1277071200, 1277676000, 1277676000, 1277676000,
1277676000, 1278280800, 1278280800, 1278280800, 1278280800, 1278885600,
1278885600, 1278885600, 1278885600, 1278885600, 1278885600, 1278885600,
1278885600, 1278885600, 1279490400, 1279490400, 1279490400, 1279490400,
1279490400, 1280095200, 1280095200, 1280700000, 1280700000, 1280700000,
1281304800, 1281304800, 1281304800, 1281304800, 1281304800, 1281909600,
1281909600, 1281909600, 1282514400, 1282514400, 1282514400, 1282514400,
1283119200, 1283119200, 1283119200, 1283119200, 1283119200, 1283119200,
1283724000, 1283724000, 1283724000, 1283724000, 1283724000, 1283724000,
1284328800, 1284328800, 1284328800, 1284933600, 1284933600, 1284933600,
1285538400, 1285538400, 1285538400, 1285538400, 1285538400, 1286143200,
1286143200, 1286143200, 1286748000, 1286748000, 1286748000, 1287352800,
1287352800, 1287352800, 1287352800, 1287957600, 1287957600, 1287957600,
1288566000, 1288566000, 1289170800, 1289170800, 1289170800, 1289170800,
1289170800, 1289170800, 1289775600, 1289775600, 1289775600, 1289775600,
1290380400, 1290380400, 1290985200, 1290985200, 1290985200, 1290985200,
1291590000, 1291590000, 1291590000, 1291590000, 1291590000, 1292194800,
1292194800, 1292194800, 1292194800, 1292799600, 1292799600, 1292799600,
1292799600, 1292799600, 1292799600, 1293404400, 1293404400, 1293404400,
1293404400, 1293404400, 1293404400, 1294009200, 1294614000, 1294614000,
1294614000, 1295218800, 1295218800, 1295218800, 1295218800, 1295218800,
1295823600, 1296428400, 1296428400, 1296428400, 1296428400, 1296428400,
1296428400, 1297033200, 1297638000, 1297638000, 1297638000, 1297638000,
1298242800, 1298242800, 1298242800, 1298242800, 1298847600, 1298847600,
1298847600, 1298847600, 1299452400, 1299452400, 1299452400, 1300057200,
1300057200, 1300057200, 1300057200, 1300662000, 1300662000, 1301263200,
1301868000, 1301868000, 1302472800, 1302472800, 1302472800, 1302472800,
1302472800, 1303077600, 1303077600, 1303077600, 1303077600, 1303682400,
1303682400, 1304287200, 1304287200, 1304287200, 1304287200, 1304892000,
1304892000, 1304892000, 1304892000, 1305496800, 1305496800, 1305496800,
1305496800, 1306101600, 1306101600, 1306101600, 1306101600, 1306706400,
1306706400, 1307311200, 1307311200, 1307311200, 1307311200, 1307311200,
1307916000, 1307916000, 1307916000, 1307916000, 1307916000, 1308520800,
1308520800, 1309125600, 1309125600, 1309125600, 1309730400, 1309730400,
1309730400, 1309730400, 1309730400, 1309730400, 1310335200, 1310335200,
1310335200, 1310335200, 1310335200, 1310940000, 1310940000, 1311544800,
1311544800, 1312149600, 1312149600, 1312149600, 1312149600, 1312149600,
1312754400, 1312754400, 1312754400, 1312754400, 1312754400, 1313359200,
1313964000, 1313964000, 1313964000, 1314568800, 1314568800, 1314568800,
1314568800, 1315173600, 1315173600, 1315173600, 1315173600, 1315173600,
1315778400, 1315778400, 1315778400, 1315778400, 1315778400, 1316383200,
1316383200, 1316383200, 1316988000, 1316988000, 1317592800, 1317592800,
1318197600, 1318197600, 1318197600, 1318802400, 1318802400, 1318802400,
1318802400, 1319407200, 1319407200, 1320015600, 1320015600, 1320015600,
1320015600, 1320015600, 1320620400, 1320620400, 1321225200, 1321225200,
1321225200, 1321225200, 1321225200, 1321225200, 1321225200, 1321225200,
1321225200, 1321225200, 1321830000, 1321830000, 1322434800, 1323039600,
1323039600, 1323039600, 1323039600, 1323039600, 1323644400, 1323644400,
1323644400, 1323644400, 1323644400, 1323644400, 1323644400, 1324249200,
1324249200, 1324249200, 1324249200, 1324249200, 1324854000, 1324854000,
1324854000, 1324854000, 1325458800, 1325458800, 1325458800, 1325458800,
1326063600, 1326063600, 1326063600, 1326668400, 1326668400, 1326668400,
1326668400, 1326668400, 1326668400, 1327273200, 1327273200, 1327273200,
1327878000, 1327878000, 1327878000, 1327878000, 1327878000, 1328482800,
1328482800, 1328482800, 1328482800, 1329087600, 1329087600, 1329087600,
1329087600, 1329087600, 1329692400, 1329692400, 1329692400, 1329692400,
1329692400, 1330297200, 1330297200, 1330297200, 1330297200, 1330297200,
1330297200, 1330297200, 1330297200, 1330297200, 1330902000, 1330902000,
1330902000, 1330902000, 1330902000, 1331506800, 1331506800, 1331506800,
1331506800, 1331506800, 1332111600, 1332111600, 1332111600, 1332712800,
1332712800, 1332712800, 1332712800, 1333317600, 1333317600, 1333317600,
1333317600, 1334527200, 1334527200, 1334527200, 1334527200, 1335132000,
1335132000, 1335132000, 1335132000, 1335132000, 1335132000, 1335736800,
1335736800, 1335736800, 1335736800, 1335736800, 1335736800, 1335736800,
1336341600, 1336341600, 1336341600, 1336341600, 1336946400, 1336946400,
1336946400, 1337551200, 1337551200, 1337551200, 1337551200, 1337551200,
1338156000, 1338156000, 1338760800, 1338760800, 1338760800, 1339365600,
1339365600, 1339365600, 1339365600, 1339970400, 1339970400, 1340575200,
1340575200, 1340575200, 1341180000, 1341180000, 1341180000, 1341784800,
1341784800, 1341784800, 1342389600, 1342389600, 1342994400, 1342994400,
1342994400, 1342994400, 1343599200, 1343599200, 1343599200, 1343599200,
1344204000, 1344204000, 1344204000, 1344204000, 1344808800, 1344808800,
1344808800, 1345413600, 1346018400, 1346018400, 1346018400, 1346623200,
1346623200, 1346623200, 1346623200, 1347228000, 1347228000, 1347228000,
1347228000, 1347228000, 1347832800, 1347832800, 1347832800, 1347832800,
1348437600, 1348437600, 1349042400, 1349042400, 1349042400, 1349042400,
1349042400, 1349042400, 1349647200, 1349647200, 1349647200, 1349647200,
1350252000, 1350252000, 1350252000, 1350252000, 1350856800, 1350856800,
1350856800, 1350856800, 1350856800, 1351465200, 1351465200, 1351465200,
1352070000, 1352070000, 1352070000, 1352070000, 1352070000, 1352674800,
1352674800, 1352674800, 1352674800, 1352674800, 1353279600, 1353279600,
1353279600, 1353884400, 1353884400, 1354489200, 1354489200, 1354489200,
1354489200, 1354489200, 1355094000, 1355094000, 1355698800, 1355698800,
1355698800), class = c("POSIXct", "POSIXt"), tzone = "")), class = "data.frame", row.names = c(NA,
-1000L))
数据2:
share_info <- structure(list(date = structure(c(13889, 13896, 13903, 13910,
13917, 13924, 13931, 13938, 13945, 13952, 13958, 13966, 13973,
13980, 13987, 13994, 14001, 14008, 14015, 14022, 14029, 14036,
14043, 14050, 14057, 14064, 14071, 14078, 14085, 14092, 14099,
14106, 14113, 14120, 14127, 14134, 14141, 14148, 14155, 14162,
14169, 14176, 14183, 14190, 14197, 14204, 14211, 14218, 14225,
14232, 14237, 14246, 14253, 14260, 14267, 14274, 14281, 14288,
14295, 14302, 14309, 14316, 14323, 14330, 14337, 14343, 14351,
14358, 14364, 14372, 14379, 14386, 14393, 14400, 14407, 14414,
14421, 14428, 14435, 14442, 14449, 14456, 14463, 14470, 14477,
14484, 14491, 14498, 14505, 14512, 14519, 14526, 14533, 14540,
14547, 14554, 14561, 14568, 14575, 14582, 14589, 14596, 14602,
14609, 14617, 14624, 14631, 14638, 14645, 14652, 14659, 14666,
14673, 14680, 14687, 14694, 14700, 14708, 14715, 14722, 14729,
14736, 14743, 14750, 14757, 14764, 14771, 14778, 14785, 14792,
14799, 14806, 14813, 14820, 14827, 14834, 14841, 14848, 14855,
14862, 14869, 14876, 14883, 14890, 14897, 14904, 14911, 14918,
14925, 14932, 14939, 14946, 14953, 14960, 14967, 14973, 14981,
14988, 14995, 15002, 15009, 15016, 15023, 15030, 15037, 15044,
15051, 15058, 15065, 15072, 15079, 15085, 15093, 15100, 15107,
15114, 15121, 15128, 15135, 15142, 15149, 15156, 15163, 15170,
15177, 15184, 15191, 15198, 15205, 15212, 15219, 15226, 15233,
15240, 15247, 15254, 15261, 15268, 15275, 15282, 15289, 15296,
15303, 15310, 15317, 15324, 15331, 15338, 15345, 15352, 15359,
15366, 15373, 15380, 15387, 15394, 15401, 15408, 15415, 15422,
15429, 15435, 15443, 15450, 15457, 15464, 15471, 15478, 15485,
15492, 15499, 15506, 15513, 15520, 15527, 15534, 15541, 15548,
15555, 15562, 15569, 15576, 15583, 15590, 15597, 15604, 15611,
15618, 15625, 15632, 15639, 15646, 15653, 15660, 15667, 15674,
15681, 15688, 15695, 15702), class = "Date"), adjusted = c(24.747421,
22.497637, 22.836473, 25.795502, 24.485403, 24.995857, 24.47637,
27.1056, 26.224661, 26.631205, 25.623804, 24.910007, 25.799982,
26.644787, 26.848055, 25.678005, 25.739576, 24.639721, 24.513144,
23.788029, 24.138477, 23.014256, 24.055735, 23.165115, 21.48127,
20.172163, 21.656496, 19.899628, 21.315849, 20.668577, 23.272152,
23.846483, 22.113907, 23.072638, 23.058084, 23.564222, 22.474094,
20.12351, 19.466503, 15.952772, 14.539044, 13.614408, 15.317719,
14.979488, 13.886943, 10.025247, 10.03412, 9.847294, 10.995496,
11.388611, 12.525135, 13.887408, 14.420643, 13.315253, 14.362259,
15.498786, 15.958066, 15.592198, 16.051481, 16.970041, 14.681421,
16.588604, 15.576628, 15.790699, 16.432917, 16.15657, 17.351479,
17.514948, 18.374041, 19.552214, 18.988743, 19.236984, 19.504929,
19.713776, 19.229105, 19.307913, 20.174797, 21.494829, 21.234766,
21.526354, 22.022842, 21.999205, 21.817942, 21.408142, 23.208899,
23.32711, 22.937016, 24.509232, 25.183033, 24.036385, 23.642344,
25.33671, 26.416378, 26.648861, 25.222443, 25.557373, 26.455784,
26.711908, 26.640984, 27.897966, 28.17379, 27.724586, 28.055584,
28.682098, 27.830976, 27.582735, 27.235985, 28.489027, 26.849821,
28.496906, 29.379555, 28.969753, 28.646639, 29.080082, 30.002136,
29.580503, 29.533228, 30.333126, 28.95793, 29.036737, 29.150679,
28.676779, 30.393166, 30.405113, 31.165739, 31.157778, 32.113533,
32.854248, 31.982113, 30.88299, 32.822392, 32.726814, 33.411774,
32.364422, 31.209545, 33.411774, 32.388309, 32.388309, 33.837879,
34.244083, 33.690536, 34.403378, 33.933456, 35.044529, 35.625946,
36.438347, 35.868874, 34.710014, 34.248062, 35.394981, 34.566654,
34.419308, 34.606468, 34.483021, 34.845409, 34.327713, 34.793644,
34.387444, 32.671062, 32.181229, 32.324596, 32.862213, 31.89052,
31.870615, 33.05336, 32.710888, 30.998482, 32.511768, 33.097168,
32.603359, 33.897617, 34.152485, 34.927811, 33.499866, 34.075912,
34.538372, 33.284866, 32.96439, 32.226078, 32.453251, 31.76362,
32.485706, 32.737213, 31.869093, 33.507977, 32.615517, 30.461426,
31.126724, 30.428984, 30.145012, 31.130779, 30.566896, 30.546627,
30.761627, 32.270702, 31.512108, 32.453251, 32.453251, 32.400517,
32.04353, 34.927811, 34.749317, 34.647903, 36.11235, 35.864899,
35.694515, 37.292839, 38.380024, 37.661999, 38.039265, 38.858711,
37.808041, 39.702496, 39.382019, 40.363724, 39.941837, 41.069592,
43.089802, 44.444729, 44.152653, 44.444729, 43.617168, 43.909245,
44.78548, 45.684174, 46.372334, 46.47184, 44.266388, 45.335945,
43.943039, 45.095505, 46.57962, 46.1236, 50.824677, 52.109806,
52.234173, 52.980377, 54.265507, 55.044865, 54.597145, 54.56398,
54.224049, 55.368217, 56.45435, 53.908981, 55.674995, 54.862461,
57.557083, 55.658413, 54.713223, 54.671764, 53.900688, 54.083096,
53.560753, 55.459427, 55.873985, 56.039803, 55.66671, 55.111202,
54.52253)), row.names = c(NA, -260L), class = c("tbl_df", "tbl",
"data.frame"))
EDIT1:
为了澄清一点,使用以下方法获取了股价数据;
library(tidyquant)
start_date <- "2008-01-07"
end_date <- "2012-12-31"
ABI <- tq_get("ABI.BR",
get = "stock.prices",
from = start_date,
to = end_date)
给出1277
的观察结果,然后我使用tq_mutate
转换为每周数据:
weeklyABI <- ABI %>%
tq_transmute(select = adjusted,
mutate_fun = to.weekly)
其中share_info <- weeklyABI
EDIT2:添加了1000个观察值
答案 0 :(得分:1)
您可以使用模除来构造周标记并合并这些值。可能要检查日历以查看是否需要抵消:
share_info$week_n <- paste( format(share_info$date,"%Y"),
sprintf( "%02i", as.POSIXlt(share_info$date, origin="1970-01-01")$yday %/% 7),
sep="_")
str( share_info$week_n )
# chr [1:260] "2008_01" "2008_02" "2008_03" "2008_04" "2008_05" "2008_06" "2008_07" "2008_08" "2008_09" "2008_10" "2008_11" ...
price_info$week_n <- paste( format(price_info$WEEKref,"%Y"),
sprintf( "%02i", as.POSIXlt(price_info$WEEKre, origin="1970-01-01")$yday %/% 7),
sep="_")
str( price_info$week_n )
# chr [1:500] "2008_01" "2008_01" "2008_02" "2008_04" "2008_06" "2008_07" "2008_07" "2008_08" "2008_10" "2008_10" "2008_11" ...
agg.price <- aggregate(price_info$DOLLARS, price_info['week_n'], FUN=mean, na.rm=TRUE)
agg.share <- aggregate(share_info$adjusted, share_info['week_n'], FUN=mean, na.rm=TRUE)
head( merge( price_info, share_info, by="week_n"))
#-----------------------------------------------------------
week_n DOLLARS WEEKref date adjusted
1 2008_01 3.69 2008-01-13 15:00:00 2008-01-11 24.74742
2 2008_01 17.98 2008-01-13 15:00:00 2008-01-11 24.74742
3 2008_02 15.00 2008-01-20 15:00:00 2008-01-18 22.49764
4 2008_04 63.48 2008-02-03 15:00:00 2008-02-01 25.79550
5 2008_06 24.90 2008-02-17 15:00:00 2008-02-15 24.99586
6 2008_07 2.69 2008-02-24 15:00:00 2008-02-22 24.47637
>
我意识到我需要使用paste(year,week)
作为汇总和合并指标