I have two data frames that I am trying to merge/aggregate together. The first data frame consists of price level information and a date. The dates range from Monday - Sunday so, 7 days a week.
The second data frame contains share price information, share price and a date, from Monday - Friday, so 5 days of the week.
The min of the date columns is the following;
> min(price_info$WEEKref)
[1] "2008-01-14 CET"
> min(share_info$date)
[1] "2008-01-11"
The max of the date columns are;
> max(price_info$WEEKref)
[1] "2012-12-31 CET"
> max(share_info$date)
[1] "2012-12-28"
which are close but not matching, which is because the price_info data frame has 7 days a week and the share_info has 5 days of trading activity.
What I am trying to do is to join both data frame together so that the weeks match.
I have tried using a mutate function from dplyr with lubridate such as; mutate(wk = week(WEEKref)) and mutate(yr = year(WEEKref)) but I am still having some problems with the 7 days of the week vs the 5 days of the week.
The 7 days of the week comes from purchase information which can be made available on Sat/Sun however the share price information can only be obtained from Monday - Friday.
Data 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))
Data 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:
To clarify some point, the share price data was obtained using the following;
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)
Which gives 1277 observations, I then used tq_mutate to convert to weekly data:
weeklyABI <- ABI %>%
tq_transmute(select = adjusted,
mutate_fun = to.weekly)
where share_info <- weeklyABI
EDIT2: Added 1000 observations
You can use modulo division to construct a week marker and merge on those values. May want to check a calendar to see if any are needing to be offset:
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
>
I realized I needed to use paste(year,week) as the aggregate and merge indicator
Related
I have a data frame and am trying to fill the missing values with the previous and next values in the data frame. I used the following code, but it did not fill and returns missing values still. I tried 2 different variations, but both do not work. Could someone please let me know what I am doing wrong? Thanks!
Data frame: 'oil.csv'
date dcoilwtico
1/1/13
1/2/13 93.14
1/3/13 92.97
1/4/13 93.12
1/7/13 93.2
1/8/13 93.21
1/9/13 93.08
1/10/13 93.81
1/11/13 93.6
1/14/13 94.27
1/15/13 93.26
1/16/13 94.28
1/17/13 95.49
1/18/13 95.61
1/21/13
1/22/13 96.09
dput(oil_df):
structure(list(date = c("2013-01-01", "2013-01-02", "2013-01-03",
"2013-01-04", "2013-01-07", "2013-01-08", "2013-01-09", "2013-01-10",
"2013-01-11", "2013-01-14", "2013-01-15", "2013-01-16", "2013-01-17",
"2013-01-18", "2013-01-21", "2013-01-22", "2013-01-23", "2013-01-24",
"2013-01-25", "2013-01-28", "2013-01-29", "2013-01-30", "2013-01-31",
"2013-02-01", "2013-02-04", "2013-02-05", "2013-02-06", "2013-02-07",
"2013-02-08", "2013-02-11", "2013-02-12", "2013-02-13", "2013-02-14",
"2013-02-15", "2013-02-18", "2013-02-19", "2013-02-20", "2013-02-21",
"2013-02-22", "2013-02-25", "2013-02-26", "2013-02-27", "2013-02-28",
"2013-03-01", "2013-03-04", "2013-03-05", "2013-03-06", "2013-03-07",
"2013-03-08", "2013-03-11", "2013-03-12", "2013-03-13", "2013-03-14",
"2013-03-15", "2013-03-18", "2013-03-19", "2013-03-20", "2013-03-21",
"2013-03-22", "2013-03-25", "2013-03-26", "2013-03-27", "2013-03-28",
"2013-03-29", "2013-04-01", "2013-04-02", "2013-04-03", "2013-04-04",
"2013-04-05", "2013-04-08", "2013-04-09", "2013-04-10", "2013-04-11",
"2013-04-12", "2013-04-15", "2013-04-16", "2013-04-17", "2013-04-18",
"2013-04-19", "2013-04-22", "2013-04-23", "2013-04-24", "2013-04-25",
"2013-04-26", "2013-04-29", "2013-04-30", "2013-05-01", "2013-05-02",
"2013-05-03", "2013-05-06", "2013-05-07", "2013-05-08", "2013-05-09",
"2013-05-10", "2013-05-13", "2013-05-14", "2013-05-15", "2013-05-16",
"2013-05-17", "2013-05-20", "2013-05-21", "2013-05-22", "2013-05-23",
"2013-05-24", "2013-05-27", "2013-05-28", "2013-05-29", "2013-05-30",
"2013-05-31", "2013-06-03", "2013-06-04", "2013-06-05", "2013-06-06",
"2013-06-07", "2013-06-10", "2013-06-11", "2013-06-12", "2013-06-13",
"2013-06-14", "2013-06-17", "2013-06-18", "2013-06-19", "2013-06-20",
"2013-06-21", "2013-06-24", "2013-06-25", "2013-06-26", "2013-06-27",
"2013-06-28", "2013-07-01", "2013-07-02", "2013-07-03", "2013-07-04",
"2013-07-05", "2013-07-08", "2013-07-09", "2013-07-10", "2013-07-11",
"2013-07-12", "2013-07-15", "2013-07-16", "2013-07-17", "2013-07-18",
"2013-07-19", "2013-07-22", "2013-07-23", "2013-07-24", "2013-07-25",
"2013-07-26", "2013-07-29", "2013-07-30", "2013-07-31", "2013-08-01",
"2013-08-02", "2013-08-05", "2013-08-06", "2013-08-07", "2013-08-08",
"2013-08-09", "2013-08-12", "2013-08-13", "2013-08-14", "2013-08-15",
"2013-08-16", "2013-08-19", "2013-08-20", "2013-08-21", "2013-08-22",
"2013-08-23", "2013-08-26", "2013-08-27", "2013-08-28", "2013-08-29",
"2013-08-30", "2013-09-02", "2013-09-03", "2013-09-04", "2013-09-05",
"2013-09-06", "2013-09-09", "2013-09-10", "2013-09-11", "2013-09-12",
"2013-09-13", "2013-09-16", "2013-09-17", "2013-09-18", "2013-09-19",
"2013-09-20", "2013-09-23", "2013-09-24", "2013-09-25", "2013-09-26",
"2013-09-27", "2013-09-30", "2013-10-01", "2013-10-02", "2013-10-03",
"2013-10-04", "2013-10-07", "2013-10-08", "2013-10-09", "2013-10-10",
"2013-10-11", "2013-10-14", "2013-10-15", "2013-10-16", "2013-10-17",
"2013-10-18", "2013-10-21", "2013-10-22", "2013-10-23", "2013-10-24",
"2013-10-25", "2013-10-28", "2013-10-29", "2013-10-30", "2013-10-31",
"2013-11-01", "2013-11-04", "2013-11-05", "2013-11-06", "2013-11-07",
"2013-11-08", "2013-11-11", "2013-11-12", "2013-11-13", "2013-11-14",
"2013-11-15", "2013-11-18", "2013-11-19", "2013-11-20", "2013-11-21",
"2013-11-22", "2013-11-25", "2013-11-26", "2013-11-27", "2013-11-28",
"2013-11-29", "2013-12-02", "2013-12-03", "2013-12-04", "2013-12-05",
"2013-12-06", "2013-12-09", "2013-12-10", "2013-12-11", "2013-12-12",
"2013-12-13", "2013-12-16", "2013-12-17", "2013-12-18", "2013-12-19",
"2013-12-20", "2013-12-23", "2013-12-24", "2013-12-25", "2013-12-26",
"2013-12-27", "2013-12-30", "2013-12-31", "2014-01-01", "2014-01-02",
"2014-01-03", "2014-01-06", "2014-01-07", "2014-01-08", "2014-01-09",
"2014-01-10", "2014-01-13", "2014-01-14", "2014-01-15", "2014-01-16",
"2014-01-17", "2014-01-20", "2014-01-21", "2014-01-22", "2014-01-23",
"2014-01-24", "2014-01-27", "2014-01-28", "2014-01-29", "2014-01-30",
"2014-01-31", "2014-02-03", "2014-02-04", "2014-02-05", "2014-02-06",
"2014-02-07", "2014-02-10", "2014-02-11", "2014-02-12", "2014-02-13",
"2014-02-14", "2014-02-17", "2014-02-18", "2014-02-19", "2014-02-20",
"2014-02-21", "2014-02-24", "2014-02-25", "2014-02-26", "2014-02-27",
"2014-02-28", "2014-03-03", "2014-03-04", "2014-03-05", "2014-03-06",
"2014-03-07", "2014-03-10", "2014-03-11", "2014-03-12", "2014-03-13",
"2014-03-14", "2014-03-17", "2014-03-18", "2014-03-19", "2014-03-20",
"2014-03-21", "2014-03-24", "2014-03-25", "2014-03-26", "2014-03-27",
"2014-03-28", "2014-03-31", "2014-04-01", "2014-04-02", "2014-04-03",
"2014-04-04", "2014-04-07", "2014-04-08", "2014-04-09", "2014-04-10",
"2014-04-11", "2014-04-14", "2014-04-15", "2014-04-16", "2014-04-17",
"2014-04-18", "2014-04-21", "2014-04-22", "2014-04-23", "2014-04-24",
"2014-04-25", "2014-04-28", "2014-04-29", "2014-04-30", "2014-05-01",
"2014-05-02", "2014-05-05", "2014-05-06", "2014-05-07", "2014-05-08",
"2014-05-09", "2014-05-12", "2014-05-13", "2014-05-14", "2014-05-15",
"2014-05-16", "2014-05-19", "2014-05-20", "2014-05-21", "2014-05-22",
"2014-05-23", "2014-05-26", "2014-05-27", "2014-05-28", "2014-05-29",
"2014-05-30", "2014-06-02", "2014-06-03", "2014-06-04", "2014-06-05",
"2014-06-06", "2014-06-09", "2014-06-10", "2014-06-11", "2014-06-12",
"2014-06-13", "2014-06-16", "2014-06-17", "2014-06-18", "2014-06-19",
"2014-06-20", "2014-06-23", "2014-06-24", "2014-06-25", "2014-06-26",
"2014-06-27", "2014-06-30", "2014-07-01", "2014-07-02", "2014-07-03",
"2014-07-04", "2014-07-07", "2014-07-08", "2014-07-09", "2014-07-10",
"2014-07-11", "2014-07-14", "2014-07-15", "2014-07-16", "2014-07-17",
"2014-07-18", "2014-07-21", "2014-07-22", "2014-07-23", "2014-07-24",
"2014-07-25", "2014-07-28", "2014-07-29", "2014-07-30", "2014-07-31",
"2014-08-01", "2014-08-04", "2014-08-05", "2014-08-06", "2014-08-07",
"2014-08-08", "2014-08-11", "2014-08-12", "2014-08-13", "2014-08-14",
"2014-08-15", "2014-08-18", "2014-08-19", "2014-08-20", "2014-08-21",
"2014-08-22", "2014-08-25", "2014-08-26", "2014-08-27", "2014-08-28",
"2014-08-29", "2014-09-01", "2014-09-02", "2014-09-03", "2014-09-04",
"2014-09-05", "2014-09-08", "2014-09-09", "2014-09-10", "2014-09-11",
"2014-09-12", "2014-09-15", "2014-09-16", "2014-09-17", "2014-09-18",
"2014-09-19", "2014-09-22", "2014-09-23", "2014-09-24", "2014-09-25",
"2014-09-26", "2014-09-29", "2014-09-30", "2014-10-01", "2014-10-02",
"2014-10-03", "2014-10-06", "2014-10-07", "2014-10-08", "2014-10-09",
"2014-10-10", "2014-10-13", "2014-10-14", "2014-10-15", "2014-10-16",
"2014-10-17", "2014-10-20", "2014-10-21", "2014-10-22", "2014-10-23",
"2014-10-24", "2014-10-27", "2014-10-28", "2014-10-29", "2014-10-30",
"2014-10-31", "2014-11-03", "2014-11-04", "2014-11-05", "2014-11-06",
"2014-11-07", "2014-11-10", "2014-11-11", "2014-11-12", "2014-11-13",
"2014-11-14", "2014-11-17", "2014-11-18", "2014-11-19", "2014-11-20",
"2014-11-21", "2014-11-24", "2014-11-25", "2014-11-26", "2014-11-27",
"2014-11-28", "2014-12-01", "2014-12-02", "2014-12-03", "2014-12-04",
"2014-12-05", "2014-12-08", "2014-12-09", "2014-12-10", "2014-12-11",
"2014-12-12", "2014-12-15", "2014-12-16", "2014-12-17", "2014-12-18",
"2014-12-19", "2014-12-22", "2014-12-23", "2014-12-24", "2014-12-25",
"2014-12-26", "2014-12-29", "2014-12-30", "2014-12-31", "2015-01-01",
"2015-01-02", "2015-01-05", "2015-01-06", "2015-01-07", "2015-01-08",
"2015-01-09", "2015-01-12", "2015-01-13", "2015-01-14", "2015-01-15",
"2015-01-16", "2015-01-19", "2015-01-20", "2015-01-21", "2015-01-22",
"2015-01-23", "2015-01-26", "2015-01-27", "2015-01-28", "2015-01-29",
"2015-01-30", "2015-02-02", "2015-02-03", "2015-02-04", "2015-02-05",
"2015-02-06", "2015-02-09", "2015-02-10", "2015-02-11", "2015-02-12",
"2015-02-13", "2015-02-16", "2015-02-17", "2015-02-18", "2015-02-19",
"2015-02-20", "2015-02-23", "2015-02-24", "2015-02-25", "2015-02-26",
"2015-02-27", "2015-03-02", "2015-03-03", "2015-03-04", "2015-03-05",
"2015-03-06", "2015-03-09", "2015-03-10", "2015-03-11", "2015-03-12",
"2015-03-13", "2015-03-16", "2015-03-17", "2015-03-18", "2015-03-19",
"2015-03-20", "2015-03-23", "2015-03-24", "2015-03-25", "2015-03-26",
"2015-03-27", "2015-03-30", "2015-03-31", "2015-04-01", "2015-04-02",
"2015-04-03", "2015-04-06", "2015-04-07", "2015-04-08", "2015-04-09",
"2015-04-10", "2015-04-13", "2015-04-14", "2015-04-15", "2015-04-16",
"2015-04-17", "2015-04-20", "2015-04-21", "2015-04-22", "2015-04-23",
"2015-04-24", "2015-04-27", "2015-04-28", "2015-04-29", "2015-04-30",
"2015-05-01", "2015-05-04", "2015-05-05", "2015-05-06", "2015-05-07",
"2015-05-08", "2015-05-11", "2015-05-12", "2015-05-13", "2015-05-14",
"2015-05-15", "2015-05-18", "2015-05-19", "2015-05-20", "2015-05-21",
"2015-05-22", "2015-05-25", "2015-05-26", "2015-05-27", "2015-05-28",
"2015-05-29", "2015-06-01", "2015-06-02", "2015-06-03", "2015-06-04",
"2015-06-05", "2015-06-08", "2015-06-09", "2015-06-10", "2015-06-11",
"2015-06-12", "2015-06-15", "2015-06-16", "2015-06-17", "2015-06-18",
"2015-06-19", "2015-06-22", "2015-06-23", "2015-06-24", "2015-06-25",
"2015-06-26", "2015-06-29", "2015-06-30", "2015-07-01", "2015-07-02",
"2015-07-03", "2015-07-06", "2015-07-07", "2015-07-08", "2015-07-09",
"2015-07-10", "2015-07-13", "2015-07-14", "2015-07-15", "2015-07-16",
"2015-07-17", "2015-07-20", "2015-07-21", "2015-07-22", "2015-07-23",
"2015-07-24", "2015-07-27", "2015-07-28", "2015-07-29", "2015-07-30",
"2015-07-31", "2015-08-03", "2015-08-04", "2015-08-05", "2015-08-06",
"2015-08-07", "2015-08-10", "2015-08-11", "2015-08-12", "2015-08-13",
"2015-08-14", "2015-08-17", "2015-08-18", "2015-08-19", "2015-08-20",
"2015-08-21", "2015-08-24", "2015-08-25", "2015-08-26", "2015-08-27",
"2015-08-28", "2015-08-31", "2015-09-01", "2015-09-02", "2015-09-03",
"2015-09-04", "2015-09-07", "2015-09-08", "2015-09-09", "2015-09-10",
"2015-09-11", "2015-09-14", "2015-09-15", "2015-09-16", "2015-09-17",
"2015-09-18", "2015-09-21", "2015-09-22", "2015-09-23", "2015-09-24",
"2015-09-25", "2015-09-28", "2015-09-29", "2015-09-30", "2015-10-01",
"2015-10-02", "2015-10-05", "2015-10-06", "2015-10-07", "2015-10-08",
"2015-10-09", "2015-10-12", "2015-10-13", "2015-10-14", "2015-10-15",
"2015-10-16", "2015-10-19", "2015-10-20", "2015-10-21", "2015-10-22",
"2015-10-23", "2015-10-26", "2015-10-27", "2015-10-28", "2015-10-29",
"2015-10-30", "2015-11-02", "2015-11-03", "2015-11-04", "2015-11-05",
"2015-11-06", "2015-11-09", "2015-11-10", "2015-11-11", "2015-11-12",
"2015-11-13", "2015-11-16", "2015-11-17", "2015-11-18", "2015-11-19",
"2015-11-20", "2015-11-23", "2015-11-24", "2015-11-25", "2015-11-26",
"2015-11-27", "2015-11-30", "2015-12-01", "2015-12-02", "2015-12-03",
"2015-12-04", "2015-12-07", "2015-12-08", "2015-12-09", "2015-12-10",
"2015-12-11", "2015-12-14", "2015-12-15", "2015-12-16", "2015-12-17",
"2015-12-18", "2015-12-21", "2015-12-22", "2015-12-23", "2015-12-24",
"2015-12-25", "2015-12-28", "2015-12-29", "2015-12-30", "2015-12-31",
"2016-01-01", "2016-01-04", "2016-01-05", "2016-01-06", "2016-01-07",
"2016-01-08", "2016-01-11", "2016-01-12", "2016-01-13", "2016-01-14",
"2016-01-15", "2016-01-18", "2016-01-19", "2016-01-20", "2016-01-21",
"2016-01-22", "2016-01-25", "2016-01-26", "2016-01-27", "2016-01-28",
"2016-01-29", "2016-02-01", "2016-02-02", "2016-02-03", "2016-02-04",
"2016-02-05", "2016-02-08", "2016-02-09", "2016-02-10", "2016-02-11",
"2016-02-12", "2016-02-15", "2016-02-16", "2016-02-17", "2016-02-18",
"2016-02-19", "2016-02-22", "2016-02-23", "2016-02-24", "2016-02-25",
"2016-02-26", "2016-02-29", "2016-03-01", "2016-03-02", "2016-03-03",
"2016-03-04", "2016-03-07", "2016-03-08", "2016-03-09", "2016-03-10",
"2016-03-11", "2016-03-14", "2016-03-15", "2016-03-16", "2016-03-17",
"2016-03-18", "2016-03-21", "2016-03-22", "2016-03-23", "2016-03-24",
"2016-03-25", "2016-03-28", "2016-03-29", "2016-03-30", "2016-03-31",
"2016-04-01", "2016-04-04", "2016-04-05", "2016-04-06", "2016-04-07",
"2016-04-08", "2016-04-11", "2016-04-12", "2016-04-13", "2016-04-14",
"2016-04-15", "2016-04-18", "2016-04-19", "2016-04-20", "2016-04-21",
"2016-04-22", "2016-04-25", "2016-04-26", "2016-04-27", "2016-04-28",
"2016-04-29", "2016-05-02", "2016-05-03", "2016-05-04", "2016-05-05",
"2016-05-06", "2016-05-09", "2016-05-10", "2016-05-11", "2016-05-12",
"2016-05-13", "2016-05-16", "2016-05-17", "2016-05-18", "2016-05-19",
"2016-05-20", "2016-05-23", "2016-05-24", "2016-05-25", "2016-05-26",
"2016-05-27", "2016-05-30", "2016-05-31", "2016-06-01", "2016-06-02",
"2016-06-03", "2016-06-06", "2016-06-07", "2016-06-08", "2016-06-09",
"2016-06-10", "2016-06-13", "2016-06-14", "2016-06-15", "2016-06-16",
"2016-06-17", "2016-06-20", "2016-06-21", "2016-06-22", "2016-06-23",
"2016-06-24", "2016-06-27", "2016-06-28", "2016-06-29", "2016-06-30",
"2016-07-01", "2016-07-04", "2016-07-05", "2016-07-06", "2016-07-07",
"2016-07-08", "2016-07-11", "2016-07-12", "2016-07-13", "2016-07-14",
"2016-07-15", "2016-07-18", "2016-07-19", "2016-07-20", "2016-07-21",
"2016-07-22", "2016-07-25", "2016-07-26", "2016-07-27", "2016-07-28",
"2016-07-29", "2016-08-01", "2016-08-02", "2016-08-03", "2016-08-04",
"2016-08-05", "2016-08-08", "2016-08-09", "2016-08-10", "2016-08-11",
"2016-08-12", "2016-08-15", "2016-08-16", "2016-08-17", "2016-08-18",
"2016-08-19", "2016-08-22", "2016-08-23", "2016-08-24", "2016-08-25",
"2016-08-26", "2016-08-29", "2016-08-30", "2016-08-31", "2016-09-01",
"2016-09-02", "2016-09-05", "2016-09-06", "2016-09-07", "2016-09-08",
"2016-09-09", "2016-09-12", "2016-09-13", "2016-09-14", "2016-09-15",
"2016-09-16", "2016-09-19", "2016-09-20", "2016-09-21", "2016-09-22",
"2016-09-23", "2016-09-26", "2016-09-27", "2016-09-28", "2016-09-29",
"2016-09-30", "2016-10-03", "2016-10-04", "2016-10-05", "2016-10-06",
"2016-10-07", "2016-10-10", "2016-10-11", "2016-10-12", "2016-10-13",
"2016-10-14", "2016-10-17", "2016-10-18", "2016-10-19", "2016-10-20",
"2016-10-21", "2016-10-24", "2016-10-25", "2016-10-26", "2016-10-27",
"2016-10-28", "2016-10-31", "2016-11-01", "2016-11-02", "2016-11-03",
"2016-11-04", "2016-11-07", "2016-11-08", "2016-11-09", "2016-11-10",
"2016-11-11", "2016-11-14", "2016-11-15", "2016-11-16", "2016-11-17",
"2016-11-18", "2016-11-21", "2016-11-22", "2016-11-23", "2016-11-24",
"2016-11-25", "2016-11-28", "2016-11-29", "2016-11-30", "2016-12-01",
"2016-12-02", "2016-12-05", "2016-12-06", "2016-12-07", "2016-12-08",
"2016-12-09", "2016-12-12", "2016-12-13", "2016-12-14", "2016-12-15",
"2016-12-16", "2016-12-19", "2016-12-20", "2016-12-21", "2016-12-22",
"2016-12-23", "2016-12-26", "2016-12-27", "2016-12-28", "2016-12-29",
"2016-12-30", "2017-01-02", "2017-01-03", "2017-01-04", "2017-01-05",
"2017-01-06", "2017-01-09", "2017-01-10", "2017-01-11", "2017-01-12",
"2017-01-13", "2017-01-16", "2017-01-17", "2017-01-18", "2017-01-19",
"2017-01-20", "2017-01-23", "2017-01-24", "2017-01-25", "2017-01-26",
"2017-01-27", "2017-01-30", "2017-01-31", "2017-02-01", "2017-02-02",
"2017-02-03", "2017-02-06", "2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23",
"2017-02-24", "2017-02-27", "2017-02-28", "2017-03-01", "2017-03-02",
"2017-03-03", "2017-03-06", "2017-03-07", "2017-03-08", "2017-03-09",
"2017-03-10", "2017-03-13", "2017-03-14", "2017-03-15", "2017-03-16",
"2017-03-17", "2017-03-20", "2017-03-21", "2017-03-22", "2017-03-23",
"2017-03-24", "2017-03-27", "2017-03-28", "2017-03-29", "2017-03-30",
"2017-03-31", "2017-04-03", "2017-04-04", "2017-04-05", "2017-04-06",
"2017-04-07", "2017-04-10", "2017-04-11", "2017-04-12", "2017-04-13",
"2017-04-14", "2017-04-17", "2017-04-18", "2017-04-19", "2017-04-20",
"2017-04-21", "2017-04-24", "2017-04-25", "2017-04-26", "2017-04-27",
"2017-04-28", "2017-05-01", "2017-05-02", "2017-05-03", "2017-05-04",
"2017-05-05", "2017-05-08", "2017-05-09", "2017-05-10", "2017-05-11",
"2017-05-12", "2017-05-15", "2017-05-16", "2017-05-17", "2017-05-18",
"2017-05-19", "2017-05-22", "2017-05-23", "2017-05-24", "2017-05-25",
"2017-05-26", "2017-05-29", "2017-05-30", "2017-05-31", "2017-06-01",
"2017-06-02", "2017-06-05", "2017-06-06", "2017-06-07", "2017-06-08",
"2017-06-09", "2017-06-12", "2017-06-13", "2017-06-14", "2017-06-15",
"2017-06-16", "2017-06-19", "2017-06-20", "2017-06-21", "2017-06-22",
"2017-06-23", "2017-06-26", "2017-06-27", "2017-06-28", "2017-06-29",
"2017-06-30", "2017-07-03", "2017-07-04", "2017-07-05", "2017-07-06",
"2017-07-07", "2017-07-10", "2017-07-11", "2017-07-12", "2017-07-13",
"2017-07-14", "2017-07-17", "2017-07-18", "2017-07-19", "2017-07-20",
"2017-07-21", "2017-07-24", "2017-07-25", "2017-07-26", "2017-07-27",
"2017-07-28", "2017-07-31", "2017-08-01", "2017-08-02", "2017-08-03",
"2017-08-04", "2017-08-07", "2017-08-08", "2017-08-09", "2017-08-10",
"2017-08-11", "2017-08-14", "2017-08-15", "2017-08-16", "2017-08-17",
"2017-08-18", "2017-08-21", "2017-08-22", "2017-08-23", "2017-08-24",
"2017-08-25", "2017-08-28", "2017-08-29", "2017-08-30", "2017-08-31"
), dcoilwtico = c(NA, 93.14, 92.97, 93.12, 93.2, 93.21, 93.08,
93.81, 93.6, 94.27, 93.26, 94.28, 95.49, 95.61, NA, 96.09, 95.06,
95.35, 95.15, 95.95, 97.62, 97.98, 97.65, 97.46, 96.21, 96.68,
96.44, 95.84, 95.71, 97.01, 97.48, 97.03, 97.3, 95.95, NA, 96.69,
94.92, 92.79, 93.12, 92.74, 92.63, 92.84, 92.03, 90.71, 90.13,
90.88, 90.47, 91.53, 92.01, 92.07, 92.44, 92.47, 93.03, 93.49,
93.71, 92.44, 93.21, 92.46, 93.41, 94.55, 95.99, 96.53, 97.24,
NA, 97.1, 97.23, 95.02, 93.26, 92.76, 93.36, 94.18, 94.59, 93.44,
91.23, 88.75, 88.73, 86.65, 87.83, 88.04, 88.81, 89.21, 91.07,
93.27, 92.63, 94.09, 93.22, 90.74, 93.7, 95.25, 95.8, 95.28,
96.24, 96.09, 95.81, 94.76, 93.96, 93.95, 94.85, 95.72, 96.29,
95.55, 93.98, 94.12, 93.84, NA, 94.65, 93.13, 93.57, 91.93, 93.41,
93.36, 93.66, 94.71, 96.11, 95.82, 95.5, 95.98, 96.66, 97.83,
97.86, 98.46, 98.24, 94.89, 93.81, 95.07, 95.25, 95.47, 97, 96.36,
97.94, 99.65, 101.92, NA, 103.09, 103.03, 103.46, 106.41, 104.77,
105.85, 106.2, 105.88, 106.39, 107.94, 108, 106.61, 107.13, 105.41,
105.47, 104.76, 104.61, 103.14, 105.1, 107.93, 106.94, 106.61,
105.32, 104.41, 103.45, 106.04, 106.19, 106.78, 106.89, 107.43,
107.58, 107.14, 104.9, 103.93, 104.93, 106.48, 105.88, 109.11,
110.17, 108.51, 107.98, NA, 108.67, 107.29, 108.5, 110.62, 109.62,
107.48, 107.65, 108.72, 108.31, 106.54, 105.36, 108.23, 106.26,
104.7, 103.62, 103.22, 102.68, 103.1, 102.86, 102.36, 102.09,
104.15, 103.29, 103.83, 103.07, 103.54, 101.63, 103.08, 102.17,
102.46, 101.15, 102.34, 100.72, 100.87, 99.28, 97.63, 96.9, 96.65,
97.4, 98.74, 98.29, 96.81, 96.29, 94.56, 94.58, 93.4, 94.74,
94.25, 94.56, 95.13, 93.12, 93.91, 93.76, 93.8, 93.03, 93.35,
93.34, 95.35, 94.53, 93.86, 93.41, 92.05, NA, 92.55, 93.61, 95.83,
96.97, 97.14, 97.48, 97.1, 98.32, 97.25, 97.21, 96.27, 97.18,
96.99, 97.59, 98.4, 99.11, 98.62, 98.87, NA, 99.18, 99.94, 98.9,
98.17, NA, 95.14, 93.66, 93.12, 93.31, 91.9, 91.36, 92.39, 91.45,
92.15, 93.78, 93.54, 93.96, NA, 94.51, 96.35, 97.23, 96.66, 95.82,
97.49, 97.34, 98.25, 97.55, 96.44, 97.24, 97.4, 97.84, 99.98,
100.12, 99.96, 100.38, 100.27, 100.31, NA, 102.54, 103.46, 103.2,
102.53, 103.17, 102.2, 102.93, 102.68, 102.88, 105.34, 103.64,
101.75, 101.82, 102.82, 101.39, 100.29, 98.29, 98.57, 99.23,
98.43, 100.08, 100.71, 99.68, 99.97, 100.05, 99.66, 100.61, 101.25,
101.73, 101.57, 99.69, 99.6, 100.29, 101.16, 100.43, 102.57,
103.55, 103.37, 103.68, 104.05, 103.7, 103.71, 104.33, NA, 104.35,
101.69, 101.47, 102.2, 100.85, 101.13, 101.56, 100.07, 99.69,
100.09, 99.74, 99.81, 101.06, 100.52, 100.32, 100.89, 102.01,
102.63, 101.74, 102.31, 102.95, 102.8, 104.31, 104.03, 105.01,
NA, 104.78, 103.37, 104.26, 103.4, 103.07, 103.34, 103.27, 103.17,
103.32, 105.09, 105.02, 105.04, 107.2, 107.49, 107.52, 106.95,
106.64, 107.08, 107.95, 106.83, 106.64, 107.04, 106.49, 106.46,
106.07, 106.06, 105.18, 104.76, NA, 104.19, 104.06, 102.93, 103.61,
101.48, 101.73, 100.56, 101.88, 103.84, 103.83, 105.34, 104.59,
103.81, 102.76, 105.23, 105.68, 104.91, 104.29, 98.23, 97.86,
98.26, 97.34, 96.93, 97.34, 97.61, 98.09, 97.36, 97.57, 95.54,
97.3, 96.44, 94.35, 96.4, 93.97, 93.61, 95.39, 95.78, 95.82,
96.44, 97.86, NA, 92.92, 95.5, 94.51, 93.32, 92.64, 92.73, 91.71,
92.89, 92.18, 92.86, 94.91, 94.33, 93.07, 92.43, 91.46, 91.55,
93.6, 93.59, 95.55, 94.53, 91.17, 90.74, 91.02, 89.76, 90.33,
88.89, 87.29, 85.76, 85.87, 85.73, 81.72, 81.82, 82.33, 82.8,
82.76, 83.25, 80.52, 82.81, 81.27, 81.26, 81.36, 82.25, 81.06,
80.53, 78.77, 77.15, 78.71, 77.87, 78.71, 77.43, 77.85, 77.16,
74.13, 75.91, 75.64, 74.55, 74.55, 75.63, 76.52, 75.74, 74.04,
73.7, NA, 65.94, 68.98, 66.99, 67.3, 66.73, 65.89, 63.13, 63.74,
60.99, 60.01, 57.81, 55.96, 55.97, 56.43, 54.18, 56.91, 55.25,
56.78, 55.7, NA, 54.59, 53.46, 54.14, 53.45, NA, 52.72, 50.05,
47.98, 48.69, 48.8, 48.35, 46.06, 45.92, 48.49, 46.37, 48.49,
NA, 46.79, 47.85, 45.93, 45.26, 44.8, 45.84, 44.08, 44.12, 47.79,
49.25, 53.04, 48.45, 50.48, 51.66, 52.99, 50.06, 48.8, 51.17,
52.66, NA, 53.56, 52.13, 51.12, 49.95, 49.56, 48.48, 50.25, 47.65,
49.84, 49.59, 50.43, 51.53, 50.76, 49.61, 49.95, 48.42, 48.06,
47.12, 44.88, 43.93, 43.39, 44.63, 44.02, 46, 47.4, 47.03, 48.75,
51.41, 48.83, 48.66, 47.72, 50.12, 49.13, NA, 52.08, 53.95, 50.44,
50.79, 51.63, 51.95, 53.3, 56.25, 56.69, 55.71, 56.37, 55.58,
56.17, 56.59, 55.98, 55.56, 57.05, 58.55, 59.62, 59.1, 58.92,
60.38, 60.93, 58.99, 59.41, 59.23, 60.72, 60.5, 59.89, 59.73,
59.44, 57.3, 58.96, 60.18, 58.88, NA, 57.29, 57.51, 57.69, 60.25,
60.24, 61.3, 59.67, 58, 59.11, 58.15, 60.15, 61.36, 60.74, 59.96,
59.53, 60.01, 59.89, 60.41, 59.62, 60.01, 61.05, 60.01, 59.59,
59.41, 58.34, 59.48, 56.94, 56.93, NA, 52.48, 52.33, 51.61, 52.76,
52.74, 52.19, 53.05, 51.4, 50.9, 50.88, 50.11, 50.59, 49.27,
48.11, 47.98, 47.17, 47.97, 48.77, 48.53, 47.11, 45.25, 45.75,
45.13, 44.69, 43.87, 44.94, 43.11, 43.22, 42.27, 42.45, 41.93,
42.58, 40.75, 41, 40.45, 38.22, 39.15, 38.5, 42.47, 45.29, 49.2,
45.38, 46.3, 46.75, 46.02, NA, 45.92, 44.13, 45.85, 44.75, 44.07,
44.58, 47.12, 46.93, 44.71, 46.67, 46.17, 44.53, 44.94, 45.55,
44.4, 45.24, 45.06, 44.75, 45.54, 46.28, 48.53, 47.86, 49.46,
49.67, 47.09, 46.7, 46.63, 46.38, 47.3, 45.91, 45.84, 45.22,
44.9, 43.91, 43.19, 43.21, 45.93, 46.02, 46.6, 46.12, 47.88,
46.32, 45.27, 44.32, 43.87, 44.23, 42.95, 41.74, 40.69, 41.68,
40.73, 40.75, 40.55, 39.39, 39.27, 40.89, 41.22, NA, 40.57, 40.43,
40.58, 39.93, 41.08, 40, 37.64, 37.46, 37.16, 36.76, 35.65, 36.31,
37.32, 35.55, 34.98, 34.72, 34.55, 36.12, 36.76, 37.62, NA, 36.36,
37.88, 36.59, 37.13, NA, 36.81, 35.97, 33.97, 33.29, 33.2, 31.42,
30.42, 30.42, 31.22, 29.45, NA, 28.47, 26.68, 29.55, 32.07, 30.31,
29.54, 32.32, 33.21, 33.66, 31.62, 29.9, 32.29, 31.63, 30.86,
29.71, 27.96, 27.54, 26.19, 29.32, NA, 29.05, 30.68, 30.77, 29.59,
31.37, 31.84, 30.35, 31.4, 31.65, 32.74, 34.39, 34.57, 34.56,
35.91, 37.9, 36.67, 37.62, 37.77, 38.51, 37.2, 36.32, 38.43,
40.17, 39.47, 39.91, 41.45, 38.28, 38.14, NA, 37.99, 36.91, 36.91,
36.94, 35.36, 34.3, 34.52, 37.74, 37.3, 39.74, 40.46, 42.12,
41.7, 41.45, 40.4, 39.74, 40.88, 42.72, 43.18, 42.76, 41.67,
42.52, 45.29, 46.03, 45.98, 44.75, 43.65, 43.77, 44.33, 44.58,
43.45, 44.68, 46.21, 46.64, 46.22, 47.72, 48.29, 48.12, 48.16,
47.67, 48.12, 48.04, 49.1, 49, 49.36, NA, 49.1, 49.07, 49.14,
48.69, 49.71, 50.37, 51.23, 50.52, 49.09, 48.89, 48.49, 47.92,
46.14, 48, 49.4, 48.95, 49.16, 49.34, 46.7, 45.8, 47.93, 49.85,
48.27, 49.02, NA, 46.73, 47.37, 45.22, 45.37, 44.73, 46.82, 44.87,
45.64, 45.93, 45.23, 44.64, 44.96, 43.96, 43.41, 42.4, 42.16,
41.9, 41.13, 41.54, 40.05, 39.5, 40.8, 41.92, 41.83, 43.06, 42.78,
41.75, 43.51, 44.47, 45.72, 46.57, 46.81, 48.2, 48.48, 46.8,
47.54, 46.29, 46.97, 47.64, 46.97, 46.32, 44.68, 43.17, 44.39,
NA, 44.85, 45.47, 47.63, 45.88, 46.28, 44.91, 43.62, 43.85, 43.04,
43.34, 43.85, 45.33, 46.1, 44.36, 45.6, 44.65, 47.07, 47.72,
47.72, 48.8, 48.67, 49.75, 50.44, 49.76, 49.76, 50.72, 50.14,
50.47, 50.35, 49.97, 50.3, 51.59, 50.31, 50.61, 50.18, 49.45,
48.75, 49.71, 48.72, 46.83, 46.66, 45.32, 44.66, 44.07, 44.88,
44.96, 45.2, 44.62, 43.39, 43.29, 45.86, 45.56, 45.37, 45.69,
47.48, 48.07, 46.72, NA, 46.72, 45.66, 45.29, 49.41, 51.08, 51.7,
51.72, 50.95, 49.85, 50.84, 51.51, 52.74, 52.99, 51.01, 50.9,
51.93, 52.13, 52.22, 51.44, 51.98, 52.01, NA, 52.82, 54.01, 53.8,
53.75, NA, 52.36, 53.26, 53.77, 53.98, 51.95, 50.82, 52.19, 53.01,
52.36, NA, 52.45, 51.12, 51.39, 52.33, 52.77, 52.38, 52.14, 53.24,
53.18, 52.63, 52.75, 53.9, 53.55, 53.81, 53.01, 52.19, 52.37,
52.99, 53.84, 52.96, 53.21, 53.11, 53.41, 53.41, NA, 54.02, 53.61,
54.48, 53.99, 54.04, 54, 53.82, 52.63, 53.33, 53.19, 52.68, 49.83,
48.75, 48.05, 47.95, 47.24, 48.34, 48.3, 48.34, 47.79, 47.02,
47.29, 47, 47.3, 47.02, 48.36, 49.47, 50.3, 50.54, 50.25, 50.99,
51.14, 51.69, 52.25, 53.06, 53.38, 53.12, 53.19, NA, 52.62, 52.46,
50.49, 50.26, 49.64, 48.9, 49.22, 49.22, 48.96, 49.31, 48.83,
47.65, 47.79, 45.55, 46.23, 46.46, 45.84, 47.28, 47.81, 47.83,
48.86, 48.64, 49.04, 49.36, 50.32, 50.81, 51.12, 50.99, 48.57,
49.58, NA, 49.63, 48.29, 48.32, 47.68, 47.4, 48.13, 45.8, 45.68,
45.82, 46.1, 46.41, 44.79, 44.47, 44.73, 44.24, 43.34, 42.48,
42.53, 42.86, 43.24, 44.25, 44.74, 44.88, 46.02, NA, NA, 45.11,
45.52, 44.25, 44.4, 45.06, 45.48, 46.06, 46.53, 46.02, 46.4,
47.1, 46.73, 45.78, 46.21, 47.77, 48.58, 49.05, 49.72, 50.21,
49.19, 49.6, 49.03, 49.57, 49.37, 49.07, 49.59, 48.54, 48.81,
47.59, 47.57, 46.8, 47.07, 48.59, 47.39, 47.65, 48.45, 47.24,
47.65, 46.4, 46.46, 45.96, 47.26)), class = "data.frame", row.names = c(NA,
-1218L))
Code:
# Tried both of these options, and both don't work
oil_df %>% fill(everything()) %>% fill(everything(), .direction = 'up')
na.locf(na.locf(oil_df), fromLast = TRUE)
# Confirm oil_df has no more missing values
sum(is.na(oil_df))
Output: Excepted 0
> na.locf(na.locf(oil_df), fromLast = TRUE)
>
> # Confirm oil_df has no more missing values
> sum(is.na(oil_df))
[1] 43
The problem with the code in the question is that it is calculating a result and then throwing it away and instead of checking the result it is checking the original data frame. Assign the result of the na.locf to a variable and then check that object. Also note that since there are no NA's on the end we can just use one na.locf if it is ok that the NA's get filled in with the next non-NA.
library(zoo)
oil_df2 <- na.locf(oil_df, fromLast = TRUE)
sum(is.na(oil_df2))
## [1] 0
You can use the downup parameter of fill() in the tidyr package :
Direction in which to fill missing values. Currently either "down" (the default), "up", "downup" (i.e. first down and then up) or "updown" (first up and then down)
df <- data.frame(
id = 1:10,
value = c(NA,100,runif(4,100,200),NA,runif(3,100,200))
)
id value
1 1 NA
2 2 100.0000
3 3 129.3819
4 4 137.7009
5 5 187.6759
6 6 155.0566
7 7 NA
8 8 125.9839
9 9 159.3502
10 10 129.0750
df <- df %>%
fill(value, .direction = "downup")
df
id value
1 1 100.0000
2 2 100.0000
3 3 129.3819
4 4 137.7009
5 5 187.6759
6 6 155.0566
7 7 155.0566
8 8 125.9839
9 9 159.3502
10 10 129.0750
I have a large time series of actual and forecasted electricity generation values for each quarter of the hour, that looks like this:
df<-structure(list(DATETIME = structure(c(1604188800, 1604189700,
1604190600, 1604191500, 1604192400, 1604193300, 1604194200, 1604195100,
1604196000, 1604196900, 1604197800, 1604198700, 1604199600, 1604200500,
1604201400, 1604202300, 1604203200, 1604204100, 1604205000, 1604205900,
1604206800, 1604207700, 1604208600, 1604209500, 1604210400, 1604211300,
1604212200, 1604213100, 1604214000, 1604214900, 1604215800, 1604216700,
1604217600, 1604218500, 1604219400, 1604220300, 1604221200, 1604222100,
1604223000, 1604223900, 1604224800, 1604225700, 1604226600, 1604227500,
1604228400, 1604229300, 1604230200, 1604231100, 1604232000, 1604232900,
1604233800, 1604234700, 1604235600, 1604236500, 1604237400, 1604238300,
1604239200, 1604240100, 1604241000, 1604241900, 1604242800, 1604243700,
1604244600, 1604245500, 1604246400, 1604247300, 1604248200, 1604249100,
1604250000, 1604250900, 1604251800, 1604252700, 1604253600, 1604254500,
1604255400, 1604256300, 1604257200, 1604258100, 1604259000, 1604259900,
1604260800, 1604261700, 1604262600, 1604263500, 1604264400, 1604265300,
1604266200, 1604267100, 1604268000, 1604268900, 1604269800, 1604270700,
1604271600, 1604272500, 1604273400, 1604274300, 1604275200, 1604276100,
1604277000, 1604277900, 1604278800, 1604279700, 1604280600, 1604281500,
1604282400, 1604283300, 1604284200, 1604285100, 1604286000, 1604286900,
1604287800, 1604288700, 1604289600, 1604290500, 1604291400, 1604292300,
1604293200, 1604294100, 1604295000, 1604295900, 1604296800, 1604297700,
1604298600, 1604299500, 1604300400, 1604301300, 1604302200, 1604303100,
1604304000, 1604304900, 1604305800, 1604306700, 1604307600, 1604308500,
1604309400, 1604310300, 1604311200, 1604312100, 1604313000, 1604313900,
1604314800, 1604315700, 1604316600, 1604317500, 1604318400, 1604319300,
1604320200, 1604321100, 1604322000, 1604322900, 1604323800, 1604324700,
1604325600, 1604326500, 1604327400, 1604328300, 1604329200, 1604330100,
1604331000, 1604331900, 1604332800, 1604333700, 1604334600, 1604335500,
1604336400, 1604337300, 1604338200, 1604339100, 1604340000, 1604340900,
1604341800, 1604342700, 1604343600, 1604344500, 1604345400, 1604346300,
1604347200, 1604348100, 1604349000, 1604349900, 1604350800, 1604351700,
1604352600, 1604353500, 1604354400, 1604355300, 1604356200, 1604357100,
1604358000, 1604358900, 1604359800, 1604360700, 1604361600, 1604362500,
1604363400, 1604364300, 1604365200, 1604366100, 1604367000, 1604367900,
1604368800, 1604369700, 1604370600, 1604371500, 1604372400, 1604373300,
1604374200, 1604375100, 1604376000, 1604376900, 1604377800, 1604378700,
1604379600, 1604380500, 1604381400, 1604382300, 1604383200, 1604384100,
1604385000, 1604385900, 1604386800, 1604387700, 1604388600, 1604389500,
1604390400, 1604391300, 1604392200, 1604393100, 1604394000, 1604394900,
1604395800, 1604396700, 1604397600, 1604398500, 1604399400, 1604400300,
1604401200, 1604402100, 1604403000, 1604403900, 1604404800, 1604405700,
1604406600, 1604407500, 1604408400, 1604409300, 1604410200, 1604411100,
1604412000, 1604412900, 1604413800, 1604414700, 1604415600, 1604416500,
1604417400, 1604418300, 1604419200, 1604420100, 1604421000, 1604421900,
1604422800, 1604423700, 1604424600, 1604425500, 1604426400, 1604427300,
1604428200, 1604429100, 1604430000, 1604430900, 1604431800, 1604432700,
1604433600, 1604434500, 1604435400, 1604436300, 1604437200, 1604438100,
1604439000, 1604439900, 1604440800, 1604441700, 1604442600, 1604443500,
1604444400, 1604445300, 1604446200, 1604447100, 1604448000, 1604448900,
1604449800, 1604450700, 1604451600, 1604452500, 1604453400, 1604454300,
1604455200, 1604456100, 1604457000, 1604457900, 1604458800, 1604459700,
1604460600, 1604461500, 1604462400, 1604463300, 1604464200, 1604465100,
1604466000, 1604466900, 1604467800, 1604468700, 1604469600, 1604470500,
1604471400, 1604472300, 1604473200, 1604474100, 1604475000, 1604475900,
1604476800, 1604477700, 1604478600, 1604479500, 1604480400, 1604481300,
1604482200, 1604483100, 1604484000, 1604484900, 1604485800, 1604486700,
1604487600, 1604488500, 1604489400, 1604490300, 1604491200, 1604492100,
1604493000, 1604493900, 1604494800, 1604495700, 1604496600, 1604497500,
1604498400, 1604499300, 1604500200, 1604501100, 1604502000, 1604502900,
1604503800, 1604504700, 1604505600, 1604506500, 1604507400, 1604508300,
1604509200, 1604510100, 1604511000, 1604511900, 1604512800, 1604513700,
1604514600, 1604515500, 1604516400, 1604517300, 1604518200, 1604519100,
1604520000, 1604520900, 1604521800, 1604522700, 1604523600, 1604524500,
1604525400, 1604526300, 1604527200, 1604528100, 1604529000, 1604529900,
1604530800, 1604531700, 1604532600, 1604533500, 1604534400, 1604535300,
1604536200, 1604537100, 1604538000, 1604538900, 1604539800, 1604540700,
1604541600, 1604542500, 1604543400, 1604544300, 1604545200, 1604546100,
1604547000, 1604547900, 1604548800, 1604549700, 1604550600, 1604551500,
1604552400, 1604553300, 1604554200, 1604555100, 1604556000, 1604556900,
1604557800, 1604558700, 1604559600, 1604560500, 1604561400, 1604562300,
1604563200, 1604564100, 1604565000, 1604565900, 1604566800, 1604567700,
1604568600, 1604569500, 1604570400, 1604571300, 1604572200, 1604573100,
1604574000, 1604574900, 1604575800, 1604576700, 1604577600, 1604578500,
1604579400, 1604580300, 1604581200, 1604582100, 1604583000, 1604583900,
1604584800, 1604585700, 1604586600, 1604587500, 1604588400, 1604589300,
1604590200, 1604591100, 1604592000, 1604592900, 1604593800, 1604594700,
1604595600, 1604596500, 1604597400, 1604598300, 1604599200, 1604600100,
1604601000, 1604601900, 1604602800, 1604603700, 1604604600, 1604605500,
1604606400, 1604607300, 1604608200, 1604609100, 1604610000, 1604610900,
1604611800, 1604612700, 1604613600, 1604614500, 1604615400, 1604616300,
1604617200, 1604618100, 1604619000, 1604619900, 1604620800, 1604621700,
1604622600, 1604623500, 1604624400, 1604625300, 1604626200, 1604627100,
1604628000, 1604628900, 1604629800, 1604630700, 1604631600, 1604632500,
1604633400, 1604634300, 1604635200, 1604636100, 1604637000, 1604637900,
1604638800, 1604639700, 1604640600, 1604641500, 1604642400, 1604643300,
1604644200, 1604645100, 1604646000, 1604646900, 1604647800, 1604648700,
1604649600, 1604650500, 1604651400, 1604652300, 1604653200, 1604654100,
1604655000, 1604655900, 1604656800, 1604657700, 1604658600, 1604659500,
1604660400, 1604661300, 1604662200, 1604663100, 1604664000, 1604664900,
1604665800, 1604666700, 1604667600, 1604668500, 1604669400, 1604670300,
1604671200, 1604672100, 1604673000, 1604673900, 1604674800, 1604675700,
1604676600, 1604677500, 1604678400, 1604679300, 1604680200, 1604681100,
1604682000, 1604682900, 1604683800, 1604684700, 1604685600, 1604686500,
1604687400, 1604688300, 1604689200, 1604690100, 1604691000, 1604691900,
1604692800, 1604693700, 1604694600, 1604695500, 1604696400, 1604697300,
1604698200, 1604699100, 1604700000, 1604700900, 1604701800, 1604702700,
1604703600, 1604704500, 1604705400, 1604706300, 1604707200, 1604708100,
1604709000, 1604709900, 1604710800, 1604711700, 1604712600, 1604713500,
1604714400, 1604715300, 1604716200, 1604717100, 1604718000, 1604718900,
1604719800, 1604720700, 1604721600, 1604722500, 1604723400, 1604724300,
1604725200, 1604726100, 1604727000, 1604727900, 1604728800, 1604729700,
1604730600, 1604731500, 1604732400, 1604733300, 1604734200, 1604735100,
1604736000, 1604736900, 1604737800, 1604738700, 1604739600, 1604740500,
1604741400, 1604742300, 1604743200, 1604744100, 1604745000, 1604745900,
1604746800, 1604747700, 1604748600, 1604749500, 1604750400, 1604751300,
1604752200, 1604753100, 1604754000, 1604754900, 1604755800, 1604756700,
1604757600, 1604758500, 1604759400, 1604760300, 1604761200, 1604762100,
1604763000, 1604763900, 1604764800, 1604765700, 1604766600, 1604767500,
1604768400, 1604769300, 1604770200, 1604771100, 1604772000, 1604772900,
1604773800, 1604774700, 1604775600, 1604776500, 1604777400, 1604778300,
1604779200, 1604780100, 1604781000, 1604781900, 1604782800, 1604783700,
1604784600, 1604785500, 1604786400, 1604787300, 1604788200, 1604789100,
1604790000, 1604790900, 1604791800, 1604792700), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Actual = c(5.718, 5.844, 5.971, 5.925,
5.414, 5.432, 5.521, 5.513, 5.524, 6.182, 6.947, 7.133, 7.746,
7.058, 6.615, 9.665, 12.015, 12.029, 11.957, 12.981, 16.03, 16.821,
16.897, 15.383, 14.029, 14.642, 10.721, 9.187, 8.466, 6.89, 5.916,
7.977, 11.577, 12.432, 14.191, 14.655, 16.77, 17.376, 16.656,
16.659, 16.249, 15.771, 15.969, 15.623, 14.488, 14.506, 14.37,
13.399, 11.806, 10.78, 9.962, 10.093, 8.922, 10.099, 9.832, 8.406,
7.077, 6.514, 5.942, 6.502, 5.731, 5.276, 7.513, 7.141, 6.74,
6.36, 7.061, 8.619, 8.845, 9.808, 9.702, 10.079, 8.703, 7.287,
7.239, 7.768, 7.338, 7.11, 6.975, 7.35, 6.209, 6.441, 6.641,
5.892, 5.148, 4.533, 4.223, 3.89, 3.498, 2.941, 3.06, 3.244,
3.521, 3.703, 3.314, 3.507, 3.705, 3.073, 2.472, 2.344, 2.695,
2.729, 2.652, 2.582, 2.731, 2.759, 2.866, 3.283, 3.138, 3.009,
3.126, 3.389, 3.205, 3.39, 3.942, 4.029, 4.186, 4.282, 4.335,
4.026, 3.863, 3.772, 3.25, 3.282, 3.332, 3.18, 2.929, 3.054,
3.579, 3.886, 3.145, 2.984, 3.305, 3.535, 3.59, 4.079, 4.141,
3.957, 3.786, 3.505, 3.101, 2.892, 2.501, 2.243, 2.151, 1.96,
1.907, 1.911, 1.901, 1.96, 1.854, 1.88, 2.25, 2.252, 2.154, 2.11,
2.081, 2.27, 2.545, 2.864, 2.854, 3.257, 3.8, 4.158, 3.732, 3.771,
4.19, 4.964, 4.323, 5.286, 4.407, 4.795, 5.225, 5.726, 5.732,
5.836, 6.799, 6.322, 6.262, 5.851, 5.172, 5.007, 5.641, 5.812,
4.964, 4.531, 5.07, 5.403, 5.176, 5.122, 5.618, 6.368, 5.941,
6.191, 6.094, 6.703, 6.977, 6.391, 6.664, 6.671, 6.65, 6.687,
7.659, 8.364, 8.088, 7.329, 7.634, 7.981, 8.941, 8.922, 8.898,
8.921, 8.65, 9.39, 8.253, 7.656, 7.588, 6.226, 5.269, 4.915,
4.918, 4.594, 4.873, 4.414, 4.384, 3.967, 3.432, 3.287, 3.48,
3.374, 3.476, 4.54, 6.395, 6.67, 6.094, 5.212, 5.289, 4.99, 4.113,
3.32, 3.09, 2.25, 2.42, 2.812, 2.544, 2.466, 2.892, 3.04, 2.665,
2.303, 2.162, 1.896, 1.704, 1.683, 1.788, 1.841, 2.162, 2.288,
2.108, 1.943, 1.582, 1.347, 1.256, 1.301, 1.649, 1.615, 1.669,
1.855, 1.952, 2.354, 2.513, 2.314, 2.314, 2.623, 2.646, 2.655,
2.795, 2.829, 3.225, 3.759, 4.926, 6.119, 6.206, 6.608, 6.237,
5.74, 6.116, 8.257, 9.366, 9.073, 8.124, 6.595, 5, 4.428, 4.25,
4.662, 6.04, 7.415, 6.713, 6.646, 6.287, 6.502, 6.023, 5.789,
6.211, 7.477, 8.396, 9.687, 11.208, 9.911, 9.085, 8.758, 8.411,
8.321, 8.393, 8.796, 9.682, 9.908, 9.637, 10.39, 11.094, 12.521,
15.086, 14.875, 15.56, 15.396, 15.365, 16.238, 17.188, 17.16,
17.902, 15.802, 14.354, 12.045, 11.883, 12.716, 13.031, 11.346,
12.645, 13.082, 14.082, 14.606, 15.297, 15.215, 14.762, 15.61,
17.65, 17.997, 17.933, 17.884, 17.323, 17.169, 19.862, 23.073,
25.928, 27.872, 28.236, 30.207, 29.643, 28.742, 28.017, 25.973,
26.97, 28.061, 26.099, 25.133, 23.174, 20.483, 19.969, 21.094,
23.736, 26.382, 29.764, 33.129, 36.769, 38.491, 35.788, 35.61,
37.34, 35.794, 35.368, 34.635, 33.84, 33.404, 32.614, 30.159,
32.87, 33.89, 34.26, 33.309, 34.895, 34.596, 35.942, 37.642,
38.688, 39.047, 39.552, 40.228, 41.329, 42.208, 43.073, 41.838,
40.083, 40.6, 41.215, 41.782, 41.454, 41.65, 42.493, 42.297,
42.925, 43.51, 42.537, 42.897, 42.832, 42.54, 43.833, 44.497,
43.118, 42.778, 41.69, 40.37, 40.367, 43.267, 44.982, 46.84,
46.205, 46.376, 43.302, 40.977, 41.712, 42.041, 42.881, 43.467,
43.469, 45.591, 42.482, 45.196, 44.643, 43.234, 43.791, 45.037,
44.114, 43.488, 42.825, 41.382, 41.102, 39.78, 40.251, 40.823,
41.788, 43.272, 42.782, 41.288, 42.616, 44.222, 47.462, 48.783,
48.18, 48.067, 47.608, 46.76, 49.739, 47.404, 47.416, 44.787,
46.945, 49.951, 51.052, 50.017, 51.615, 51.965, 53.686, 54.802,
57.408, 57.979, 57.367, 57.363, 53.659, 50.096, 47.254, 43.275,
43.44, 42.811, 43.405, 47.903, 51.26, 52.405, 55.277, 54.637,
54.626, 54.327, 53.697, 53.366, 54.955, 52.65, 51.526, 52.107,
53.781, 50.12, 51.497, 54.426, 55.57, 54.662, 50.37, 54.62, 58.996,
60.24, 57.378, 56.576, 56.589, 58.439, 59.768, 59.263, 56.691,
59.286, 59.953, 60.097, 55.379, 49.2, 46.915, 52.828, 53.813,
52.989, 53.829, 53.209, 54.683, 55.321, 56.154, 55.016, 54.061,
53.296, 53.772, 52.342, 51.84, 52.607, 53.034, 54.727, 55.591,
54.567, 53.198, 50.51, 49.453, 49.908, 48.499, 47.923, 49.602,
48.64, 51.539, 52.353, 51.426, 50.687, 48.629, 46.685, 48.598,
48.944, 48.143, 46.887, 47.518, 46.229, 45.746, 46.078, 47.155,
46.933, 49.07, 49.243, 48.567, 48.352, 48.28, 48.954, 49.512,
48.704, 49.681, 50.053, 50.957, 49.603, 47.978, 50, 50.963, 50.415,
50.266, 50.155, 50.662, 51.918, 53.166, 52.792, 54.156, 55.07,
53.702, 54.07, 54.098, 54.46, 53.783, 54.023, 53.997, 55.079,
54.819, 54.935, 53.642, 54.181, 54.903, 56.18, 56.016, 55.307,
53.558, 52.03, 54.562, 58.919, 58.351, 57.536, 59.128, 60.585,
60.938, 60.858, 60.539, 59.114, 57.326, 58.094, 58.355, 58.507,
57.85, 55.829, 57.801, 59.946, 58.755, 56.767, 55.223, 55.452,
58.029, 60.207, 60.816, 60.403, 59.365, 58.132, 57.989, 59.469,
58.991, 58.748, 58.43, 57.926, 58.671, 58.192, 58.335, 58.222,
58.802, 58.458, 58.537, 59.014, 59.015, 57.656, 56.055, 53.788,
54.659, 53.911, 54.722, 56.387, 57.327, 57.251, 57.596, 56.207,
54.747, 53.934, 55.108, 55.669, 57.554, 57.824, 56.961, 54.605,
54.705, 54.599, 54.044, 53.627), Forecasted = c(4.843, 4.843,
4.843, 4.843, 3.695, 3.695, 3.695, 3.695, 3.313, 3.313, 3.313,
3.313, 3.198, 3.198, 3.198, 3.198, 3.74, 3.74, 3.74, 3.74, 5.115,
5.115, 5.115, 5.115, 7.01, 7.01, 7.01, 7.01, 9.236, 9.236, 9.236,
9.236, 16.695, 16.695, 16.695, 16.695, 28.66, 28.66, 28.66, 28.66,
34.778, 34.778, 34.778, 34.778, 35.03, 35.03, 35.03, 35.03, 32.805,
32.805, 32.805, 32.805, 28.593, 28.593, 28.593, 28.593, 22.283,
22.283, 22.283, 22.283, 19.164, 19.164, 19.164, 19.164, 17.839,
17.839, 17.839, 17.839, 15.454, 15.454, 15.454, 15.454, 12.195,
12.195, 12.195, 12.195, 8.568, 8.568, 8.568, 8.568, 7.477, 7.477,
7.477, 7.477, 6.314, 6.314, 6.314, 6.314, 5.763, 5.763, 5.763,
5.763, 4.615, 4.615, 4.615, 4.615, 3.57, 3.57, 3.57, 3.57, 3.438,
3.438, 3.438, 3.438, 3.618, 3.618, 3.618, 3.618, 2.983, 2.983,
2.983, 2.983, 2.513, 2.513, 2.513, 2.513, 2.075, 2.075, 2.075,
2.075, 2.015, 2.015, 2.015, 2.015, 2.315, 2.315, 2.315, 2.315,
2.325, 2.325, 2.325, 2.325, 2.363, 2.363, 2.363, 2.363, 2.058,
2.058, 2.058, 2.058, 1.455, 1.455, 1.455, 1.455, 1.878, 1.878,
1.878, 1.878, 2.165, 2.165, 2.165, 2.165, 2.633, 2.633, 2.633,
2.633, 3.279, 3.279, 3.279, 3.279, 2.935, 2.935, 2.935, 2.935,
2.833, 2.833, 2.833, 2.833, 2.89, 2.89, 2.89, 2.89, 3.952, 3.952,
3.952, 3.952, 4.037, 4.037, 4.037, 4.037, 4.311, 4.311, 4.311,
4.311, 4.242, 4.242, 4.242, 4.242, 2.478, 2.478, 2.478, 2.478,
6.285, 6.285, 6.285, 6.285, 6.803, 6.803, 6.803, 6.803, 7.54,
7.54, 7.54, 7.54, 8.383, 8.383, 8.383, 8.383, 8.86, 8.86, 8.86,
8.86, 8.65, 8.65, 8.65, 8.65, 8.11, 8.11, 8.11, 8.11, 7.783,
7.783, 7.783, 7.783, 6.995, 6.995, 6.995, 6.995, 5.98, 5.98,
5.98, 5.98, 5.22, 5.22, 5.22, 5.22, 5.023, 5.023, 5.023, 5.023,
7.343, 7.343, 7.343, 7.343, 7.29, 7.29, 7.29, 7.29, 7.315, 7.315,
7.315, 7.315, 7.63, 7.63, 7.63, 7.63, 7.875, 7.875, 7.875, 7.875,
7.993, 7.993, 7.993, 7.993, 5.315, 5.315, 5.315, 5.315, 5.787,
5.787, 5.787, 5.787, 5.097, 5.097, 5.097, 5.097, 6.878, 6.878,
6.878, 6.878, 6.337, 6.337, 6.337, 6.337, 4.025, 4.025, 4.025,
4.025, 2.665, 2.665, 2.665, 2.665, 4.573, 4.573, 4.573, 4.573,
6.248, 6.248, 6.248, 6.248, 5.534, 5.534, 5.534, 5.534, 6.268,
6.268, 6.268, 6.268, 5.964, 5.964, 5.964, 5.964, 5.23, 5.23,
5.23, 5.23, 7.155, 7.155, 7.155, 7.155, 9.775, 9.775, 9.775,
9.775, 12.565, 12.565, 12.565, 12.565, 11.868, 11.868, 11.868,
11.868, 11.473, 11.473, 11.473, 11.473, 10.698, 10.698, 10.698,
10.698, 12.933, 12.933, 12.933, 12.933, 13.148, 13.148, 13.148,
13.148, 12.845, 12.845, 12.845, 12.845, 11.588, 11.588, 11.588,
11.588, 16.374, 16.374, 16.374, 16.374, 19.543, 19.543, 19.543,
19.543, 21.938, 21.938, 21.938, 21.938, 22.425, 22.425, 22.425,
22.425, 22.738, 22.738, 22.738, 22.738, 28.695, 28.695, 28.695,
28.695, 29.352, 29.352, 29.352, 29.352, 34.76, 34.76, 34.76,
34.76, 31.81, 31.81, 31.81, 31.81, 33.791, 33.791, 33.791, 33.791,
35.23, 35.23, 35.23, 35.23, 35.308, 35.308, 35.308, 35.308, 35.13,
35.13, 35.13, 35.13, 37.11, 37.11, 37.11, 37.11, 37.073, 37.073,
37.073, 37.073, 40.596, 40.596, 40.596, 40.596, 40.7, 40.7, 40.7,
40.7, 45.062, 45.062, 45.062, 45.062, 43.892, 43.892, 43.892,
43.892, 44.33, 44.33, 44.33, 44.33, 42.638, 42.638, 42.638, 42.638,
45.95, 45.95, 45.95, 45.95, 50.997, 50.997, 50.997, 50.997, 50.458,
50.458, 50.458, 50.458, 49.61, 49.61, 49.61, 49.61, 50.95, 50.95,
50.95, 50.95, 49.273, 49.273, 49.273, 49.273, 46.993, 46.993,
46.993, 46.993, 47.368, 47.368, 47.368, 47.368, 48.526, 48.526,
48.526, 48.526, 51.974, 51.974, 51.974, 51.974, 61.343, 61.343,
61.343, 61.343, 61.38, 61.38, 61.38, 61.38, 61.763, 61.763, 61.763,
61.763, 61.96, 61.96, 61.96, 61.96, 59.295, 59.295, 59.295, 59.295,
59.98, 59.98, 59.98, 59.98, 59.233, 59.233, 59.233, 59.233, 56.633,
56.633, 56.633, 56.633, 57.81, 57.81, 57.81, 57.81, 63.66, 63.66,
63.66, 63.66, 67.34, 67.34, 67.34, 67.34, 67.59, 67.59, 67.59,
67.59, 68.453, 68.453, 68.453, 68.453, 66.291, 66.291, 66.291,
66.291, 62.495, 62.495, 62.495, 62.495, 61.142, 61.142, 61.142,
61.142, 51.96, 51.96, 51.96, 51.96, 48.465, 48.465, 48.465, 48.465,
51.932, 51.932, 51.932, 51.932, 54.498, 54.498, 54.498, 54.498,
56.787, 56.787, 56.787, 56.787, 58.053, 58.053, 58.053, 58.053,
57.649, 57.649, 57.649, 57.649, 54.156, 54.156, 54.156, 54.156,
39.908, 39.908, 39.908, 39.908, 40.138, 40.138, 40.138, 40.138,
41.303, 41.303, 41.303, 41.303, 41.175, 41.175, 41.175, 41.175,
39.023, 39.023, 39.023, 39.023, 39.09, 39.09, 39.09, 39.09, 39.238,
39.238, 39.238, 39.238, 44.308, 44.308, 44.308, 44.308, 41.595,
41.595, 41.595, 41.595, 45.503, 45.503, 45.503, 45.503, 45.773,
45.773, 45.773, 45.773, 46.703, 46.703, 46.703, 46.703, 51.37,
51.37, 51.37, 51.37, 50.908, 50.908, 50.908, 50.908, 46.036,
46.036, 46.036, 46.036, 43.526, 43.526, 43.526, 43.526, 38.05,
38.05, 38.05, 38.05, 34.29, 34.29, 34.29, 34.29, 34.306, 34.306,
34.306, 34.306, 37.668, 37.668, 37.668, 37.668, 41.563, 41.563,
41.563, 41.563, 41.171, 41.171, 41.171, 41.171, 42.225, 42.225,
42.225, 42.225, 43.463, 43.463, 43.463, 43.463)), row.names = c(NA,
672L), class = "data.frame")
I want to calculate the mean absolute percentage error for each (calendar) week of the dataset and I have tried several ways, such as the function apply.weekly, but none of them seems to work.
apply.weekly(df, function(x) mean(abs((df$Actual-df$Forecasted)/df$Actual)))
I keep getting as outcome the same value for each quarter of the dataset, and not an outcome for each week.
Do you have any ideas on how to implement this calculation?
Thank you in advance for your help.
Perhaps like so:
library(lubridate)
library(magrittr)
df %>% group_by( isoweek( DATETIME ), year( DATETIME ) ) %>%
summarise( DateCount = n_distinct(date(DATETIME)), MeanError = mean( abs(Forecasted-Actual)/Actual ) )
It outputs:
`isoweek(DATETIME)` `year(DATETIME)` DateCount MeanError
<dbl> <dbl> <int> <dbl>
1 44 2020 1 0.858
2 45 2020 6 0.357
With the updated data source:
df <- read.delim( "https://raw.githubusercontent.com/Argiro1983/data-set/main/test1", sep=";" ) %>%
as_tibble
df %<>% mutate( DATETIME = dmy_hm(DATETIME) )
df %>% group_by( year( DATETIME ), isoweek( DATETIME ) ) %>%
summarise( DateCount = n_distinct(date(DATETIME)), MeanError = mean( abs(Forecasted-Actual)/Actual ) )
Output:
`year(DATETIME)` `isoweek(DATETIME)` DateCount MeanError
<dbl> <dbl> <int> <dbl>
1 2020 44 1 0.858
2 2020 45 7 0.321
3 2020 46 7 0.505
4 2020 47 7 0.439
5 2020 48 7 0.309
6 2020 49 7 0.545
7 2020 50 7 0.342
8 2020 51 7 0.357
9 2020 52 7 0.204
10 2020 53 4 0.120
# … with 14 more rows
Dates should work fine if you set them up correctly.
As said, I am looking for a density / probability estimation. See here for the difference to heat maps and I checked that sum(kde$fhat)*diff(kde$x1[1:2])*diff(kde$x2[1:2]) = 1 (you can run it with the example, all diffs are almost equal).
I would expect a qualitatively similar plot to this Python example (you have to scroll down a bit...),
but I get
Increasing gridsize improved the situation but faces its limits (see limit.memory()), the bandwidth slightly changes the picture, but the main feature (low density over a larger area, no density for a huge area) still remains unresolved.
What's the mistake? The algorithm or anything else? As I have bigger data sets, I include a set with 1000 points below. Reproducible example (more or less from here):
library("leaflet")
library("KernSmooth")
gc()
memory.limit(size=16000)
kde <- KernSmooth::bkde2D(dat[ , c("longitude", "latitude")],
bandwidth=c(.0045, .0068),
# bandwidth=c(.1, .1), # does not improve...
gridsize = c(6000,6000))
CL <- contourLines(kde$x1 , kde$x2 , kde$fhat)
LEVS <- as.factor(sapply(CL, `[[`, "level"))
NLEV <- length(levels(LEVS))
pgons <- lapply(1:length(CL), function(i)
sp::Polygons(list(sp::Polygon(cbind(CL[[i]]$x, CL[[i]]$y))), ID=i))
spgons = sp::SpatialPolygons(pgons)
leaflet(spgons) %>% addTiles() %>%
addCircles(lng = dat$longitude, lat = dat$latitude,
radius = .5, opacity = .9, col = "blue") %>%
addPolygons(color = heat.colors(NLEV, NULL)[LEVS])
Edit: Useful Link with overview of available packages
Data
dat <- data.frame(latitude = c(
47.086, 47.086, 46.979, 46.968, 47.053, 47.078, 47.044, 47.054,
46.935, 47.053, 46.832, 47.047, 47.065,
46.983, 47.082, 47.194, 46.962, 47.056, 46.977, 46.977, 47.171,
47.075, 47.056, 47.17, 47.043, 47.119, 47.166, 47.053, 46.771,
47.166, 47.064, 47.082, 47.144, 47.034, 47.065, 47.015, 47.032,
47.166, 47.085, 47.046, 47.209, 46.824, 47.058, 47.088, 47.083,
47.054, 47.05, 47.079, 47.046, 46.957, 47.131, 47.062, 46.962,
46.958, 47.218, 47.019, 47.042, 47.209, 47.049, 47.119, 47.041,
46.901, 46.736, 47.235, 47.05, 47.052, 47.047, 47.058, 47.135,
47.044, 47.166, 47.181, 46.88, 47.058, 47.048, 47.035, 47.058,
47.059, 46.881, 46.89, 47.065, 47.087, 47.077, 47.229, 47.082,
46.89, 47.054, 47.142, 47.048, 47.047, 47.062, 47.058, 47.026,
47.037, 47.038, 46.883, 47.059, 47.081, 47.051, 47.058, 47.249,
47.028, 47.217, 47.06, 47.05, 47.047, 47.038, 47.046, 46.962,
47.175, 47.057, 47.065, 46.821, 47.22, 46.945, 47.047, 47.151,
47.051, 47.21, 47.194, 47.171, 46.954, 46.958, 47.148, 46.818,
46.88, 46.872, 47.089, 47.037, 47.142, 47.096, 47.14, 46.969,
47.047, 47.085, 47.051, 47.13, 47.063, 47.06, 47.101, 47.05,
46.987, 47.082, 47.05, 46.89, 47.053, 47.056, 47.122, 47.041,
46.887, 47.055, 47.056, 47.042, 47.059, 46.987, 47.124, 47.062,
47.131, 47.059, 47.047, 47.055, 47.04, 47.075, 47.047, 46.959,
47.059, 47.049, 47.206, 46.9, 47.123, 47.015, 47.219, 47.166,
47.027, 47.021, 47.06, 47.153, 47.047, 47.057, 47.029, 47.042,
47.074, 47.214, 47.112, 47.086, 47.223, 47.018, 47.081, 47.173,
47.098, 46.961, 47.055, 46.973, 46.989, 47.018, 47.046, 46.944,
46.873, 47.056, 47.047, 47.223, 47.051, 47.073, 47.119, 46.962,
46.963, 47.057, 47.166, 47.047, 47.065, 47.167, 47.058, 46.87,
47.088, 47.051, 47.154, 47.035, 47.049, 46.714, 47.229, 47.09,
47.124, 47.059, 47.038, 47.038, 47.367, 46.975, 47.057, 46.833,
47.05, 47.04, 46.89, 47.139, 47.053, 47.051, 47.088, 47.051,
47.022, 46.878, 46.983, 47.075, 47.05, 47.194, 47.047, 47.188,
47.166, 47.006, 46.962, 47.062, 47.125, 46.881, 47.126, 46.957,
47.034, 47.073, 47.029, 47.075, 47.015, 47.042, 47.168, 47.096,
47.047, 47.073, 47.053, 47.047, 46.962, 46.89, 47.033, 47.033,
47.062, 46.707, 47.083, 47.051, 47.172, 47.059, 47.113, 47.032,
47.088, 47.062, 47.062, 46.89, 47.103, 47.043, 47.166, 47.058,
47.14, 47.073, 47.082, 47.153, 46.962, 47.059, 47.166, 47.059,
47.069, 47.227, 46.89, 47.232, 47.13, 47.021, 46.9, 47.085, 47.058,
47.09, 47.07, 46.906, 47.223, 46.894, 46.91, 47.08, 47.058, 47.242,
46.84, 46.89, 47.128, 47.182, 47.093, 47.05, 47.033, 47.107,
47.111, 47.166, 47.04, 47.166, 47.058, 46.89, 47.059, 47.058,
47.044, 47.059, 47.058, 46.956, 47.044, 47.152, 47.136, 47.166,
47.15, 47.052, 46.968, 47.232, 46.862, 47.034, 47.051, 47.089,
47.17, 47.084, 47.225, 47.167, 47.042, 47.078, 47.062, 47.086,
47.062, 47.061, 46.96, 47.046, 47.028, 47.058, 47.075, 47.062,
46.9, 47.125, 47.032, 47.044, 46.825, 47.048, 47.033, 47.049,
46.88, 47.157, 47.139, 47.202, 47.202, 47.059, 47.08, 47.039,
47.062, 47.166, 47.042, 47.231, 47.113, 47.056, 47.047, 47.052,
47.02, 47.156, 47.207, 46.893, 47.04, 47.065, 47.087, 47.063,
47.224, 47.02, 47.112, 47.062, 47.114, 47.058, 47.168, 47.048,
47.058, 47.055, 47.166, 46.994, 47.122, 47.081, 47.101, 47.047,
46.89, 46.978, 46.957, 47.062, 47.062, 47.038, 47.238, 47.166,
46.962, 47.038, 46.89, 47.059, 47.041, 47.047, 47.069, 46.975,
47.08, 47.086, 47.056, 47.055, 47.053, 47.074, 47.034, 47.051,
47.047, 46.875, 47.111, 47.119, 47.062, 47.058, 47.025, 47.062,
47.045, 47.117, 46.887, 46.962, 47.047, 47.107, 47.106, 47.047,
47.173, 47.184, 46.905, 47.166, 47.062, 47.153, 47.02, 47.161,
47.059, 47.055, 47.059, 47.209, 47.021, 47.058, 46.89, 47.044,
47.055, 47.13, 47.016, 47.034, 47.039, 47.188, 46.869, 47.165,
46.986, 47.049, 47.058, 47.039, 47.069, 47.036, 47.043, 47.053,
46.875, 47.103, 47.098, 47.201, 47.015, 47.047, 47.22, 47.092,
47.049, 47.209, 46.9, 47.171, 47.065, 46.956, 47.2, 47.058, 46.899,
47.032, 47.032, 47.049, 46.99, 46.944, 47.068, 47.05, 47.039,
47.11, 47.085, 46.891, 47.051, 47.225, 47.167, 46.897, 47.048,
47.05, 47.166, 47.166, 47.15, 47.222, 47.101, 46.976, 47.126,
47.061, 47.09, 47.373, 47.083, 47.039, 47.197, 46.962, 46.887,
46.89, 47.097, 47.142, 47.08, 47.062, 47.044, 47.058, 47.047,
47.212, 47.047, 47.161, 47.062, 47.045, 47.042, 46.986, 47.042,
47.137, 47.048, 46.951, 47.062, 46.954, 47.065, 47.054, 47.054,
47.044, 47.068, 46.877, 47.111, 47.173, 47.075, 47.166, 47.135,
46.957, 47.076, 46.966, 46.89, 46.985, 47.053, 47.2, 47.166,
47.065, 47.147, 47.055, 47.059, 47.088, 47.073, 47.118, 47.058,
47.058, 47.058, 47.131, 47.047, 47.033, 47.094, 47.052, 47.059,
46.904, 47.056, 47.042, 47.183, 46.884, 47.086, 47.086, 47.076,
46.89, 47.062, 46.729, 47.047, 47.152, 47.031, 47.082, 47.033,
47.174, 46.962, 47.042, 47.071, 46.869, 47.09, 47.142, 47.096,
47.071, 47.044, 47.142, 47.042, 46.884, 47.167, 46.977, 47.049,
47.059, 47.047, 46.89, 47.047, 47.051, 47.033, 47.044, 47.203,
46.823, 47.087, 47.059, 47.047, 47.177, 47.048, 46.99, 47.051,
47.061, 46.987, 47.073, 47.078, 47.055, 47.094, 46.893, 47.08,
47.171, 47.049, 47.064, 46.962, 47.054, 47.055, 46.872, 47.131,
46.872, 47.154, 47.029, 47.048, 46.872, 46.872, 46.872, 46.872,
46.872, 46.866, 47.053, 47.166, 47.042, 47.061, 47.244, 47.078,
47.124, 46.978, 47.059, 47.166, 47.074, 47.059, 47.038, 47.245,
47.245, 46.972, 46.887, 47.063, 47.057, 47.165, 47.02, 47.147,
47.017, 47.06, 47.06, 46.96, 47.05, 47.021, 46.962, 47.058, 46.987,
47.164, 47.142, 46.948, 46.967, 47.036, 47.047, 47.065, 47.066,
47.172, 47.049, 46.91, 47.084, 46.968, 46.956, 47.244, 47.115,
47.075, 47.066, 47.059, 47.027, 46.88, 47.085, 47.039, 47.033,
47.062, 47.166, 47.058, 47.17, 47.048, 47.113, 47.049, 46.825,
47.055, 47.069, 47.073, 47.03, 46.89, 47.058, 46.955, 47.037,
46.878, 47.171, 47.051, 46.957, 47.209, 47.032, 47.08, 47.151,
47.155, 47.081, 47.039, 47.168, 47.05, 47.082, 46.786, 46.837,
47.165, 46.869, 47.04, 46.907, 47.011, 47.02, 47.226, 47.047,
47.058, 47.161, 46.898, 46.787, 47.032, 46.889, 46.992, 47.016,
47.047, 47.053, 47.119, 47.061, 47.132, 47.046, 47.056, 47.166,
46.875, 47.21, 47.035, 47.053, 47.047, 47.131, 47.166, 47.042,
46.819, 47.07, 47.062, 47.08, 47.212, 47.049, 47.037, 47.08,
47.011, 46.89, 47.085, 47.092, 47.272, 47.035, 47.232, 47.035,
47.171, 47.067, 46.951, 47.086, 47.138, 47.165, 47.047, 46.882,
46.994, 46.89, 47.053, 47.166, 47.095, 47.051, 47.166, 47.047,
47.021, 47.236, 47.043, 47.187, 47.166, 47.062, 47.026, 46.862,
47.044, 47.044, 47.085, 47.053, 47.062, 46.89, 46.962, 47.166,
47.047, 47.059, 46.962, 47.074, 47.106, 47.051, 47.167, 46.966,
47.056, 47.062, 47.062, 47.062, 46.9, 47.047, 47.053, 47.077,
47.055, 47.037, 47.066, 47.042, 47.156, 47.037, 47.078, 47.13,
47.092, 46.984, 46.862, 47.093, 47.209, 47.204, 47.122, 47.051,
47.038, 47.142, 47.135, 47.129, 47.152, 47.083, 47.044, 47.053,
47.177, 47.062, 47.047, 46.944, 47.055, 47.199, 47.079, 47.036,
47.044, 47.051, 46.962, 47.079, 47.021, 46.962, 46.962, 46.897,
47.122, 47.165, 47.058, 47.055, 47.084, 47.069, 47.058, 46.91,
47.058, 47.034, 47.172, 47.065, 47.032, 47.033, 47.049, 46.704,
47.153, 47.147, 47.057, 47.035, 46.884, 47.051, 47.059, 47.151,
47.032, 47.087, 46.962, 47.014, 46.962, 47.036, 47.058, 46.963,
47.075, 46.865, 47.171, 47.064, 46.828, 47.047, 47.076, 47.059,
47.042, 47.06, 47.029, 47.166, 47.101, 47.105, 47.074, 46.962,
47.161, 47.058, 47.142, 46.979, 46.926, 47.05, 47.141, 47.058,
47.058, 47.075, 47.062, 47.176, 47.065, 47.059, 46.979, 47.095,
46.962, 47.237, 47.05, 47.012, 47.023, 47.033, 47.062, 47.122,
47.168, 47.063, 47.061, 46.859, 46.889, 47.029, 47.065, 47.039,
46.962, 47.032, 47.044, 46.866, 46.89, 47.209, 47.18, 46.857,
47.031, 47.021, 46.991, 47.048, 46.962, 46.884, 46.895, 46.884,
47.165, 46.957, 46.962, 47.047, 47.08, 47.08, 46.952, 47.115,
47.059, 46.874, 47.05, 47.211, 47.214, 47.183, 46.965, 47.075,
47.051, 46.861, 47.209, 47.076, 47.041, 46.881, 47.024),
longitude = c(8.262, 8.435, 8.337, 8.364, 8.301, 8.361, 8.297, 8.337, 8.394, 8.283,
8.289, 8.307, 8.078, 8.41, 8.344, 8.521, 8.356, 8.312, 8.306,
8.306, 8.104, 8.438, 8.098, 8.29, 8.373, 8.342, 8.118, 8.3, 8.671,
8.118, 8.278, 8.13, 8.014, 8.332, 8.318, 8.478, 8.284, 8.118,
8.351, 8.302, 8.192, 8.65, 8.294, 8.319, 8.444, 8.069, 8.311,
8.299, 8.302, 8.371, 8.192, 8.299, 8.356, 8.369, 7.95, 8.311,
8.185, 8.187, 8.339, 7.988, 8.324, 8.281, 8.628, 8.078, 8.314,
8.294, 8.314, 8.326, 8.192, 8.297, 8.118, 8.283, 8.016, 8.294,
8.376, 8.244, 8.294, 8.348, 8.645, 8.244, 8.078, 8.34, 8.277,
7.844, 8.344, 8.244, 8.385, 8.132, 8.309, 8.314, 8.299, 8.294,
8.307, 8.155, 8.288, 8.646, 8.294, 8.44, 8.311, 8.284, 7.97,
8.305, 8.267, 8.285, 8.309, 8.298, 8.261, 8.38, 8.356, 8.128,
8.289, 8.073, 8.653, 8.158, 7.424, 8.314, 8.318, 8.339, 8.097,
8.515, 8.104, 8.047, 8.39, 8.223, 8.411, 8.244, 8.376, 8.278,
8.281, 7.973, 8.279, 7.924, 8.352, 8.314, 8.351, 8.262, 8.319,
8.319, 8.275, 8.28, 8.275, 8.311, 8.304, 8.311, 8.244, 8.259,
8.32, 7.99, 8.298, 8.231, 8.356, 8.292, 8.318, 8.279, 8.311,
8.349, 8.299, 8.315, 8.294, 8.302, 8.332, 8.323, 8.279, 8.314,
8.36, 8.279, 8.299, 8.194, 8.269, 8.397, 8.31, 8.262, 8.118,
8.279, 8.316, 8.276, 8.163, 8.314, 8.324, 8.421, 8.311, 8.36,
7.989, 8.096, 8.397, 8.113, 8.316, 8.441, 7.989, 8.267, 8.363,
8.282, 8.338, 8.313, 8.311, 8.309, 8.277, 8.271, 8.313, 8.314,
8.253, 8.311, 8.274, 7.991, 8.356, 8.37, 8.331, 8.118, 8.314,
8.078, 8.289, 8.294, 8.655, 8.272, 8.306, 8.324, 8.292, 8.299,
8.612, 8.262, 8.267, 8.352, 8.285, 8.425, 8.312, 8.499, 8.427,
8.302, 8.184, 8.256, 8.309, 8.244, 8.194, 8.267, 8.311, 7.959,
8.308, 8.312, 8.713, 8.403, 8.29, 8.301, 8.515, 8.314, 8.017,
8.118, 8.305, 8.356, 8.28, 8.386, 8.253, 8.199, 8.371, 8.423,
8.288, 8.335, 8.329, 8.48, 8.326, 8.106, 8.454, 8.264, 8.275,
8.35, 8.314, 8.356, 8.244, 8.286, 8.284, 8.299, 8.597, 8.344,
8.337, 8.283, 8.294, 8.393, 8.279, 8.354, 8.299, 8.299, 8.244,
8.342, 8.362, 8.118, 8.294, 8.192, 8.26, 8.312, 8.168, 8.356,
8.29, 8.118, 8.428, 8.316, 8.078, 8.244, 8.08, 8.138, 8.316,
8.275, 8.351, 8.294, 8.267, 8.325, 7.93, 8.11, 8.249, 8.622,
8.44, 8.326, 8.226, 8.175, 8.244, 8.236, 7.982, 7.97, 8.307,
8.272, 8.194, 8.195, 8.118, 8.319, 8.118, 8.294, 8.244, 8.279,
8.294, 8.297, 8.279, 8.294, 8.382, 8.297, 8.089, 8.317, 8.118,
8.172, 8.284, 8.364, 8.078, 8.644, 8.27, 8.309, 8.278, 8.115,
8.041, 8.081, 8.295, 8.366, 8.333, 8.299, 8.294, 8.315, 8.388,
8.528, 8.305, 8.294, 8.294, 8.286, 8.299, 8.274, 8.446, 8.279,
8.297, 8.651, 8.313, 8.329, 8.313, 8.646, 8.116, 7.919, 8.234,
8.234, 8.294, 8.345, 8.303, 8.312, 8.118, 8.187, 8.237, 8.096,
7.949, 8.302, 8.3, 8.315, 8.316, 8.189, 8.245, 8.307, 8.078,
8.289, 8.073, 8.26, 8.311, 8.454, 8.299, 8.277, 8.294, 8.098,
8.309, 8.308, 8.321, 8.118, 8.308, 8.355, 8.348, 8.253, 8.314,
8.249, 8.341, 8.369, 8.299, 8.299, 8.181, 8.139, 8.118, 8.356,
8.327, 8.249, 8.294, 8.298, 8.314, 8.317, 8.343, 8.345, 8.039,
8.35, 8.299, 8.307, 8.366, 8.266, 8.296, 8.315, 8.641, 8.195,
7.988, 8.299, 8.326, 8.307, 8.315, 8.303, 7.984, 8.231, 8.356,
8.314, 8.451, 8.355, 8.068, 8.1, 8.027, 8.274, 8.118, 8.074,
8.325, 8.278, 8.104, 8.301, 8.318, 8.294, 8.196, 8.308, 8.315,
8.244, 8.306, 8.318, 8.319, 8.301, 8.143, 8.179, 7.975, 8.643,
8.114, 8.312, 8.306, 8.302, 8.289, 8.36, 8.321, 8.304, 8.302,
8.664, 8.267, 8.465, 8.247, 8.309, 8.305, 8.249, 8.395, 8.268,
8.178, 8.269, 8.285, 8.078, 8.273, 8.314, 8.294, 8.262, 8.278,
8.285, 8.312, 8.424, 8.277, 8.366, 8.309, 8.325, 8.361, 8.437,
8.239, 8.308, 8.147, 8.291, 8.391, 8.301, 8.312, 8.118, 8.118,
8.229, 8.264, 8.269, 8.344, 8.402, 8.353, 8.105, 8.559, 8.337,
8.32, 8.229, 8.356, 8.231, 8.244, 8.276, 8.132, 8.287, 8.299,
8.297, 8.294, 8.264, 8.016, 8.314, 8.296, 8.299, 8.28, 8.301,
8.412, 8.301, 8.315, 8.364, 8.383, 8.299, 8.014, 8.078, 8.346,
8.346, 8.314, 8.317, 8.643, 8.321, 8.1, 8.27, 8.118, 8.131, 8.336,
8.286, 8.47, 8.244, 8.312, 8.345, 8.314, 8.118, 8.078, 8.016,
8.311, 8.294, 8.292, 8.295, 7.987, 8.294, 8.294, 8.294, 8.315,
8.314, 8.327, 8.344, 8.294, 8.294, 8.277, 8.349, 8.14, 8.006,
8.637, 8.346, 8.03, 8.267, 8.244, 8.299, 8.546, 8.289, 8.167,
8.278, 7.954, 8.286, 8.103, 8.356, 8.301, 8.288, 8.652, 8.267,
8.132, 8.369, 8.282, 8.297, 8.132, 8.315, 8.637, 8.291, 8.417,
8.262, 8.279, 8.314, 8.244, 8.314, 8.311, 8.293, 8.3, 8.432,
8.404, 8.289, 8.294, 8.298, 8.101, 8.267, 8.306, 8.311, 8.306,
8.311, 8.434, 8.343, 8.324, 8.372, 8.245, 8.301, 8.113, 8.262,
8.306, 8.356, 8.311, 8.304, 8.376, 8.192, 8.376, 8.03, 8.421,
8.348, 8.376, 8.376, 8.376, 8.376, 8.376, 8.65, 8.338, 8.118,
8.324, 8.281, 8.137, 8.343, 8.163, 8.415, 8.294, 8.118, 8.329,
8.294, 8.321, 8.223, 8.223, 8.444, 8.231, 8.303, 8.301, 8.29,
8.307, 8.234, 8.31, 8.068, 8.347, 8.362, 8.307, 8.316, 8.356,
8.294, 8.415, 8.298, 8.132, 8.05, 8.422, 8.329, 8.314, 8.078,
8.017, 8.096, 8.309, 7.936, 8.31, 8.364, 8.365, 7.966, 7.993,
8.283, 8.077, 8.294, 8.302, 8.244, 8.434, 8.313, 8.286, 8.286,
8.118, 8.294, 8.105, 8.376, 8.393, 8.313, 8.391, 8.346, 8.316,
8.275, 8.25, 8.244, 8.326, 8.362, 8.311, 8.647, 8.106, 8.308,
8.37, 8.196, 8.284, 8.084, 8.167, 8.214, 8.277, 8.185, 8.142,
8.264, 8.444, 8.162, 8.634, 8.291, 8.644, 8.182, 8.297, 8.339,
8.315, 8.255, 8.314, 8.326, 8.291, 8.248, 8.159, 8.284, 8.206,
8.305, 8.661, 8.302, 8.069, 7.992, 8.31, 8.195, 8.191, 8.344,
8.118, 8.664, 7.979, 8.281, 8.286, 8.302, 8.195, 8.118, 8.326,
8.387, 8.325, 8.299, 8.281, 8.093, 8.303, 8.272, 8.281, 8.293,
8.249, 8.349, 8.342, 8.235, 8.259, 8.08, 8.259, 8.293, 8.314,
8.62, 8.442, 8.083, 8.297, 8.264, 8.247, 8.308, 8.244, 8.3, 8.118,
8.278, 8.298, 8.118, 8.29, 8.316, 8.077, 8.313, 8.237, 8.118,
8.299, 8.307, 8.644, 8.35, 8.297, 8.351, 8.284, 8.299, 8.244,
8.356, 8.118, 8.298, 8.294, 8.356, 8.335, 8.374, 8.27, 8.118,
8.382, 8.301, 8.299, 8.299, 8.299, 8.626, 8.298, 8.285, 8.277,
8.324, 8.282, 8.314, 8.301, 8.328, 8.183, 8.264, 8.311, 8.35,
8.413, 8.644, 8.275, 8.093, 8.039, 7.99, 8.347, 8.306, 8.132,
7.924, 8.319, 8.115, 8.125, 8.297, 8.277, 8.209, 8.299, 8.314,
8.277, 8.308, 7.967, 8.124, 8.431, 8.314, 8.311, 8.356, 8.27,
8.316, 8.356, 8.356, 8.26, 7.987, 8.297, 8.294, 8.316, 8.345,
8.281, 8.088, 8.622, 8.294, 8.27, 8.096, 8.078, 8.284, 8.449,
8.301, 8.6, 8.325, 8.108, 8.341, 8.328, 8.637, 8.298, 8.294,
8.227, 8.442, 8.037, 8.356, 8.305, 8.356, 8.333, 8.326, 8.363,
8.283, 8.649, 8.102, 8.316, 8.188, 8.314, 8.267, 8.279, 8.318,
8.347, 8.056, 8.118, 8.203, 8.269, 8.338, 8.356, 8.265, 8.326,
8.132, 8.341, 8.26, 8.339, 8.327, 8.294, 8.294, 8.438, 8.315,
8.136, 8.078, 8.294, 8.419, 8.27, 8.016, 8.073, 8.34, 8.486,
8.307, 8.275, 8.315, 7.99, 7.996, 8.303, 8.275, 8.655, 8.249,
8.421, 8.078, 8.314, 8.356, 8.289, 8.297, 8.237, 8.244, 8.178,
8.112, 8.646, 8.399, 8.316, 8.307, 8.301, 8.356, 8.637, 8.621,
8.637, 8.297, 8.372, 8.356, 8.314, 8.441, 8.345, 8.016, 8.39,
8.279, 8.674, 8.312, 7.99, 7.991, 8.023, 8.475, 8.283, 8.308,
8.658, 8.096, 8.439, 8.306, 8.645, 8.454),
stringsAsFactors = FALSE)
You may want to look at leaflet.extras. It has inbuilt heat map opportunities.
library(magrittr)
library(leaflet)
library(leaflet.extras)
leaflet(dat) %>% addTiles() %>%
addHeatmap(lng = ~longitude, lat = ~latitude, radius = 10, minOpacity = .5) %>%
addCircles(lng = ~longitude, lat = ~latitude)
To further fit it to your needs you may want to play with radius, blur, minOpacity and max attributes. To adjust the color palette you can use gradient.
For example:
leaflet(dat) %>% addTiles() %>%
addHeatmap(lng = ~longitude, lat = ~latitude, blur = 20, radius = 10, minOpacity = .5, gradient = RColorBrewer::brewer.pal(7, "Reds")) %>%
addCircles(lng = ~longitude, lat = ~latitude)
I've been struggling for a few days with this problem, so help would be much appreciated. I have a line in a function that goes like this:
sig <- which(!ma.sig[1,]==0)&which(!pricebreak[1,]==0) ## Retrieves signals
sig <- which(sig)
for (i in 1:length(sig))
cursig[i] <- cur[i]
}
Basically what I'm doing is calculating signals, and if these signals are 0 or 1 I want R to save the corresponding variables. So for example, here's what the signal table might look like:
>ma.sig(MergedData)
Eur JPY GBP CHF AUD CAD NZD SEK NOK
1 0 1 0 1 1 0 0 0
>price.break(MergedData)
Eur JPY GBP CHF AUD CAD NZD SEK NOK
1 0 1 0 1 1 0 1 1
And so on for 29 variables. And here's the original currency, cur, vector:
> cur
[1] "EUR" "JPY" "GBP" "CHF" "AUD" "CAD" "NZD" "SEK" "NOK" "CZK" "HUF" "ILS" "PLN" "RUB" "TRY"
[16] "ZAR" "BRL" "CLP" "COP" "MXN" "PEN" "CNY" "IDR" "INR" "KRW" "MYR" "SGD" "PHP" "THB"
So basically I want to save a variable cursig such that it displays only the currencies with a signal of 1. So I'll have something like this:
cursig
[1] "GBP" " AUD" "CAD"....
And so on for whichever currency displays 1 for both signal functions. My problem is that
cursig[i] <- cur[i]
Returns the very last currency for which the signals are true. I know this is probably a very rookie mistake, but a nudge in the right direction would be much appreciated.
EDIT:
Here's the full function:
new.trend <- function(MergedData)
{
ret <- as.list(rep(NA, length(MergedData)))
ma.sig <- ma.crossover(MergedData)
pricebreak <- price.channel(MergedData)
sig <- which(!ma.sig[1,]==0)&which(!pricebreak[1,]==0) ## Retrieves signals
sig <- which(sig)
cursig <- character() #Preparing to save currency variables
for (i in 1:length(sig)) { #Calculates output variables based on active signals
x <- MergedData[[i]]
x <- xts(x[,-1], order.by=x[,1])
dev20 <- (x[,5]-SMA(x[,5], n=20))/x[,5]*100
dev50 <- (x[,5]-SMA(x[,5], n=50))/x[,5]*100
RSI <- RSI(x[,5], n=14)
cursig <- c(cursig, cur[1])
ret[[i]] <- na.omit(merge(tail(dev20, n=1L), tail(dev50, n=1L), tail(RSI, n=1L)))
}
na.omit(do.call(merge,ret))
}
print(new.trend(MergedData))
The reason I'm using a for() loop is because MergedData consits of 29 dataframes such that MergedData[[1] returns something like:
Date EUR.OPEN EUR.HIGH EUR.LOW EUR.CLOSE EUR.LAST
1 2016-08-30 1.1153 1.1192 1.1153 1.1161 1.1161
2 2016-08-29 1.1158 1.1208 1.1158 1.1189 1.1189
3 2016-08-28 1.1181 1.1341 1.1181 1.1198 1.1198
4 2016-08-27 1.1181 1.1341 1.1181 1.1198 1.1198
5 2016-08-26 1.1181 1.1341 1.1181 1.1198 1.1198
And so on for 29 currencies listed in cur.
Sample dput is here:
tzone = "", class = c("POSIXct",
"POSIXt")), PHP.OPEN = c(46.385, 46.283, 46.283, 46.283,
46.283, 46.33, 46.45, 46.45, 46.5, 46.29, 46.29, 46.29, 46.235,
46.28, 46.225, 46.445, 46.455, 46.455, 46.455, 46.365, 46.73,
46.85, 46.423, 46.05, 46.05, 46.05, 46.975, 46.03, 46.035,
46.915, 47.017, 47.017, 47.017, 47.05, 46.825, 47.105, 47.12,
47, 47, 47, 47.055, 46.015, 46.725, 46.675, 46.671, 46.671,
46.671, 47.019, 47, 47.055, 47.033, 47, 47, 47, 46.85, 46.765,
46.765, 46.753, 46.465, 46.465, 46.465, 46.035, 46.49, 45.93,
46.955, 46.51, 46.51, 46.51, 46.475, 46.365, 46.31, 46.3,
44.623, 44.623, 44.623, 46.285, 46.31, 46.073, 46.07, 46.06,
46.06, 46.06, 45.859, 46, 45.728, 46.2, 46.452, 46.452, 46.452,
46.53, 46.64, 46.653, 46.69, 46.614, 46.614, 46.614, 46.429,
46.63, 46.788, 46.69, 46.714, 46.714, 46.714, 46.705, 46.465,
46.325, 46.055, 46.55, 46.55, 46.55, 46.458, 46.53, 46.753,
47.085, 47.085, 47.085, 47.085, 47.118, 46.595, 46.845, 46.225,
46.66, 46.66, 46.66, 46.722, 46.64, 46.728, 46.683, 46.285,
46.285, 46.285, 46.224, 46.04, 46.055, 46.06, 46.049, 46.049,
46.049, 46.154, 45.903, 46.017, 46, 46.12, 46.12, 46.12,
46.07, 46.165, 46.153, 45, 45.95, 45.95, 45.95, 45.91, 46.02,
46.235, 46.325, 46.227, 46.227, 46.227, 46.227, 46.227, 46.16,
46, 46.168, 46.168, 46.168, 46.357, 46.475, 46.61, 46.439,
46.595, 46.595, 46.595, 46.653, 46.835, 46.285, 46.772, 46.861,
46.861, 46.861, 47.02, 47.225, 47.3, 47.525, 47.076, 47.076,
47.076, 47.62, 47.613, 47.575, 47.585, 47.552, 47.552, 47.552,
47.51, 47.45, 47.403, 47.435, 47.45, 47.45, 47.45, 47.375,
47.445, 47, 47.665, 47.665, 47.665, 47.665, 47.719, 47.825,
47.67, 47.623, 47.615, 47.615, 47.615, 47.717, 47.809, 47.869,
47.72, 47.8, 47.8, 47.8, 47.695, 47.67, 47.586, 47.635, 47.63,
47.63, 47.63, 47.55, 47.335, 47.2, 47.233, 46.92, 46.92,
46.92, 46.98, 46.82, 46.875, 47, 47.17, 47.17, 47.17), PHP.HIGH = c(46.535,
46.438, 46.438, 46.438, 46.438, 46.583, 46.645, 46.59, 46.702,
46.488, 46.488, 46.488, 46.41, 46.88, 46.88, 46.77, 46.865,
46.865, 46.865, 46.8, 46.865, 46.975, 47.02, 47.04, 47.04,
47.04, 47.075, 47.358, 47.487, 47.03, 47.135, 47.135, 47.135,
47.18, 47.228, 47.24, 47.228, 47.155, 47.155, 47.155, 47.23,
47.158, 46.96, 46.89, 47.055, 47.055, 47.055, 47.22, 47.435,
47.49, 47.32, 47.585, 47.585, 47.585, 47.112, 47.345, 47.345,
46.895, 47.065, 47.065, 47.065, 47.935, 47.98, 47.95, 47.183,
46.975, 46.975, 46.975, 46.587, 46.64, 46.388, 46.445, 46.475,
46.475, 46.475, 46.455, 46.479, 47.065, 46.222, 46.155, 46.155,
46.155, 46.388, 46.12, 46.265, 46.45, 46.982, 46.982, 46.982,
46.615, 46.79, 47.065, 46.825, 46.71, 46.71, 46.71, 46.788,
46.889, 46.925, 46.86, 46.86, 46.86, 46.86, 46.947, 46.705,
46.457, 46.655, 46.8, 46.8, 46.8, 46.715, 46.75, 47.205,
47.385, 47.385, 47.385, 47.385, 47.245, 47.132, 47.75, 47.041,
46.964, 46.964, 46.964, 46.878, 46.842, 46.94, 46.874, 46.68,
46.68, 46.68, 46.46, 46.225, 46.16, 46.2, 46.25, 46.25, 46.25,
46.306, 46.487, 46.14, 46.18, 46.266, 46.266, 46.266, 46.17,
46.338, 46.295, 46.105, 47.02, 47.02, 47.02, 46.085, 46.262,
46.439, 46.537, 46.463, 46.463, 46.463, 46.463, 46.463, 46.39,
46.577, 46.379, 46.379, 46.379, 46.58, 46.925, 46.83, 46.607,
46.795, 46.795, 46.795, 46.855, 47.025, 47.035, 46.925, 47.125,
47.125, 47.125, 47.252, 47.307, 47.5, 47.605, 47.603, 47.603,
47.603, 47.62, 47.717, 47.66, 47.705, 47.905, 47.905, 47.905,
47.595, 47.695, 47.59, 47.555, 47.74, 47.74, 47.74, 47.488,
47.632, 47.795, 47.757, 47.757, 47.757, 47.757, 47.8, 47.925,
47.79, 47.74, 47.775, 47.775, 47.775, 47.945, 47.995, 48.005,
47.9, 47.911, 47.911, 47.911, 47.958, 47.895, 47.755, 47.885,
47.78, 47.78, 47.78, 47.748, 47.55, 47.46, 47.335, 47.173,
47.173, 47.173, 47.14, 47.062, 47.188, 47.158, 47.17, 47.17,
47.17), PHP.LOW = c(46.385, 46.283, 46.283, 46.283, 46.283,
46.33, 46.45, 46.45, 46.5, 46.29, 46.29, 46.29, 46.235, 46.28,
46.225, 46.445, 46.455, 46.455, 46.455, 46.365, 46.73, 46.85,
46.423, 46.05, 46.05, 46.05, 46.975, 46.03, 46.035, 46.915,
47.017, 47.017, 47.017, 47.05, 46.825, 47.105, 47.12, 47,
47, 47, 47.055, 46.015, 46.725, 46.675, 46.671, 46.671, 46.671,
47.019, 47, 47.055, 47.033, 47, 47, 47, 46.85, 46.765, 46.765,
46.753, 46.465, 46.465, 46.465, 46.035, 46.49, 45.93, 46.955,
46.51, 46.51, 46.51, 46.475, 46.365, 46.31, 46.3, 44.623,
44.623, 44.623, 46.285, 46.31, 46.073, 46.07, 46.06, 46.06,
46.06, 45.859, 46, 45.728, 46.2, 46.452, 46.452, 46.452,
46.53, 46.64, 46.653, 46.69, 46.614, 46.614, 46.614, 46.429,
46.63, 46.788, 46.69, 46.714, 46.714, 46.714, 46.705, 46.465,
46.325, 46.055, 46.55, 46.55, 46.55, 46.458, 46.53, 46.753,
47.085, 47.085, 47.085, 47.085, 47.118, 46.595, 46.845, 46.225,
46.66, 46.66, 46.66, 46.722, 46.64, 46.728, 46.683, 46.285,
46.285, 46.285, 46.224, 46.04, 46.055, 46.06, 46.049, 46.049,
46.049, 46.154, 45.903, 46.017, 46, 46.12, 46.12, 46.12,
46.07, 46.165, 46.153, 45, 45.95, 45.95, 45.95, 45.91, 46.02,
46.235, 46.325, 46.227, 46.227, 46.227, 46.227, 46.227, 46.16,
46, 46.168, 46.168, 46.168, 46.357, 46.475, 46.61, 46.439,
46.595, 46.595, 46.595, 46.653, 46.835, 46.285, 46.772, 46.861,
46.861, 46.861, 47.02, 47.225, 47.3, 47.525, 47.076, 47.076,
47.076, 47.62, 47.613, 47.575, 47.585, 47.552, 47.552, 47.552,
47.51, 47.45, 47.403, 47.435, 47.45, 47.45, 47.45, 47.375,
47.445, 47, 47.665, 47.665, 47.665, 47.665, 47.719, 47.825,
47.67, 47.623, 47.615, 47.615, 47.615, 47.717, 47.809, 47.869,
47.72, 47.8, 47.8, 47.8, 47.695, 47.67, 47.586, 47.635, 47.63,
47.63, 47.63, 47.55, 47.335, 47.2, 47.233, 46.92, 46.92,
46.92, 46.98, 46.82, 46.875, 47, 47.17, 47.17, 47.17), PHP.CLOSE = c(46.46,
46.36, 46.36, 46.36, 46.36, 46.345, 46.535, 46.45, 46.58,
46.45, 46.45, 46.45, 46.28, 46.41, 46.88, 46.455, 46.6, 46.6,
46.6, 46.79, 46.75, 46.85, 46.835, 46.9, 46.9, 46.9, 47.02,
47.035, 46.961, 46.94, 47.11, 47.11, 47.11, 47.07, 47.18,
47.14, 47.15, 47.12, 47.12, 47.12, 47.077, 46.725, 46.94,
46.714, 46.85, 46.85, 46.85, 47.05, 47.21, 47.321, 47.31,
47.125, 47.125, 47.125, 47, 47.03, 47.03, 46.775, 46.915,
46.915, 46.915, 47.085, 46.965, 46.895, 47.03, 46.95, 46.95,
46.95, 46.51, 46.475, 46.38, 46.335, 46.425, 46.425, 46.425,
46.43, 46.43, 46.31, 46.08, 46.13, 46.13, 46.13, 46.055,
46.01, 46.11, 46.225, 46.465, 46.465, 46.465, 46.53, 46.64,
46.75, 46.75, 46.635, 46.635, 46.635, 46.69, 46.785, 46.92,
46.788, 46.75, 46.75, 46.75, 46.8, 46.66, 46.42, 46.46, 46.555,
46.555, 46.555, 46.647, 46.555, 46.757, 47.09, 47.09, 47.09,
47.09, 47.24, 47.08, 46.89, 46.965, 46.89, 46.89, 46.89,
46.77, 46.78, 46.76, 46.85, 46.645, 46.645, 46.645, 46.44,
46.225, 46.093, 46.185, 46.065, 46.065, 46.065, 46.25, 46.085,
46.018, 46.122, 46.135, 46.135, 46.135, 46.1, 46.2, 46.26,
46.08, 46.015, 46.015, 46.015, 46.07, 46.028, 46.35, 46.34,
46.395, 46.395, 46.395, 46.395, 46.395, 46.285, 46.33, 46.36,
46.36, 46.36, 46.38, 46.785, 46.83, 46.595, 46.62, 46.62,
46.62, 46.695, 46.855, 46.958, 46.92, 46.946, 46.946, 46.946,
47.125, 47.298, 47.35, 47.558, 47.515, 47.515, 47.515, 47.62,
47.625, 47.635, 47.619, 47.665, 47.665, 47.665, 47.55, 47.45,
47.573, 47.45, 47.74, 47.74, 47.74, 47.45, 47.47, 47.671,
47.665, 47.665, 47.665, 47.665, 47.775, 47.86, 47.685, 47.69,
47.65, 47.65, 47.65, 47.743, 47.815, 47.997, 47.89, 47.805,
47.805, 47.805, 47.935, 47.89, 47.67, 47.706, 47.745, 47.745,
47.745, 47.64, 47.542, 47.45, 47.269, 47.158, 47.158, 47.158,
47.01, 47.025, 46.89, 47.125, 47.17, 47.17, 47.17), PHP.LAST = c(46.46,
46.36, 46.36, 46.36, 46.36, 46.345, 46.535, 46.45, 46.58,
46.45, 46.45, 46.45, 46.28, 46.41, 46.88, 46.455, 46.6, 46.6,
46.6, 46.79, 46.75, 46.85, 46.835, 46.9, 46.9, 46.9, 47.02,
47.035, 46.961, 46.94, 47.11, 47.11, 47.11, 47.07, 47.18,
47.14, 47.15, 47.12, 47.12, 47.12, 47.077, 46.725, 46.94,
46.714, 46.85, 46.85, 46.85, 47.05, 47.21, 47.321, 47.31,
47.125, 47.125, 47.125, 47, 47.03, 47.03, 46.775, 46.915,
46.915, 46.915, 47.085, 46.965, 46.895, 47.03, 46.95, 46.95,
46.95, 46.51, 46.475, 46.38, 46.335, 46.425, 46.425, 46.425,
46.43, 46.43, 46.31, 46.08, 46.13, 46.13, 46.13, 46.055,
46.01, 46.11, 46.225, 46.465, 46.465, 46.465, 46.53, 46.64,
46.75, 46.75, 46.635, 46.635, 46.635, 46.69, 46.785, 46.92,
46.788, 46.75, 46.75, 46.75, 46.8, 46.66, 46.42, 46.46, 46.555,
46.555, 46.555, 46.647, 46.555, 46.757, 47.09, 47.09, 47.09,
47.09, 47.24, 47.08, 46.89, 46.965, 46.89, 46.89, 46.89,
46.77, 46.78, 46.76, 46.85, 46.645, 46.645, 46.645, 46.44,
46.225, 46.093, 46.185, 46.065, 46.065, 46.065, 46.25, 46.085,
46.018, 46.122, 46.135, 46.135, 46.135, 46.1, 46.2, 46.26,
46.08, 46.015, 46.015, 46.015, 46.07, 46.028, 46.35, 46.34,
46.395, 46.395, 46.395, 46.395, 46.395, 46.285, 46.33, 46.36,
46.36, 46.36, 46.38, 46.785, 46.83, 46.595, 46.62, 46.62,
46.62, 46.695, 46.855, 46.958, 46.92, 46.946, 46.946, 46.946,
47.125, 47.298, 47.35, 47.558, 47.515, 47.515, 47.515, 47.62,
47.625, 47.635, 47.619, 47.665, 47.665, 47.665, 47.55, 47.45,
47.573, 47.45, 47.74, 47.74, 47.74, 47.45, 47.47, 47.671,
47.665, 47.665, 47.665, 47.665, 47.775, 47.86, 47.685, 47.69,
47.65, 47.65, 47.65, 47.743, 47.815, 47.997, 47.89, 47.805,
47.805, 47.805, 47.935, 47.89, 47.67, 47.706, 47.745, 47.745,
47.745, 47.64, 47.542, 47.45, 47.269, 47.158, 47.158, 47.158,
47.01, 47.025, 46.89, 47.125, 47.17, 47.17, 47.17)), .Names = c("Date",
"PHP.OPEN", "PHP.HIGH", "PHP.LOW", "PHP.CLOSE", "PHP.LAST"
), row.names = c(NA, -243L), class = "data.frame"), structure(list(
Date = structure(c(1472533200, 1472446800, 1472360400,
1472274000, 1472187600, 1472101200, 1472014800, 1471928400,
1471842000, 1471755600, 1471669200, 1471582800, 1471496400,
1471410000, 1471323600, 1471237200, 1471150800, 1471064400,
1470978000, 1470891600, 1470805200, 1470718800, 1470632400,
1470546000, 1470459600, 1470373200, 1470286800, 1470200400,
1470114000, 1470027600, 1469941200, 1469854800, 1469768400,
1469682000, 1469595600, 1469509200, 1469422800, 1469336400,
1469250000, 1469163600, 1469077200, 1468990800, 1468904400,
1468818000, 1468731600, 1468645200, 1468558800, 1468472400,
1468386000, 1468299600, 1468213200, 1468126800, 1468040400,
1467954000, 1467867600, 1467781200, 1467694800, 1467608400,
1467522000, 1467435600, 1467349200, 1467262800, 1467176400,
1467090000, 1467003600, 1466917200, 1466830800, 1466744400,
1466658000, 1466571600, 1466485200, 1466398800, 1466312400,
1466226000, 1466139600, 1466053200, 1465966800, 1465880400,
1465794000, 1465707600, 1465621200, 1465534800, 1465448400,
1465362000, 1465275600, 1465189200, 1465102800, 1465016400,
1464930000, 1464843600, 1464757200, 1464670800, 1464584400,
1464498000, 1464411600, 1464325200, 1464238800, 1464152400,
1464066000, 1463979600, 1463893200, 1463806800, 1463720400,
1463634000, 1463547600, 1463461200, 1463374800, 1463288400,
1463202000, 1463115600, 1463029200, 1462942800, 1462856400,
1462770000, 1462683600, 1462597200, 1462510800, 1462424400,
1462338000, 1462251600, 1462165200, 1462078800, 1461992400,
1461906000, 1461819600, 1461733200, 1461646800, 1461560400,
1461474000, 1461387600, 1461301200, 1461214800, 1461128400,
1461042000, 1460955600, 1460869200, 1460782800, 1460696400,
1460610000, 1460523600, 1460437200, 1460350800, 1460264400,
1460178000, 1460091600, 1460005200, 1459918800, 1459832400,
1459746000, 1459659600, 1459573200, 1459486800, 1459400400,
1459314000, 1459227600, 1459141200, 1459054800, 1458968400,
1458882000, 1458795600, 1458709200, 1458622800, 1458536400,
1458450000, 1458363600, 1458277200, 1458190800, 1458104400,
1458018000, 1457931600, 1457848800, 1457762400, 1457676000,
1457589600, 1457503200, 1457416800, 1457330400, 1457244000,
1457157600, 1457071200, 1456984800, 1456898400, 1456812000,
1456725600, 1456639200, 1456552800, 1456466400, 1456380000,
1456293600, 1456207200, 1456120800, 1456034400, 1455948000,
1455861600, 1455775200, 1455688800, 1455602400, 1455516000,
1455429600, 1455343200, 1455256800, 1455170400, 1455084000,
1454997600, 1454911200, 1454824800, 1454738400, 1454652000,
1454565600, 1454479200, 1454392800, 1454306400, 1454220000,
1454133600, 1454047200, 1453960800, 1453874400, 1453788000,
1453701600, 1453615200, 1453528800, 1453442400, 1453356000,
1453269600, 1453183200, 1453096800, 1453010400, 1452924000,
1452837600, 1452751200, 1452664800, 1452578400, 1452492000,
1452405600, 1452319200, 1452232800, 1452146400, 1452060000,
1451973600, 1451887200, 1451800800, 1451714400, 1451628000
), tzone = "", class = c("POSIXct", "POSIXt")), THB.OPEN = c(34.53,
34.596, 34.506, 34.506, 34.506, 34.539, 34.573, 34.564,
34.607, 34.528, 34.528, 34.528, 34.531, 34.577, 34.52,
34.577, 34.696, 34.696, 34.696, 34.731, 34.738, 34.911,
34.901, 34.796, 34.796, 34.796, 34.886, 34.739, 34.74,
34.72, 34.8, 34.8, 34.8, 34.84, 34.951, 34.98, 34.905,
34.87, 34.87, 34.87, 34.95, 34.97, 34.931, 34.918, 34.907,
34.907, 34.907, 35.029, 35.13, 35.103, 35.049, 35.169,
35.169, 35.169, 35.145, 35.149, 35.012, 35.011, 35.06,
35.06, 35.06, 35.097, 35.175, 35.238, 35.257, 35.114,
35.114, 35.114, 35.146, 35.173, 35.15, 35.162, 35.225,
35.225, 35.225, 35.151, 35.24, 35.12, 35.16, 35.147,
35.147, 35.147, 35.085, 35.189, 35.217, 35.26, 35.545,
35.545, 35.545, 35.57, 35.64, 35.691, 35.69, 35.56, 35.56,
35.56, 35.571, 35.625, 35.664, 35.56, 35.622, 35.622,
35.622, 35.641, 35.486, 35.32, 35.341, 35.311, 35.311,
35.311, 35.217, 35.19, 35.193, 35.09, 35.126, 35.126,
35.126, 35.075, 34.99, 34.83, 34.83, 34.885, 34.885,
34.885, 35.007, 35.09, 35.08, 35.052, 35.02, 35.02, 35.02,
34.883, 34.825, 34.88, 35.01, 35.051, 35.051, 35.051,
35.024, 34.951, 35.005, 35.04, 35.09, 35.09, 35.09, 35.15,
35.214, 35.237, 35.12, 35.09, 35.09, 35.09, 35.159, 35.189,
35.323, 35.274, 35.265, 35.265, 35.265, 35.188, 34.917,
34.88, 34.85, 34.745, 34.745, 34.745, 34.77, 34.99, 35.07,
35.05, 35.064, 35.064, 35.064, 35.239, 35.287, 35.365,
35.349, 35.385, 35.385, 35.385, 35.476, 35.54, 35.588,
35.618, 35.63, 35.63, 35.63, 35.65, 35.69, 35.715, 35.717,
35.54, 35.54, 35.54, 35.537, 35.6, 35.59, 35.552, 35.228,
35.228, 35.228, 35.228, 35.3, 35.388, 35.495, 35.48,
35.48, 35.48, 35.55, 35.77, 35.579, 35.591, 35.684, 35.684,
35.684, 35.775, 35.77, 35.89, 35.925, 36.026, 36.026,
36.026, 36.235, 36.272, 36.26, 36.29, 36.28, 36.28, 36.28,
36.256, 36.237, 36.235, 36.25, 36.17, 36.17, 36.17, 36.226,
36.103, 36.08, 36.033, 36.02, 36.02, 36.02), THB.HIGH = c(34.655,
34.72, 34.601, 34.601, 34.601, 34.675, 34.69, 34.676,
34.764, 34.671, 34.671, 34.671, 34.69, 34.737, 34.65,
34.82, 34.86, 34.86, 34.86, 34.838, 34.967, 35.005, 35.065,
34.93, 34.93, 34.93, 35.039, 34.95, 34.84, 34.91, 34.92,
34.92, 34.92, 34.967, 35.074, 35.086, 35.08, 35, 35,
35, 35.088, 35.09, 35.04, 35.03, 35.085, 35.085, 35.085,
35.23, 35.257, 35.206, 35.225, 35.264, 35.264, 35.264,
35.239, 35.281, 35.198, 35.125, 35.16, 35.16, 35.16,
35.228, 35.295, 35.371, 35.435, 35.546, 35.546, 35.546,
35.244, 35.29, 35.24, 35.284, 35.356, 35.356, 35.356,
35.254, 35.39, 35.303, 35.295, 35.295, 35.295, 35.295,
35.24, 35.241, 35.327, 35.411, 35.64, 35.64, 35.64, 35.704,
35.77, 35.755, 35.8, 35.701, 35.701, 35.701, 35.68, 35.795,
35.787, 35.706, 35.736, 35.736, 35.736, 35.8, 35.625,
35.557, 35.461, 35.466, 35.466, 35.466, 35.34, 35.294,
35.267, 35.166, 35.193, 35.193, 35.193, 35.19, 35.184,
34.94, 34.934, 35.009, 35.009, 35.009, 35.16, 35.195,
35.222, 35.17, 35.095, 35.095, 35.095, 35.095, 34.949,
35.02, 35.11, 35.12, 35.12, 35.12, 35.211, 35.055, 35.07,
35.112, 35.234, 35.234, 35.234, 35.253, 35.31, 35.39,
35.29, 35.175, 35.175, 35.175, 35.262, 35.365, 35.44,
35.408, 35.365, 35.365, 35.365, 35.395, 35.13, 34.963,
34.952, 34.925, 34.925, 34.925, 34.975, 35.145, 35.15,
35.151, 35.26, 35.26, 35.26, 35.31, 35.393, 35.47, 35.495,
35.46, 35.46, 35.46, 35.559, 35.656, 35.655, 35.763,
35.707, 35.707, 35.707, 35.714, 35.759, 35.776, 35.781,
35.75, 35.75, 35.75, 35.63, 35.68, 35.671, 35.69, 35.64,
35.64, 35.64, 35.325, 35.428, 35.539, 35.612, 35.651,
35.651, 35.651, 35.755, 35.87, 35.771, 35.77, 35.861,
35.861, 35.861, 35.885, 35.874, 36.045, 36.055, 36.253,
36.253, 36.253, 36.29, 36.327, 36.325, 36.382, 36.374,
36.374, 36.374, 36.345, 36.338, 36.375, 36.345, 36.327,
36.327, 36.327, 36.35, 36.257, 36.177, 36.19, 36.033,
36.033, 36.033), THB.LOW = c(34.53, 34.596, 34.506, 34.506,
34.506, 34.539, 34.573, 34.564, 34.607, 34.528, 34.528,
34.528, 34.531, 34.577, 34.52, 34.577, 34.696, 34.696,
34.696, 34.731, 34.738, 34.911, 34.901, 34.796, 34.796,
34.796, 34.886, 34.739, 34.74, 34.72, 34.8, 34.8, 34.8,
34.84, 34.951, 34.98, 34.905, 34.87, 34.87, 34.87, 34.95,
34.97, 34.931, 34.918, 34.907, 34.907, 34.907, 35.029,
35.13, 35.103, 35.049, 35.169, 35.169, 35.169, 35.145,
35.149, 35.012, 35.011, 35.06, 35.06, 35.06, 35.097,
35.175, 35.238, 35.257, 35.114, 35.114, 35.114, 35.146,
35.173, 35.15, 35.162, 35.225, 35.225, 35.225, 35.151,
35.24, 35.12, 35.16, 35.147, 35.147, 35.147, 35.085,
35.189, 35.217, 35.26, 35.545, 35.545, 35.545, 35.57,
35.64, 35.691, 35.69, 35.56, 35.56, 35.56, 35.571, 35.625,
35.664, 35.56, 35.622, 35.622, 35.622, 35.641, 35.486,
35.32, 35.341, 35.311, 35.311, 35.311, 35.217, 35.19,
35.193, 35.09, 35.126, 35.126, 35.126, 35.075, 34.99,
34.83, 34.83, 34.885, 34.885, 34.885, 35.007, 35.09,
35.08, 35.052, 35.02, 35.02, 35.02, 34.883, 34.825, 34.88,
35.01, 35.051, 35.051, 35.051, 35.024, 34.951, 35.005,
35.04, 35.09, 35.09, 35.09, 35.15, 35.214, 35.237, 35.12,
35.09, 35.09, 35.09, 35.159, 35.189, 35.323, 35.274,
35.265, 35.265, 35.265, 35.188, 34.917, 34.88, 34.85,
34.745, 34.745, 34.745, 34.77, 34.99, 35.07, 35.05, 35.064,
35.064, 35.064, 35.239, 35.287, 35.365, 35.349, 35.385,
35.385, 35.385, 35.476, 35.54, 35.588, 35.618, 35.63,
35.63, 35.63, 35.65, 35.69, 35.715, 35.717, 35.54, 35.54,
35.54, 35.537, 35.6, 35.59, 35.552, 35.228, 35.228, 35.228,
35.228, 35.3, 35.388, 35.495, 35.48, 35.48, 35.48, 35.55,
35.77, 35.579, 35.591, 35.684, 35.684, 35.684, 35.775,
35.77, 35.89, 35.925, 36.026, 36.026, 36.026, 36.235,
36.272, 36.26, 36.29, 36.28, 36.28, 36.28, 36.256, 36.237,
36.235, 36.25, 36.17, 36.17, 36.17, 36.226, 36.103, 36.08,
36.033, 36.02, 36.02, 36.02), THB.CLOSE = c(34.558, 34.601,
34.565, 34.565, 34.565, 34.565, 34.618, 34.64, 34.67,
34.635, 34.635, 34.635, 34.605, 34.7, 34.575, 34.61,
34.81, 34.81, 34.81, 34.76, 34.751, 34.965, 34.974, 34.822,
34.822, 34.822, 34.927, 34.94, 34.75, 34.77, 34.83, 34.83,
34.83, 34.868, 34.97, 35.01, 35.05, 34.93, 34.93, 34.93,
34.995, 34.989, 35.01, 35.005, 34.94, 34.94, 34.94, 35.03,
35.16, 35.138, 35.11, 35.25, 35.25, 35.25, 35.173, 35.214,
35.134, 35.048, 35.086, 35.086, 35.086, 35.138, 35.23,
35.29, 35.35, 35.339, 35.339, 35.339, 35.173, 35.24,
35.173, 35.21, 35.235, 35.235, 35.235, 35.24, 35.25,
35.3, 35.18, 35.26, 35.26, 35.26, 35.21, 35.21, 35.23,
35.28, 35.629, 35.629, 35.629, 35.61, 35.67, 35.705,
35.75, 35.658, 35.658, 35.658, 35.6, 35.71, 35.73, 35.66,
35.679, 35.679, 35.679, 35.71, 35.6, 35.49, 35.37, 35.43,
35.43, 35.43, 35.32, 35.27, 35.236, 35.13, 35.149, 35.149,
35.149, 35.179, 35.184, 34.92, 34.841, 34.92, 34.92,
34.92, 35.02, 35.15, 35.16, 35.11, 35.04, 35.04, 35.04,
35.01, 34.86, 34.91, 35.03, 35.065, 35.065, 35.065, 35.12,
35.036, 35.02, 35.07, 35.12, 35.12, 35.12, 35.205, 35.256,
35.32, 35.263, 35.105, 35.105, 35.105, 35.185, 35.235,
35.426, 35.36, 35.27, 35.27, 35.27, 35.35, 35.118, 34.91,
34.89, 34.85, 34.85, 34.85, 34.78, 35.02, 35.132, 35.08,
35.16, 35.16, 35.16, 35.25, 35.32, 35.425, 35.42, 35.403,
35.403, 35.403, 35.51, 35.57, 35.625, 35.64, 35.64, 35.64,
35.64, 35.68, 35.7, 35.74, 35.755, 35.745, 35.745, 35.745,
35.548, 35.61, 35.61, 35.65, 35.58, 35.58, 35.58, 35.245,
35.36, 35.489, 35.51, 35.497, 35.497, 35.497, 35.56,
35.841, 35.76, 35.605, 35.73, 35.73, 35.73, 35.825, 35.79,
35.908, 36.021, 36.037, 36.037, 36.037, 36.263, 36.3,
36.27, 36.32, 36.366, 36.366, 36.366, 36.313, 36.25,
36.33, 36.28, 36.307, 36.307, 36.307, 36.285, 36.22,
36.15, 36.14, 36.02, 36.02, 36.02), THB.LAST = c(34.558,
34.601, 34.565, 34.565, 34.565, 34.565, 34.618, 34.64,
34.67, 34.635, 34.635, 34.635, 34.605, 34.7, 34.575,
34.61, 34.81, 34.81, 34.81, 34.76, 34.751, 34.965, 34.974,
34.822, 34.822, 34.822, 34.927, 34.94, 34.75, 34.77,
34.83, 34.83, 34.83, 34.868, 34.97, 35.01, 35.05, 34.93,
34.93, 34.93, 34.995, 34.989, 35.01, 35.005, 34.94, 34.94,
34.94, 35.03, 35.16, 35.138, 35.11, 35.25, 35.25, 35.25,
35.173, 35.214, 35.134, 35.048, 35.086, 35.086, 35.086,
35.138, 35.23, 35.29, 35.35, 35.339, 35.339, 35.339,
35.173, 35.24, 35.173, 35.21, 35.235, 35.235, 35.235,
35.24, 35.25, 35.3, 35.18, 35.26, 35.26, 35.26, 35.21,
35.21, 35.23, 35.28, 35.629, 35.629, 35.629, 35.61, 35.67,
35.705, 35.75, 35.658, 35.658, 35.658, 35.6, 35.71, 35.73,
35.66, 35.679, 35.679, 35.679, 35.71, 35.6, 35.49, 35.37,
35.43, 35.43, 35.43, 35.32, 35.27, 35.236, 35.13, 35.149,
35.149, 35.149, 35.179, 35.184, 34.92, 34.841, 34.92,
34.92, 34.92, 35.02, 35.15, 35.16, 35.11, 35.04, 35.04,
35.04, 35.01, 34.86, 34.91, 35.03, 35.065, 35.065, 35.065,
35.12, 35.036, 35.02, 35.07, 35.12, 35.12, 35.12, 35.205,
35.256, 35.32, 35.263, 35.105, 35.105, 35.105, 35.185,
35.235, 35.426, 35.36, 35.27, 35.27, 35.27, 35.35, 35.118,
34.91, 34.89, 34.85, 34.85, 34.85, 34.78, 35.02, 35.132,
35.08, 35.16, 35.16, 35.16, 35.25, 35.32, 35.425, 35.42,
35.403, 35.403, 35.403, 35.51, 35.57, 35.625, 35.64,
35.64, 35.64, 35.64, 35.68, 35.7, 35.74, 35.755, 35.745,
35.745, 35.745, 35.548, 35.61, 35.61, 35.65, 35.58, 35.58,
35.58, 35.245, 35.36, 35.489, 35.51, 35.497, 35.497,
35.497, 35.56, 35.841, 35.76, 35.605, 35.73, 35.73, 35.73,
35.825, 35.79, 35.908, 36.021, 36.037, 36.037, 36.037,
36.263, 36.3, 36.27, 36.32, 36.366, 36.366, 36.366, 36.313,
36.25, 36.33, 36.28, 36.307, 36.307, 36.307, 36.285,
36.22, 36.15, 36.14, 36.02, 36.02, 36.02)), .Names = c("Date",
"THB.OPEN", "THB.HIGH", "THB.LOW", "THB.CLOSE", "THB.LAST"
), row.names = c(NA, -243L), class = "data.frame"))
And the two functions listed within the original function is here:
price.channel <- function(MergedData)
{
ret <- as.list(rep(NA, length(MergedData)))
for (i in 1:length(MergedData)) {
x<- MergedData[[i]]
x <- xts(x[,-1], order.by=x[,1])
high <- runMax(x[,2], 20)
low <- runMin(x[,3], 20)
mid <- (high+low)/2
channel <- cbind(high, mid, low)
sig <- ifelse(sign(high-x[,5])>=0, sign(high-x[,5]), sign(low-x[,5]))
ret[[i]] <- sig
}
na.omit(do.call(merge, ret))
}
ma.crossover<- function(MergedData){
ma <- as.list(rep(NA, length(MergedData)))
for(i in 1:length(MergedData)) {
x <- MergedData[[i]]
x <- xts(x[,-1], order.by=x[,1])
ma20 <- SMA(x[,3], n=20)
ma50 <- SMA(x[,3], n=50)
ma[[i]] <- sign(ma20 - ma50)
}
na.omit(do.call(merge, ma))
}
And I want to create a table from new.trend with the following:
out.trend <- matrix(new.trend(MergedData), ncol = 3, byrow = TRUE, dimnames = list(paste0(cursig), c("% dev from 20MA", "% dev from 20MA", "RSI")))
I think manotheshark's answer is right about not needing a for loop and using intersect(), you could do it like this without relying on names():
The dummy data:
ma.sig <- read.table(text = "Eur JPY GBP CHF AUD CAD NZD SEK NOK
1 0 1 0 1 1 0 0 0", header = T)
pricebreak <- read.table(text = "Eur JPY GBP CHF AUD CAD NZD SEK NOK
1 0 1 0 1 1 0 1 1", , header = T)
cur <- c("EUR", "JPY","GBP", "CHF", "AUD", "CAD", "NZD", "SEK", "NOK", "CZK", "HUF", "ILS")
Manotheshark's method, without names():
sig <- intersect(which(!ma.sig[1,]==0), which(!pricebreak[1,]==0)) ## Retrieves signals
sig
[1] 1 3 5 6
cur[sig]
"EUR" "GBP" "AUD" "CAD"
Or your idea for a for loop with my comment:
cursig <- character()
for (i in sig) { #not 1:length(sig) thats always 1,2,3... in order
cursig <- c(cursig, cur[i])
}
cursig
[1] "EUR" "GBP" "AUD" "CAD"
If I understand what you're trying to get, a for loop should not be required. You can index the vectors on the conditions you're looking for
names(ma.sig[ma.sig == 1])
names(price.break[price.break == 1])
This will return the names that meet both conditions
intersect(names(ma.sig[ma.sig == 1]), names(price.break[price.break == 1]))
[1] "Eur" "GBP" "AUD" "CAD"
My dataset is a list of 29 dataframes, such that one data frame looks like this:
>MergedData[[1]]
Date EUR.HIGH EUR.LOW EUR.CLOSE
1 2016-08-30 1.1192 1.1153 1.1161
2 2016-08-29 1.1208 1.1158 1.1189
3 2016-08-28 1.1341 1.1181 1.1198
4 2016-08-27 1.1341 1.1181 1.1198
5 2016-08-26 1.1341 1.1181 1.1198
6 2016-08-25 1.1298 1.1259 1.1285
7 2016-08-24 1.1312 1.1245 1.1264
So using this list, I calculated 'signals' in R with values of -2,1 and 0. I want R to do calculations and leave them in an output for variables that have values that aren't zero. Here's what my signal looks like in R:
>price.break
EUR JPY GBP CHF AUD CAD NZD SEK
2016-02-19 NA NA NA NA NA NA NA NA
2016-02-20 0 -2 0 0 -2 0 0 0
2016-02-21 0 0 0 0 0 0 0 0
>ma.sig
EUR JPY GBP CHF AUD CAD NZD SEK
2016-02-19 NA NA NA NA NA NA NA NA
2016-02-20 0 1 0 0 1 0 0 0
2016-02-21 0 0 0 0 0 0 0 0
Since we have that both JPY and AUD have returned non-zero signals, I want R to calculate an output table that looks like this:
Currency Deviation from MA RSI
JPY
AUD
Here is my (rather terrible) attempt at trying to have R do this:
new.trend <- function(x)
{
ret <- as.list(rep(NA, length(x)))
for (i in 1:length(MergedData)){
x <- MergedData[[i]]
x <- xts(x[,-1], order.by=x[,1])
ma.sig <- ma.crossover(x)
pricebreak <- price.channel(x)
sig.col <- col(x)[which(!ma.sig[,i]&ma.sig[,i-1)]==0)&which(!pricebreak[,i]&pricebreak[,i-1]==0)]
sig.row <- row(x)[which(!ma.sig[,i]&ma.sig[,i-1)]==0)&which(!pricebreak[,i]&pricebreak[,i-1]==0)]
ma20 <- SMA(x[sig.row, sig.col], n=20)
ma50 <- SMA(x[sig.row, sig.col], n=50)
RSI <- RSI(x[sig.row, sig.col], n=14)
dev20 <- x[sig.row, sig.col]-ma20[sig.row, sig.col]/x[sig.row, sig.col]*100
dev50 <- x[sig.row, sig.col]-ma50[sig.row, sig.col]/x[sig.row, sig.col]*100
ret[[i]] <- merge(RSI, 20dev, 50dev)
}
na.omit(do.call(merge,ret))
}
With sig.col and sig.row, I'm hoping to extract the exact row/column number in order to selectively calculate the instances where we get nonnegative signals. I get the error saying 'incorrect number dimensions', and I have a strong feeling what I'm doing isn't correct anyway.
ma.crossover and pricebreak are just functions that calculate the following:
ma.crossover<- function(MergedData){
ma <- as.list(rep(NA, length(MergedData)))
for(i in 1:length(MergedData)) {
x <- MergedData[[i]]
x <- xts(x[,-1], order.by=x[,1])
ma20 <- SMA(x[,3], n=20)
ma50 <- SMA(x[,3], n=50)
ma[[i]] <- sign(ma20 - ma50)
}
na.omit(do.call(merge, ma))
}
##price channel
price.channel <- function(MergedData)
{
ret <- as.list(rep(NA, length(MergedData)))
for (i in 1:length(MergedData)) {
x<- MergedData[[i]]
x <- xts(x[,-1], order.by=x[,1])
high <- runMax(x[,2], 20)
low <- runMin(x[,3], 20)
mid <- (high+low)/2
channel <- cbind(high, mid, low)
ret[[i]] <- merge(sign(high-x[,5]), sign(x[,5]-low))
}
na.omit(do.call(merge, ret))
}
I've been stuck on this for some time, so comments or feedback would be very much appreciated. Here's a sample of my dput, if that helps:
tzone = "", class = c("POSIXct",
"POSIXt")), PHP.OPEN = c(46.385, 46.283, 46.283, 46.283,
46.283, 46.33, 46.45, 46.45, 46.5, 46.29, 46.29, 46.29, 46.235,
46.28, 46.225, 46.445, 46.455, 46.455, 46.455, 46.365, 46.73,
46.85, 46.423, 46.05, 46.05, 46.05, 46.975, 46.03, 46.035,
46.915, 47.017, 47.017, 47.017, 47.05, 46.825, 47.105, 47.12,
47, 47, 47, 47.055, 46.015, 46.725, 46.675, 46.671, 46.671,
46.671, 47.019, 47, 47.055, 47.033, 47, 47, 47, 46.85, 46.765,
46.765, 46.753, 46.465, 46.465, 46.465, 46.035, 46.49, 45.93,
46.955, 46.51, 46.51, 46.51, 46.475, 46.365, 46.31, 46.3,
44.623, 44.623, 44.623, 46.285, 46.31, 46.073, 46.07, 46.06,
46.06, 46.06, 45.859, 46, 45.728, 46.2, 46.452, 46.452, 46.452,
46.53, 46.64, 46.653, 46.69, 46.614, 46.614, 46.614, 46.429,
46.63, 46.788, 46.69, 46.714, 46.714, 46.714, 46.705, 46.465,
46.325, 46.055, 46.55, 46.55, 46.55, 46.458, 46.53, 46.753,
47.085, 47.085, 47.085, 47.085, 47.118, 46.595, 46.845, 46.225,
46.66, 46.66, 46.66, 46.722, 46.64, 46.728, 46.683, 46.285,
46.285, 46.285, 46.224, 46.04, 46.055, 46.06, 46.049, 46.049,
46.049, 46.154, 45.903, 46.017, 46, 46.12, 46.12, 46.12,
46.07, 46.165, 46.153, 45, 45.95, 45.95, 45.95, 45.91, 46.02,
46.235, 46.325, 46.227, 46.227, 46.227, 46.227, 46.227, 46.16,
46, 46.168, 46.168, 46.168, 46.357, 46.475, 46.61, 46.439,
46.595, 46.595, 46.595, 46.653, 46.835, 46.285, 46.772, 46.861,
46.861, 46.861, 47.02, 47.225, 47.3, 47.525, 47.076, 47.076,
47.076, 47.62, 47.613, 47.575, 47.585, 47.552, 47.552, 47.552,
47.51, 47.45, 47.403, 47.435, 47.45, 47.45, 47.45, 47.375,
47.445, 47, 47.665, 47.665, 47.665, 47.665, 47.719, 47.825,
47.67, 47.623, 47.615, 47.615, 47.615, 47.717, 47.809, 47.869,
47.72, 47.8, 47.8, 47.8, 47.695, 47.67, 47.586, 47.635, 47.63,
47.63, 47.63, 47.55, 47.335, 47.2, 47.233, 46.92, 46.92,
46.92, 46.98, 46.82, 46.875, 47, 47.17, 47.17, 47.17), PHP.HIGH = c(46.535,
46.438, 46.438, 46.438, 46.438, 46.583, 46.645, 46.59, 46.702,
46.488, 46.488, 46.488, 46.41, 46.88, 46.88, 46.77, 46.865,
46.865, 46.865, 46.8, 46.865, 46.975, 47.02, 47.04, 47.04,
47.04, 47.075, 47.358, 47.487, 47.03, 47.135, 47.135, 47.135,
47.18, 47.228, 47.24, 47.228, 47.155, 47.155, 47.155, 47.23,
47.158, 46.96, 46.89, 47.055, 47.055, 47.055, 47.22, 47.435,
47.49, 47.32, 47.585, 47.585, 47.585, 47.112, 47.345, 47.345,
46.895, 47.065, 47.065, 47.065, 47.935, 47.98, 47.95, 47.183,
46.975, 46.975, 46.975, 46.587, 46.64, 46.388, 46.445, 46.475,
46.475, 46.475, 46.455, 46.479, 47.065, 46.222, 46.155, 46.155,
46.155, 46.388, 46.12, 46.265, 46.45, 46.982, 46.982, 46.982,
46.615, 46.79, 47.065, 46.825, 46.71, 46.71, 46.71, 46.788,
46.889, 46.925, 46.86, 46.86, 46.86, 46.86, 46.947, 46.705,
46.457, 46.655, 46.8, 46.8, 46.8, 46.715, 46.75, 47.205,
47.385, 47.385, 47.385, 47.385, 47.245, 47.132, 47.75, 47.041,
46.964, 46.964, 46.964, 46.878, 46.842, 46.94, 46.874, 46.68,
46.68, 46.68, 46.46, 46.225, 46.16, 46.2, 46.25, 46.25, 46.25,
46.306, 46.487, 46.14, 46.18, 46.266, 46.266, 46.266, 46.17,
46.338, 46.295, 46.105, 47.02, 47.02, 47.02, 46.085, 46.262,
46.439, 46.537, 46.463, 46.463, 46.463, 46.463, 46.463, 46.39,
46.577, 46.379, 46.379, 46.379, 46.58, 46.925, 46.83, 46.607,
46.795, 46.795, 46.795, 46.855, 47.025, 47.035, 46.925, 47.125,
47.125, 47.125, 47.252, 47.307, 47.5, 47.605, 47.603, 47.603,
47.603, 47.62, 47.717, 47.66, 47.705, 47.905, 47.905, 47.905,
47.595, 47.695, 47.59, 47.555, 47.74, 47.74, 47.74, 47.488,
47.632, 47.795, 47.757, 47.757, 47.757, 47.757, 47.8, 47.925,
47.79, 47.74, 47.775, 47.775, 47.775, 47.945, 47.995, 48.005,
47.9, 47.911, 47.911, 47.911, 47.958, 47.895, 47.755, 47.885,
47.78, 47.78, 47.78, 47.748, 47.55, 47.46, 47.335, 47.173,
47.173, 47.173, 47.14, 47.062, 47.188, 47.158, 47.17, 47.17,
47.17), PHP.LOW = c(46.385, 46.283, 46.283, 46.283, 46.283,
46.33, 46.45, 46.45, 46.5, 46.29, 46.29, 46.29, 46.235, 46.28,
46.225, 46.445, 46.455, 46.455, 46.455, 46.365, 46.73, 46.85,
46.423, 46.05, 46.05, 46.05, 46.975, 46.03, 46.035, 46.915,
47.017, 47.017, 47.017, 47.05, 46.825, 47.105, 47.12, 47,
47, 47, 47.055, 46.015, 46.725, 46.675, 46.671, 46.671, 46.671,
47.019, 47, 47.055, 47.033, 47, 47, 47, 46.85, 46.765, 46.765,
46.753, 46.465, 46.465, 46.465, 46.035, 46.49, 45.93, 46.955,
46.51, 46.51, 46.51, 46.475, 46.365, 46.31, 46.3, 44.623,
44.623, 44.623, 46.285, 46.31, 46.073, 46.07, 46.06, 46.06,
46.06, 45.859, 46, 45.728, 46.2, 46.452, 46.452, 46.452,
46.53, 46.64, 46.653, 46.69, 46.614, 46.614, 46.614, 46.429,
46.63, 46.788, 46.69, 46.714, 46.714, 46.714, 46.705, 46.465,
46.325, 46.055, 46.55, 46.55, 46.55, 46.458, 46.53, 46.753,
47.085, 47.085, 47.085, 47.085, 47.118, 46.595, 46.845, 46.225,
46.66, 46.66, 46.66, 46.722, 46.64, 46.728, 46.683, 46.285,
46.285, 46.285, 46.224, 46.04, 46.055, 46.06, 46.049, 46.049,
46.049, 46.154, 45.903, 46.017, 46, 46.12, 46.12, 46.12,
46.07, 46.165, 46.153, 45, 45.95, 45.95, 45.95, 45.91, 46.02,
46.235, 46.325, 46.227, 46.227, 46.227, 46.227, 46.227, 46.16,
46, 46.168, 46.168, 46.168, 46.357, 46.475, 46.61, 46.439,
46.595, 46.595, 46.595, 46.653, 46.835, 46.285, 46.772, 46.861,
46.861, 46.861, 47.02, 47.225, 47.3, 47.525, 47.076, 47.076,
47.076, 47.62, 47.613, 47.575, 47.585, 47.552, 47.552, 47.552,
47.51, 47.45, 47.403, 47.435, 47.45, 47.45, 47.45, 47.375,
47.445, 47, 47.665, 47.665, 47.665, 47.665, 47.719, 47.825,
47.67, 47.623, 47.615, 47.615, 47.615, 47.717, 47.809, 47.869,
47.72, 47.8, 47.8, 47.8, 47.695, 47.67, 47.586, 47.635, 47.63,
47.63, 47.63, 47.55, 47.335, 47.2, 47.233, 46.92, 46.92,
46.92, 46.98, 46.82, 46.875, 47, 47.17, 47.17, 47.17), PHP.CLOSE = c(46.46,
46.36, 46.36, 46.36, 46.36, 46.345, 46.535, 46.45, 46.58,
46.45, 46.45, 46.45, 46.28, 46.41, 46.88, 46.455, 46.6, 46.6,
46.6, 46.79, 46.75, 46.85, 46.835, 46.9, 46.9, 46.9, 47.02,
47.035, 46.961, 46.94, 47.11, 47.11, 47.11, 47.07, 47.18,
47.14, 47.15, 47.12, 47.12, 47.12, 47.077, 46.725, 46.94,
46.714, 46.85, 46.85, 46.85, 47.05, 47.21, 47.321, 47.31,
47.125, 47.125, 47.125, 47, 47.03, 47.03, 46.775, 46.915,
46.915, 46.915, 47.085, 46.965, 46.895, 47.03, 46.95, 46.95,
46.95, 46.51, 46.475, 46.38, 46.335, 46.425, 46.425, 46.425,
46.43, 46.43, 46.31, 46.08, 46.13, 46.13, 46.13, 46.055,
46.01, 46.11, 46.225, 46.465, 46.465, 46.465, 46.53, 46.64,
46.75, 46.75, 46.635, 46.635, 46.635, 46.69, 46.785, 46.92,
46.788, 46.75, 46.75, 46.75, 46.8, 46.66, 46.42, 46.46, 46.555,
46.555, 46.555, 46.647, 46.555, 46.757, 47.09, 47.09, 47.09,
47.09, 47.24, 47.08, 46.89, 46.965, 46.89, 46.89, 46.89,
46.77, 46.78, 46.76, 46.85, 46.645, 46.645, 46.645, 46.44,
46.225, 46.093, 46.185, 46.065, 46.065, 46.065, 46.25, 46.085,
46.018, 46.122, 46.135, 46.135, 46.135, 46.1, 46.2, 46.26,
46.08, 46.015, 46.015, 46.015, 46.07, 46.028, 46.35, 46.34,
46.395, 46.395, 46.395, 46.395, 46.395, 46.285, 46.33, 46.36,
46.36, 46.36, 46.38, 46.785, 46.83, 46.595, 46.62, 46.62,
46.62, 46.695, 46.855, 46.958, 46.92, 46.946, 46.946, 46.946,
47.125, 47.298, 47.35, 47.558, 47.515, 47.515, 47.515, 47.62,
47.625, 47.635, 47.619, 47.665, 47.665, 47.665, 47.55, 47.45,
47.573, 47.45, 47.74, 47.74, 47.74, 47.45, 47.47, 47.671,
47.665, 47.665, 47.665, 47.665, 47.775, 47.86, 47.685, 47.69,
47.65, 47.65, 47.65, 47.743, 47.815, 47.997, 47.89, 47.805,
47.805, 47.805, 47.935, 47.89, 47.67, 47.706, 47.745, 47.745,
47.745, 47.64, 47.542, 47.45, 47.269, 47.158, 47.158, 47.158,
47.01, 47.025, 46.89, 47.125, 47.17, 47.17, 47.17), PHP.LAST = c(46.46,
46.36, 46.36, 46.36, 46.36, 46.345, 46.535, 46.45, 46.58,
46.45, 46.45, 46.45, 46.28, 46.41, 46.88, 46.455, 46.6, 46.6,
46.6, 46.79, 46.75, 46.85, 46.835, 46.9, 46.9, 46.9, 47.02,
47.035, 46.961, 46.94, 47.11, 47.11, 47.11, 47.07, 47.18,
47.14, 47.15, 47.12, 47.12, 47.12, 47.077, 46.725, 46.94,
46.714, 46.85, 46.85, 46.85, 47.05, 47.21, 47.321, 47.31,
47.125, 47.125, 47.125, 47, 47.03, 47.03, 46.775, 46.915,
46.915, 46.915, 47.085, 46.965, 46.895, 47.03, 46.95, 46.95,
46.95, 46.51, 46.475, 46.38, 46.335, 46.425, 46.425, 46.425,
46.43, 46.43, 46.31, 46.08, 46.13, 46.13, 46.13, 46.055,
46.01, 46.11, 46.225, 46.465, 46.465, 46.465, 46.53, 46.64,
46.75, 46.75, 46.635, 46.635, 46.635, 46.69, 46.785, 46.92,
46.788, 46.75, 46.75, 46.75, 46.8, 46.66, 46.42, 46.46, 46.555,
46.555, 46.555, 46.647, 46.555, 46.757, 47.09, 47.09, 47.09,
47.09, 47.24, 47.08, 46.89, 46.965, 46.89, 46.89, 46.89,
46.77, 46.78, 46.76, 46.85, 46.645, 46.645, 46.645, 46.44,
46.225, 46.093, 46.185, 46.065, 46.065, 46.065, 46.25, 46.085,
46.018, 46.122, 46.135, 46.135, 46.135, 46.1, 46.2, 46.26,
46.08, 46.015, 46.015, 46.015, 46.07, 46.028, 46.35, 46.34,
46.395, 46.395, 46.395, 46.395, 46.395, 46.285, 46.33, 46.36,
46.36, 46.36, 46.38, 46.785, 46.83, 46.595, 46.62, 46.62,
46.62, 46.695, 46.855, 46.958, 46.92, 46.946, 46.946, 46.946,
47.125, 47.298, 47.35, 47.558, 47.515, 47.515, 47.515, 47.62,
47.625, 47.635, 47.619, 47.665, 47.665, 47.665, 47.55, 47.45,
47.573, 47.45, 47.74, 47.74, 47.74, 47.45, 47.47, 47.671,
47.665, 47.665, 47.665, 47.665, 47.775, 47.86, 47.685, 47.69,
47.65, 47.65, 47.65, 47.743, 47.815, 47.997, 47.89, 47.805,
47.805, 47.805, 47.935, 47.89, 47.67, 47.706, 47.745, 47.745,
47.745, 47.64, 47.542, 47.45, 47.269, 47.158, 47.158, 47.158,
47.01, 47.025, 46.89, 47.125, 47.17, 47.17, 47.17)), .Names = c("Date",
"PHP.OPEN", "PHP.HIGH", "PHP.LOW", "PHP.CLOSE", "PHP.LAST"
), row.names = c(NA, -243L), class = "data.frame"), structure(list(
Date = structure(c(1472533200, 1472446800, 1472360400,
1472274000, 1472187600, 1472101200, 1472014800, 1471928400,
1471842000, 1471755600, 1471669200, 1471582800, 1471496400,
1471410000, 1471323600, 1471237200, 1471150800, 1471064400,
1470978000, 1470891600, 1470805200, 1470718800, 1470632400,
1470546000, 1470459600, 1470373200, 1470286800, 1470200400,
1470114000, 1470027600, 1469941200, 1469854800, 1469768400,
1469682000, 1469595600, 1469509200, 1469422800, 1469336400,
1469250000, 1469163600, 1469077200, 1468990800, 1468904400,
1468818000, 1468731600, 1468645200, 1468558800, 1468472400,
1468386000, 1468299600, 1468213200, 1468126800, 1468040400,
1467954000, 1467867600, 1467781200, 1467694800, 1467608400,
1467522000, 1467435600, 1467349200, 1467262800, 1467176400,
1467090000, 1467003600, 1466917200, 1466830800, 1466744400,
1466658000, 1466571600, 1466485200, 1466398800, 1466312400,
1466226000, 1466139600, 1466053200, 1465966800, 1465880400,
1465794000, 1465707600, 1465621200, 1465534800, 1465448400,
1465362000, 1465275600, 1465189200, 1465102800, 1465016400,
1464930000, 1464843600, 1464757200, 1464670800, 1464584400,
1464498000, 1464411600, 1464325200, 1464238800, 1464152400,
1464066000, 1463979600, 1463893200, 1463806800, 1463720400,
1463634000, 1463547600, 1463461200, 1463374800, 1463288400,
1463202000, 1463115600, 1463029200, 1462942800, 1462856400,
1462770000, 1462683600, 1462597200, 1462510800, 1462424400,
1462338000, 1462251600, 1462165200, 1462078800, 1461992400,
1461906000, 1461819600, 1461733200, 1461646800, 1461560400,
1461474000, 1461387600, 1461301200, 1461214800, 1461128400,
1461042000, 1460955600, 1460869200, 1460782800, 1460696400,
1460610000, 1460523600, 1460437200, 1460350800, 1460264400,
1460178000, 1460091600, 1460005200, 1459918800, 1459832400,
1459746000, 1459659600, 1459573200, 1459486800, 1459400400,
1459314000, 1459227600, 1459141200, 1459054800, 1458968400,
1458882000, 1458795600, 1458709200, 1458622800, 1458536400,
1458450000, 1458363600, 1458277200, 1458190800, 1458104400,
1458018000, 1457931600, 1457848800, 1457762400, 1457676000,
1457589600, 1457503200, 1457416800, 1457330400, 1457244000,
1457157600, 1457071200, 1456984800, 1456898400, 1456812000,
1456725600, 1456639200, 1456552800, 1456466400, 1456380000,
1456293600, 1456207200, 1456120800, 1456034400, 1455948000,
1455861600, 1455775200, 1455688800, 1455602400, 1455516000,
1455429600, 1455343200, 1455256800, 1455170400, 1455084000,
1454997600, 1454911200, 1454824800, 1454738400, 1454652000,
1454565600, 1454479200, 1454392800, 1454306400, 1454220000,
1454133600, 1454047200, 1453960800, 1453874400, 1453788000,
1453701600, 1453615200, 1453528800, 1453442400, 1453356000,
1453269600, 1453183200, 1453096800, 1453010400, 1452924000,
1452837600, 1452751200, 1452664800, 1452578400, 1452492000,
1452405600, 1452319200, 1452232800, 1452146400, 1452060000,
1451973600, 1451887200, 1451800800, 1451714400, 1451628000
), tzone = "", class = c("POSIXct", "POSIXt")), THB.OPEN = c(34.53,
34.596, 34.506, 34.506, 34.506, 34.539, 34.573, 34.564,
34.607, 34.528, 34.528, 34.528, 34.531, 34.577, 34.52,
34.577, 34.696, 34.696, 34.696, 34.731, 34.738, 34.911,
34.901, 34.796, 34.796, 34.796, 34.886, 34.739, 34.74,
34.72, 34.8, 34.8, 34.8, 34.84, 34.951, 34.98, 34.905,
34.87, 34.87, 34.87, 34.95, 34.97, 34.931, 34.918, 34.907,
34.907, 34.907, 35.029, 35.13, 35.103, 35.049, 35.169,
35.169, 35.169, 35.145, 35.149, 35.012, 35.011, 35.06,
35.06, 35.06, 35.097, 35.175, 35.238, 35.257, 35.114,
35.114, 35.114, 35.146, 35.173, 35.15, 35.162, 35.225,
35.225, 35.225, 35.151, 35.24, 35.12, 35.16, 35.147,
35.147, 35.147, 35.085, 35.189, 35.217, 35.26, 35.545,
35.545, 35.545, 35.57, 35.64, 35.691, 35.69, 35.56, 35.56,
35.56, 35.571, 35.625, 35.664, 35.56, 35.622, 35.622,
35.622, 35.641, 35.486, 35.32, 35.341, 35.311, 35.311,
35.311, 35.217, 35.19, 35.193, 35.09, 35.126, 35.126,
35.126, 35.075, 34.99, 34.83, 34.83, 34.885, 34.885,
34.885, 35.007, 35.09, 35.08, 35.052, 35.02, 35.02, 35.02,
34.883, 34.825, 34.88, 35.01, 35.051, 35.051, 35.051,
35.024, 34.951, 35.005, 35.04, 35.09, 35.09, 35.09, 35.15,
35.214, 35.237, 35.12, 35.09, 35.09, 35.09, 35.159, 35.189,
35.323, 35.274, 35.265, 35.265, 35.265, 35.188, 34.917,
34.88, 34.85, 34.745, 34.745, 34.745, 34.77, 34.99, 35.07,
35.05, 35.064, 35.064, 35.064, 35.239, 35.287, 35.365,
35.349, 35.385, 35.385, 35.385, 35.476, 35.54, 35.588,
35.618, 35.63, 35.63, 35.63, 35.65, 35.69, 35.715, 35.717,
35.54, 35.54, 35.54, 35.537, 35.6, 35.59, 35.552, 35.228,
35.228, 35.228, 35.228, 35.3, 35.388, 35.495, 35.48,
35.48, 35.48, 35.55, 35.77, 35.579, 35.591, 35.684, 35.684,
35.684, 35.775, 35.77, 35.89, 35.925, 36.026, 36.026,
36.026, 36.235, 36.272, 36.26, 36.29, 36.28, 36.28, 36.28,
36.256, 36.237, 36.235, 36.25, 36.17, 36.17, 36.17, 36.226,
36.103, 36.08, 36.033, 36.02, 36.02, 36.02), THB.HIGH = c(34.655,
34.72, 34.601, 34.601, 34.601, 34.675, 34.69, 34.676,
34.764, 34.671, 34.671, 34.671, 34.69, 34.737, 34.65,
34.82, 34.86, 34.86, 34.86, 34.838, 34.967, 35.005, 35.065,
34.93, 34.93, 34.93, 35.039, 34.95, 34.84, 34.91, 34.92,
34.92, 34.92, 34.967, 35.074, 35.086, 35.08, 35, 35,
35, 35.088, 35.09, 35.04, 35.03, 35.085, 35.085, 35.085,
35.23, 35.257, 35.206, 35.225, 35.264, 35.264, 35.264,
35.239, 35.281, 35.198, 35.125, 35.16, 35.16, 35.16,
35.228, 35.295, 35.371, 35.435, 35.546, 35.546, 35.546,
35.244, 35.29, 35.24, 35.284, 35.356, 35.356, 35.356,
35.254, 35.39, 35.303, 35.295, 35.295, 35.295, 35.295,
35.24, 35.241, 35.327, 35.411, 35.64, 35.64, 35.64, 35.704,
35.77, 35.755, 35.8, 35.701, 35.701, 35.701, 35.68, 35.795,
35.787, 35.706, 35.736, 35.736, 35.736, 35.8, 35.625,
35.557, 35.461, 35.466, 35.466, 35.466, 35.34, 35.294,
35.267, 35.166, 35.193, 35.193, 35.193, 35.19, 35.184,
34.94, 34.934, 35.009, 35.009, 35.009, 35.16, 35.195,
35.222, 35.17, 35.095, 35.095, 35.095, 35.095, 34.949,
35.02, 35.11, 35.12, 35.12, 35.12, 35.211, 35.055, 35.07,
35.112, 35.234, 35.234, 35.234, 35.253, 35.31, 35.39,
35.29, 35.175, 35.175, 35.175, 35.262, 35.365, 35.44,
35.408, 35.365, 35.365, 35.365, 35.395, 35.13, 34.963,
34.952, 34.925, 34.925, 34.925, 34.975, 35.145, 35.15,
35.151, 35.26, 35.26, 35.26, 35.31, 35.393, 35.47, 35.495,
35.46, 35.46, 35.46, 35.559, 35.656, 35.655, 35.763,
35.707, 35.707, 35.707, 35.714, 35.759, 35.776, 35.781,
35.75, 35.75, 35.75, 35.63, 35.68, 35.671, 35.69, 35.64,
35.64, 35.64, 35.325, 35.428, 35.539, 35.612, 35.651,
35.651, 35.651, 35.755, 35.87, 35.771, 35.77, 35.861,
35.861, 35.861, 35.885, 35.874, 36.045, 36.055, 36.253,
36.253, 36.253, 36.29, 36.327, 36.325, 36.382, 36.374,
36.374, 36.374, 36.345, 36.338, 36.375, 36.345, 36.327,
36.327, 36.327, 36.35, 36.257, 36.177, 36.19, 36.033,
36.033, 36.033), THB.LOW = c(34.53, 34.596, 34.506, 34.506,
34.506, 34.539, 34.573, 34.564, 34.607, 34.528, 34.528,
34.528, 34.531, 34.577, 34.52, 34.577, 34.696, 34.696,
34.696, 34.731, 34.738, 34.911, 34.901, 34.796, 34.796,
34.796, 34.886, 34.739, 34.74, 34.72, 34.8, 34.8, 34.8,
34.84, 34.951, 34.98, 34.905, 34.87, 34.87, 34.87, 34.95,
34.97, 34.931, 34.918, 34.907, 34.907, 34.907, 35.029,
35.13, 35.103, 35.049, 35.169, 35.169, 35.169, 35.145,
35.149, 35.012, 35.011, 35.06, 35.06, 35.06, 35.097,
35.175, 35.238, 35.257, 35.114, 35.114, 35.114, 35.146,
35.173, 35.15, 35.162, 35.225, 35.225, 35.225, 35.151,
35.24, 35.12, 35.16, 35.147, 35.147, 35.147, 35.085,
35.189, 35.217, 35.26, 35.545, 35.545, 35.545, 35.57,
35.64, 35.691, 35.69, 35.56, 35.56, 35.56, 35.571, 35.625,
35.664, 35.56, 35.622, 35.622, 35.622, 35.641, 35.486,
35.32, 35.341, 35.311, 35.311, 35.311, 35.217, 35.19,
35.193, 35.09, 35.126, 35.126, 35.126, 35.075, 34.99,
34.83, 34.83, 34.885, 34.885, 34.885, 35.007, 35.09,
35.08, 35.052, 35.02, 35.02, 35.02, 34.883, 34.825, 34.88,
35.01, 35.051, 35.051, 35.051, 35.024, 34.951, 35.005,
35.04, 35.09, 35.09, 35.09, 35.15, 35.214, 35.237, 35.12,
35.09, 35.09, 35.09, 35.159, 35.189, 35.323, 35.274,
35.265, 35.265, 35.265, 35.188, 34.917, 34.88, 34.85,
34.745, 34.745, 34.745, 34.77, 34.99, 35.07, 35.05, 35.064,
35.064, 35.064, 35.239, 35.287, 35.365, 35.349, 35.385,
35.385, 35.385, 35.476, 35.54, 35.588, 35.618, 35.63,
35.63, 35.63, 35.65, 35.69, 35.715, 35.717, 35.54, 35.54,
35.54, 35.537, 35.6, 35.59, 35.552, 35.228, 35.228, 35.228,
35.228, 35.3, 35.388, 35.495, 35.48, 35.48, 35.48, 35.55,
35.77, 35.579, 35.591, 35.684, 35.684, 35.684, 35.775,
35.77, 35.89, 35.925, 36.026, 36.026, 36.026, 36.235,
36.272, 36.26, 36.29, 36.28, 36.28, 36.28, 36.256, 36.237,
36.235, 36.25, 36.17, 36.17, 36.17, 36.226, 36.103, 36.08,
36.033, 36.02, 36.02, 36.02), THB.CLOSE = c(34.558, 34.601,
34.565, 34.565, 34.565, 34.565, 34.618, 34.64, 34.67,
34.635, 34.635, 34.635, 34.605, 34.7, 34.575, 34.61,
34.81, 34.81, 34.81, 34.76, 34.751, 34.965, 34.974, 34.822,
34.822, 34.822, 34.927, 34.94, 34.75, 34.77, 34.83, 34.83,
34.83, 34.868, 34.97, 35.01, 35.05, 34.93, 34.93, 34.93,
34.995, 34.989, 35.01, 35.005, 34.94, 34.94, 34.94, 35.03,
35.16, 35.138, 35.11, 35.25, 35.25, 35.25, 35.173, 35.214,
35.134, 35.048, 35.086, 35.086, 35.086, 35.138, 35.23,
35.29, 35.35, 35.339, 35.339, 35.339, 35.173, 35.24,
35.173, 35.21, 35.235, 35.235, 35.235, 35.24, 35.25,
35.3, 35.18, 35.26, 35.26, 35.26, 35.21, 35.21, 35.23,
35.28, 35.629, 35.629, 35.629, 35.61, 35.67, 35.705,
35.75, 35.658, 35.658, 35.658, 35.6, 35.71, 35.73, 35.66,
35.679, 35.679, 35.679, 35.71, 35.6, 35.49, 35.37, 35.43,
35.43, 35.43, 35.32, 35.27, 35.236, 35.13, 35.149, 35.149,
35.149, 35.179, 35.184, 34.92, 34.841, 34.92, 34.92,
34.92, 35.02, 35.15, 35.16, 35.11, 35.04, 35.04, 35.04,
35.01, 34.86, 34.91, 35.03, 35.065, 35.065, 35.065, 35.12,
35.036, 35.02, 35.07, 35.12, 35.12, 35.12, 35.205, 35.256,
35.32, 35.263, 35.105, 35.105, 35.105, 35.185, 35.235,
35.426, 35.36, 35.27, 35.27, 35.27, 35.35, 35.118, 34.91,
34.89, 34.85, 34.85, 34.85, 34.78, 35.02, 35.132, 35.08,
35.16, 35.16, 35.16, 35.25, 35.32, 35.425, 35.42, 35.403,
35.403, 35.403, 35.51, 35.57, 35.625, 35.64, 35.64, 35.64,
35.64, 35.68, 35.7, 35.74, 35.755, 35.745, 35.745, 35.745,
35.548, 35.61, 35.61, 35.65, 35.58, 35.58, 35.58, 35.245,
35.36, 35.489, 35.51, 35.497, 35.497, 35.497, 35.56,
35.841, 35.76, 35.605, 35.73, 35.73, 35.73, 35.825, 35.79,
35.908, 36.021, 36.037, 36.037, 36.037, 36.263, 36.3,
36.27, 36.32, 36.366, 36.366, 36.366, 36.313, 36.25,
36.33, 36.28, 36.307, 36.307, 36.307, 36.285, 36.22,
36.15, 36.14, 36.02, 36.02, 36.02), THB.LAST = c(34.558,
34.601, 34.565, 34.565, 34.565, 34.565, 34.618, 34.64,
34.67, 34.635, 34.635, 34.635, 34.605, 34.7, 34.575,
34.61, 34.81, 34.81, 34.81, 34.76, 34.751, 34.965, 34.974,
34.822, 34.822, 34.822, 34.927, 34.94, 34.75, 34.77,
34.83, 34.83, 34.83, 34.868, 34.97, 35.01, 35.05, 34.93,
34.93, 34.93, 34.995, 34.989, 35.01, 35.005, 34.94, 34.94,
34.94, 35.03, 35.16, 35.138, 35.11, 35.25, 35.25, 35.25,
35.173, 35.214, 35.134, 35.048, 35.086, 35.086, 35.086,
35.138, 35.23, 35.29, 35.35, 35.339, 35.339, 35.339,
35.173, 35.24, 35.173, 35.21, 35.235, 35.235, 35.235,
35.24, 35.25, 35.3, 35.18, 35.26, 35.26, 35.26, 35.21,
35.21, 35.23, 35.28, 35.629, 35.629, 35.629, 35.61, 35.67,
35.705, 35.75, 35.658, 35.658, 35.658, 35.6, 35.71, 35.73,
35.66, 35.679, 35.679, 35.679, 35.71, 35.6, 35.49, 35.37,
35.43, 35.43, 35.43, 35.32, 35.27, 35.236, 35.13, 35.149,
35.149, 35.149, 35.179, 35.184, 34.92, 34.841, 34.92,
34.92, 34.92, 35.02, 35.15, 35.16, 35.11, 35.04, 35.04,
35.04, 35.01, 34.86, 34.91, 35.03, 35.065, 35.065, 35.065,
35.12, 35.036, 35.02, 35.07, 35.12, 35.12, 35.12, 35.205,
35.256, 35.32, 35.263, 35.105, 35.105, 35.105, 35.185,
35.235, 35.426, 35.36, 35.27, 35.27, 35.27, 35.35, 35.118,
34.91, 34.89, 34.85, 34.85, 34.85, 34.78, 35.02, 35.132,
35.08, 35.16, 35.16, 35.16, 35.25, 35.32, 35.425, 35.42,
35.403, 35.403, 35.403, 35.51, 35.57, 35.625, 35.64,
35.64, 35.64, 35.64, 35.68, 35.7, 35.74, 35.755, 35.745,
35.745, 35.745, 35.548, 35.61, 35.61, 35.65, 35.58, 35.58,
35.58, 35.245, 35.36, 35.489, 35.51, 35.497, 35.497,
35.497, 35.56, 35.841, 35.76, 35.605, 35.73, 35.73, 35.73,
35.825, 35.79, 35.908, 36.021, 36.037, 36.037, 36.037,
36.263, 36.3, 36.27, 36.32, 36.366, 36.366, 36.366, 36.313,
36.25, 36.33, 36.28, 36.307, 36.307, 36.307, 36.285,
36.22, 36.15, 36.14, 36.02, 36.02, 36.02)), .Names = c("Date",
"THB.OPEN", "THB.HIGH", "THB.LOW", "THB.CLOSE", "THB.LAST"
), row.names = c(NA, -243L), class = "data.frame"))