I am working with a trade tick database, investigating market micro structure. the example code below is for a specific trading day.
What approach would be advised to determine the day of the xts object's index and then use it to loop through the code below, per day?
my.xts <- xts(my.frame1[,-1], order.by=my.frame1[,1])
my.val <- my.xts[,1]*my.xts[,2]
my.cumvol <- cumsum(my.xts[,2])
my.cumval <- cumsum(my.val[,1])
my.vwap <- my.cumval/my.cumvol
my.ratio <- (my.xts[,1]/my.vwap - 1)*10000
op <- par(mfrow=c(1,2))
plot(my.ratio)
plot(my.xts[,1])
lines(my.vwap)
par(op)
Example of my.xts:
PRICE QUANTITY
2013-06-05 10:00:08 2772 1250
2013-06-05 10:00:08 2772 1204
2013-06-05 10:00:11 2772 1773
2013-06-05 10:00:11 2772 62
2013-06-05 10:00:11 2773 1250
2013-06-05 10:00:11 2773 1250
2013-06-05 10:00:14 2773 1250
2013-06-05 10:00:14 2773 1250
2013-06-05 10:00:14 2772 1217
2013-06-05 10:00:33 2773 1811
2013-06-05 10:00:33 2773 1250
2013-06-05 10:00:42 2773 1610
2013-06-05 10:00:42 2773 1420
2013-06-05 10:00:42 2773 190
2013-06-05 10:01:17 2773 1357
2013-06-05 10:01:17 2773 1800
2013-06-05 10:01:17 2773 1495
2013-06-05 10:01:17 2773 283
2013-06-05 10:02:07 2775 246
2013-06-05 10:02:07 2775 171
2013-06-05 10:02:07 2775 1233
2013-06-05 10:02:14 2775 1500
2013-06-05 10:02:33 2773 700
2013-06-05 10:02:40 2773 3034
2013-06-05 10:02:40 2773 750
2013-06-05 10:02:40 2773 1498
2013-06-05 10:02:58 2773 515
2013-06-05 10:02:58 2773 11985
2013-06-05 10:03:02 2773 1665
2013-06-05 10:03:03 2772 1360
2013-06-05 10:03:03 2771 1140
2013-06-05 10:03:08 2775 3325
2013-06-05 10:03:34 2772 1500
2013-06-05 10:03:34 2772 1360
2013-06-05 10:03:34 2772 1220
2013-06-05 10:03:34 2772 3323
2013-06-05 10:03:42 2772 327
2013-06-05 10:04:13 2775 3339
2013-06-05 10:04:39 2773 7730
2013-06-05 10:04:48 2775 1554
2013-06-05 10:05:15 2774 3561
2013-06-05 10:05:15 2774 1439
2013-06-05 10:05:16 2773 577
2013-06-05 10:05:16 2773 2225
2013-06-05 10:05:16 2773 1083
2013-06-05 10:05:16 2773 1900
2013-06-05 10:05:16 2774 1502
2013-06-05 10:05:16 2773 102
2013-06-05 10:05:16 2773 1338
2013-06-05 10:05:16 2773 1228
2013-06-05 10:05:16 2773 2270
2013-06-05 10:05:26 2773 2775
2013-06-05 10:05:36 2772 9000
2013-06-05 10:05:36 2772 1000
2013-06-05 10:05:42 2773 2409
2013-06-05 10:05:44 2773 464
2013-06-05 10:05:44 2773 1743
2013-06-05 10:05:44 2773 1477
2013-06-05 10:05:44 2773 1925
2013-06-05 10:05:44 2773 2438
2013-06-05 10:05:44 2773 309
2013-06-05 10:05:44 2773 1932
2013-06-05 10:05:44 2773 1447
2013-06-05 10:05:44 2773 446
2013-06-05 10:05:44 2773 1410
2013-06-05 10:05:44 2773 372
2013-06-05 10:05:44 2773 715
2013-06-05 10:05:44 2773 847
2013-06-05 10:05:44 2773 1628
2013-06-05 10:05:44 2773 1574
2013-06-05 10:05:44 2773 1504
2013-06-05 10:05:44 2773 963
2013-06-05 10:05:44 2773 1531
2013-06-05 10:05:44 2773 1628
2013-06-05 10:05:44 2773 2185
2013-06-05 10:05:44 2773 1639
2013-06-05 10:05:44 2773 2330
2013-06-05 10:05:44 2773 2113
2013-06-05 10:05:44 2773 841
2013-06-05 10:05:45 2773 2226
2013-06-05 10:05:45 2773 1332
2013-06-05 10:05:45 2773 2320
2013-06-05 10:05:45 2773 845
2013-06-05 10:05:45 2773 1665
2013-06-05 10:05:45 2773 609
2013-06-05 10:05:45 2773 2226
2013-06-05 10:05:45 2773 1497
2013-06-05 10:05:45 2773 405
2013-06-05 10:05:45 2773 1022
2013-06-05 10:05:45 2773 174
2013-06-05 10:05:45 2773 1608
2013-06-05 10:05:45 2773 228
2013-06-05 10:05:45 2773 2020
2013-06-05 10:05:45 2773 1885
2013-06-05 10:05:45 2773 1524
2013-06-05 10:05:45 2773 2262
2013-06-05 10:05:45 2773 1618
2013-06-05 10:05:45 2773 2060
2013-06-05 10:05:45 2773 1843
2013-06-05 10:05:45 2773 2036
2013-06-05 10:05:45 2773 293
2013-06-05 10:05:45 2773 2034
2013-06-05 10:05:45 2773 1039
2013-06-05 10:05:45 2773 704
2013-06-05 10:05:46 2773 91
2013-06-05 10:05:46 2773 1135
2013-06-05 10:05:46 2773 1583
2013-06-05 10:05:46 2773 972
2013-06-05 10:05:46 2773 1309
2013-06-05 10:05:46 2773 2429
2013-06-05 10:05:46 2773 2164
2013-06-05 10:05:46 2773 1708
2013-06-05 10:05:46 2773 1527
2013-06-05 10:05:46 2773 2174
2013-06-05 10:05:46 2773 434
2013-06-05 10:05:46 2773 892
2013-06-05 10:05:46 2773 1554
2013-06-05 10:05:46 2773 2091
2013-06-05 10:05:46 2773 2107
2013-06-05 10:05:46 2773 1933
2013-06-05 10:05:46 2773 2341
2013-06-05 10:05:46 2773 619
2013-06-05 10:05:46 2773 623
2013-06-05 10:05:46 2773 2393
2013-06-05 10:05:46 2773 1977
2013-06-05 10:05:46 2773 1897
2013-06-05 10:05:46 2773 204
2013-06-05 10:05:46 2773 890
2013-06-05 10:05:46 2773 1738
2013-06-05 10:05:46 2773 1998
2013-06-05 10:05:46 2773 1684
2013-06-05 10:05:46 2773 1069
2013-06-05 10:05:46 2773 1578
2013-06-05 10:05:46 2773 1769
2013-06-05 10:05:47 2773 1535
2013-06-05 10:05:47 2773 1159
2013-06-05 10:05:47 2773 1354
2013-06-05 10:05:47 2773 1300
2013-06-05 10:05:47 2773 1300
2013-06-05 10:05:47 2773 1605
2013-06-05 10:05:47 2773 937
2013-06-05 10:05:47 2775 2802
2013-06-05 10:05:47 2773 2063
2013-06-05 10:05:47 2773 2095
2013-06-05 10:05:47 2773 283
2013-06-05 10:05:47 2773 366
2013-06-05 10:05:47 2773 1940
2013-06-05 10:05:52 2775 2011
2013-06-05 10:05:52 2775 1135
2013-06-05 10:06:01 2775 3184
2013-06-05 10:06:02 2775 3184
2013-06-05 10:06:02 2775 1911
2013-06-05 10:06:02 2775 3184
2013-06-05 10:06:02 2775 3184
2013-06-05 10:06:05 2779 703
2013-06-05 10:06:05 2779 1109
2013-06-05 10:06:05 2779 2802
2013-06-05 10:06:05 2779 1212
2013-06-05 10:06:05 2778 740
2013-06-05 10:06:05 2777 1346
2013-06-05 10:06:05 2778 260
2013-06-05 10:06:06 2779 1591
2013-06-05 10:06:06 2780 51
2013-06-05 10:06:07 2779 3006
2013-06-05 10:06:18 2779 3188
2013-06-05 10:06:18 2779 3188
2013-06-05 10:06:18 2779 564
2013-06-05 10:06:18 2779 2624
2013-06-05 10:06:19 2779 2207
2013-06-05 10:06:21 2776 1136
2013-06-05 10:06:21 2776 1300
2013-06-05 10:06:21 2776 1300
2013-06-05 10:06:21 2776 1800
2013-06-05 10:06:21 2776 16
2013-06-05 10:06:30 2780 3051
2013-06-05 10:06:30 2780 3179
2013-06-05 10:06:30 2779 229
2013-06-05 10:06:30 2780 2950
2013-06-05 10:06:41 2780 3171
2013-06-05 10:06:41 2780 2598
2013-06-05 10:06:41 2780 573
2013-06-05 10:06:41 2780 859
2013-06-05 10:06:49 2780 4768
2013-06-05 10:06:49 2780 2338
2013-06-05 10:06:49 2780 2856
2013-06-05 10:06:49 2780 38
2013-06-05 10:06:52 2782 292
2013-06-05 10:06:52 2781 2932
2013-06-05 10:07:34 2782 209
2013-06-05 10:07:34 2782 3255
2013-06-05 10:07:42 2780 1435
2013-06-05 10:07:42 2780 1285
2013-06-05 10:07:42 2780 400
2013-06-05 10:07:42 2780 637
2013-06-05 10:07:42 2780 1547
2013-06-05 10:07:42 2780 263
2013-06-05 10:07:42 2780 5200
2013-06-05 10:07:42 2780 5200
2013-06-05 10:07:42 2782 4475
2013-06-05 10:07:42 2780 2663
2013-06-05 10:07:42 2780 438
2013-06-05 10:07:42 2780 5200
2013-06-05 10:07:42 2781 1900
2013-06-05 10:07:42 2780 510
2013-06-05 10:07:42 2780 910
2013-06-05 10:07:42 2782 2646
2013-06-05 10:07:42 2780 1445
2013-06-05 10:07:42 2780 4290
2013-06-05 10:07:42 2780 3477
2013-06-05 10:07:42 2780 4800
2013-06-05 10:07:43 2780 3200
2013-06-05 10:07:43 2780 3877
2013-06-05 10:07:43 2780 1323
2013-06-05 10:07:54 2782 2373
2013-06-05 10:07:59 2782 3249
2013-06-05 10:08:03 2780 2077
2013-06-05 10:08:03 2780 10000
2013-06-05 10:08:03 2780 1903
2013-06-05 10:08:03 2780 1226
2013-06-05 10:08:03 2780 670
2013-06-05 10:08:03 2780 7359
2013-06-05 10:08:03 2780 2923
2013-06-05 10:08:03 2780 10000
2013-06-05 10:08:03 2780 564
2013-06-05 10:08:04 2780 1488
2013-06-05 10:08:11 2780 200
2013-06-05 10:08:11 2780 223
2013-06-05 10:08:11 2780 4713
2013-06-05 10:08:13 2778 1453
2013-06-05 10:08:13 2780 1500
2013-06-05 10:08:13 2780 1594
2013-06-05 10:08:13 2780 8837
2013-06-05 10:08:13 2780 1599
2013-06-05 10:08:13 2780 3186
2013-06-05 10:08:13 2780 10000
2013-06-05 10:08:13 2780 10000
2013-06-05 10:08:13 2780 2763
2013-06-05 10:08:13 2780 1163
2013-06-05 10:08:13 2780 1900
2013-06-05 10:08:14 2782 2924
2013-06-05 10:08:23 2782 3183
2013-06-05 10:08:24 2782 1572
2013-06-05 10:08:24 2780 1211
2013-06-05 10:08:24 2782 1372
2013-06-05 10:08:24 2782 239
2013-06-05 10:08:24 2782 2581
2013-06-05 10:08:24 2782 3183
2013-06-05 10:08:25 2780 5596
2013-06-05 10:08:25 2780 1845
2013-06-05 10:08:25 2780 1307
2013-06-05 10:08:25 2780 1500
2013-06-05 10:08:25 2780 6604
2013-06-05 10:08:25 2780 10000
2013-06-05 10:08:25 2780 1300
2013-06-05 10:08:37 2782 597
2013-06-05 10:08:37 2782 3180
2013-06-05 10:08:39 2780 2028
2013-06-05 10:08:39 2780 3005
2013-06-05 10:08:39 2780 251
2013-06-05 10:08:39 2780 1430
2013-06-05 10:08:39 2780 244
2013-06-05 10:08:39 2780 673
2013-06-05 10:08:39 2781 1300
2013-06-05 10:08:39 2780 1500
2013-06-05 10:08:39 2780 10000
2013-06-05 10:08:39 2780 947
2013-06-05 10:08:39 2781 1500
2013-06-05 10:08:39 2780 10000
2013-06-05 10:08:42 2782 886
2013-06-05 10:08:46 2782 717
2013-06-05 10:08:46 2784 283
2013-06-05 10:08:50 2780 1889
2013-06-05 10:08:50 2780 2254
2013-06-05 10:08:50 2780 1650
2013-06-05 10:08:50 2780 1300
2013-06-05 10:08:50 2780 1549
2013-06-05 10:08:50 2780 1758
2013-06-05 10:08:50 2780 2274
2013-06-05 10:08:50 2780 1097
2013-06-05 10:08:50 2780 1500
2013-06-05 10:08:51 2780 1804
2013-06-05 10:08:51 2780 2386
2013-06-05 10:08:51 2780 2337
2013-06-05 10:08:51 2780 2042
2013-06-05 10:08:51 2780 2377
2013-06-05 10:08:51 2780 2066
2013-06-05 10:08:51 2780 182
2013-06-05 10:08:51 2780 1784
2013-06-05 10:08:51 2780 1913
2013-06-05 10:08:51 2780 457
2013-06-05 10:08:51 2780 2429
2013-06-05 10:08:51 2780 1511
2013-06-05 10:08:51 2780 2316
2013-06-05 10:08:51 2780 1569
2013-06-05 10:08:51 2780 2008
2013-06-05 10:08:51 2780 777
2013-06-05 10:08:51 2780 1792
2013-06-05 10:08:51 2780 1815
2013-06-05 10:08:51 2780 933
2013-06-05 10:08:51 2780 1684
2013-06-05 10:08:51 2780 2255
2013-06-05 10:08:51 2780 2049
2013-06-05 10:08:51 2780 1858
2013-06-05 10:08:51 2780 1659
2013-06-05 10:08:56 2784 1000
2013-06-05 10:08:56 2784 3717
2013-06-05 10:08:57 2783 3173
2013-06-05 10:08:57 2783 3173
2013-06-05 10:08:57 2783 3173
2013-06-05 10:08:58 2783 1215
2013-06-05 10:08:58 2783 3173
2013-06-05 10:09:09 2780 2469
2013-06-05 10:09:09 2780 2412
2013-06-05 10:09:09 2780 1859
2013-06-05 10:09:09 2780 3186
2013-06-05 10:09:09 2780 1732
2013-06-05 10:09:09 2780 1787
2013-06-05 10:09:09 2780 1827
2013-06-05 10:09:09 2780 2419
2013-06-05 10:09:09 2780 2238
2013-06-05 10:09:09 2780 2120
2013-06-05 10:09:09 2780 1300
2013-06-05 10:09:09 2780 3186
2013-06-05 10:09:10 2780 1846
2013-06-05 10:09:10 2780 1023
2013-06-05 10:09:10 2780 2213
2013-06-05 10:09:10 2780 1514
2013-06-05 10:09:10 2780 2172
2013-06-05 10:09:10 2780 1741
2013-06-05 10:09:10 2780 2267
2013-06-05 10:09:10 2780 1523
2013-06-05 10:09:10 2780 1510
2013-06-05 10:09:10 2780 2056
2013-06-05 10:09:10 2780 1593
2013-06-05 10:09:10 2780 3186
2013-06-05 10:09:10 2780 1727
2013-06-05 10:09:10 2780 276
2013-06-05 10:09:10 2780 771
2013-06-05 10:09:10 2780 2240
2013-06-05 10:09:10 2780 2134
2013-06-05 10:09:19 2783 3174
2013-06-05 10:09:20 2783 3174
2013-06-05 10:09:20 2783 1207
2013-06-05 10:09:20 2783 3174
2013-06-05 10:09:20 2783 3174
2013-06-05 10:09:21 2780 2238
2013-06-05 10:09:21 2780 1619
2013-06-05 10:09:21 2780 938
2013-06-05 10:09:21 2780 1572
2013-06-05 10:09:21 2780 2496
2013-06-05 10:09:21 2780 2339
2013-06-05 10:09:21 2780 2457
2013-06-05 10:09:21 2780 2398
2013-06-05 10:09:21 2780 1806
2013-06-05 10:09:21 2780 1811
2013-06-05 10:09:21 2780 1300
2013-06-05 10:09:21 2780 1907
2013-06-05 10:09:21 2780 1804
2013-06-05 10:09:21 2780 1919
2013-06-05 10:09:21 2780 2098
2013-06-05 10:09:22 2780 808
2013-06-05 10:09:22 2780 2128
2013-06-05 10:09:22 2780 1604
2013-06-05 10:09:22 2780 1620
2013-06-05 10:09:22 2778 303
2013-06-05 10:09:22 2778 1362
2013-06-05 10:09:22 2780 1910
2013-06-05 10:09:22 2780 2277
2013-06-05 10:09:22 2778 1331
2013-06-05 10:09:22 2780 2018
2013-06-05 10:09:22 2780 2422
2013-06-05 10:09:22 2780 2142
2013-06-05 10:09:22 2778 341
2013-06-05 10:09:22 2780 142
2013-06-05 10:09:22 2780 2222
2013-06-05 10:09:22 2780 1502
2013-06-05 10:09:22 2780 2271
2013-06-05 10:09:22 2780 1574
2013-06-05 10:09:22 2780 2187
2013-06-05 10:09:22 2780 1515
2013-06-05 10:09:22 2780 2079
2013-06-05 10:09:22 2780 1669
2013-06-05 10:09:23 2778 1115
2013-06-05 10:09:23 2780 1857
2013-06-05 10:09:23 2777 1750
2013-06-05 10:09:23 2777 1650
2013-06-05 10:09:35 2783 1010
2013-06-05 10:09:35 2783 2190
2013-06-05 10:09:35 2783 3200
2013-06-05 10:09:35 2783 3200
2013-06-05 10:09:35 2783 3200
2013-06-05 10:09:36 2783 796
2013-06-05 10:09:49 2783 3191
2013-06-05 10:09:49 2783 3117
2013-06-05 10:09:49 2783 1926
2013-06-05 10:09:49 2783 74
2013-06-05 10:10:01 2783 1512
2013-06-05 10:10:01 2783 2687
2013-06-05 10:10:16 2783 3145
2013-06-05 10:10:16 2783 966
2013-06-05 10:10:27 2783 662
2013-06-06 10:00:40 2733 353
2013-06-06 10:00:40 2733 2145
2013-06-06 10:00:40 2733 1569
2013-06-06 10:00:58 2732 9000
2013-06-06 10:00:58 2732 1500
2013-06-06 10:00:58 2732 1884
2013-06-06 10:00:58 2732 2508
2013-06-06 10:01:00 2732 5942
2013-06-06 10:01:03 2732 650
2013-06-06 10:01:03 2732 250
2013-06-06 10:01:03 2732 1458
2013-06-06 10:01:04 2732 712
2013-06-06 10:01:04 2732 1674
2013-06-06 10:01:04 2732 26
2013-06-06 10:01:08 2732 901
2013-06-06 10:01:21 2732 1386
2013-06-06 10:01:21 2732 422
2013-06-06 10:01:22 2732 1600
2013-06-06 10:01:34 2731 3003
2013-06-06 10:01:34 2731 1361
2013-06-06 10:01:34 2731 3000
2013-06-06 10:01:34 2731 1541
2013-06-06 10:01:34 2731 4000
2013-06-06 10:01:35 2730 4723
2013-06-06 10:01:38 2732 1000
2013-06-06 10:01:41 2732 5758
2013-06-06 10:01:41 2732 1932
2013-06-06 10:01:41 2732 8092
2013-06-06 10:01:41 2732 9253
2013-06-06 10:01:41 2732 2549
2013-06-06 10:01:41 2732 704
2013-06-06 10:01:41 2732 6654
2013-06-06 10:01:55 2735 500
2013-06-06 10:02:01 2733 1260
2013-06-06 10:02:01 2733 1260
2013-06-06 10:02:01 2733 1997
2013-06-06 10:02:01 2733 419
2013-06-06 10:03:13 2735 1813
2013-06-06 10:03:16 2735 1832
2013-06-06 10:03:16 2735 374
2013-06-06 10:03:16 2735 2864
2013-06-06 10:03:16 2735 4324
2013-06-06 10:03:17 2735 2245
2013-06-06 10:03:18 2735 5645
2013-06-06 10:03:18 2735 2518
2013-06-06 10:03:19 2735 1305
2013-06-06 10:03:19 2735 1354
2013-06-06 10:03:21 2735 597
2013-06-06 10:03:21 2735 1712
2013-06-06 10:03:22 2735 200
2013-06-06 10:03:22 2735 197
2013-06-06 10:03:22 2735 2045
2013-06-06 10:03:22 2735 45
2013-06-06 10:03:25 2737 200
2013-06-06 10:03:29 2735 2234
2013-06-06 10:03:34 2739 1500
2013-06-06 10:03:41 2739 4458
2013-06-06 10:03:41 2739 2500
2013-06-06 10:03:46 2739 14603
2013-06-06 10:03:50 2736 359
2013-06-06 10:04:06 2739 1088
2013-06-06 10:04:10 2739 1499
2013-06-06 10:04:10 2739 1
2013-06-06 10:04:11 2739 1
2013-06-06 10:04:11 2739 1495
2013-06-06 10:04:16 2739 3505
2013-06-06 10:04:19 2740 172
2013-06-06 10:04:19 2738 9910
2013-06-06 10:04:19 2738 1300
2013-06-06 10:04:24 2738 1468
2013-06-06 10:04:24 2738 1824
2013-06-06 10:04:41 2739 1601
2013-06-06 10:04:47 2738 4595
2013-06-06 10:04:53 2738 394
2013-06-06 10:04:53 2738 6165
2013-06-06 10:04:53 2738 6162
2013-06-06 10:04:53 2738 1800
2013-06-06 10:04:53 2738 5479
2013-06-06 10:05:01 2739 7985
2013-06-06 10:05:05 2739 1947
2013-06-06 10:05:08 2740 5000
2013-06-06 10:05:08 2739 3664
2013-06-06 10:05:08 2739 5527
2013-06-06 10:05:08 2740 3056
2013-06-06 10:05:08 2740 5000
2013-06-06 10:05:53 2740 516
2013-06-06 10:06:07 2740 1000
2013-06-06 10:06:44 2740 3347
2013-06-06 10:06:44 2740 3653
2013-06-06 10:06:58 2740 730
2013-06-06 10:07:27 2738 4613
2013-06-06 10:07:27 2738 1532
2013-06-06 10:07:27 2738 1505
2013-06-06 10:07:38 2738 100
2013-06-06 10:07:47 2738 6052
2013-06-06 10:07:47 2738 5287
2013-06-06 10:07:47 2738 1542
2013-06-06 10:07:47 2738 1443
2013-06-06 10:08:17 2739 5419
2013-06-06 10:08:17 2739 3650
2013-06-06 10:08:17 2739 3385
2013-06-06 10:08:17 2739 2149
2013-06-06 10:08:19 2738 1936
2013-06-06 10:08:22 2738 3971
2013-06-06 10:08:29 2739 1961
2013-06-06 10:08:29 2739 4278
2013-06-06 10:08:29 2739 1426
2013-06-06 10:08:32 2739 1847
2013-06-06 10:08:38 2739 3838
2013-06-06 10:08:38 2740 2662
2013-06-06 10:08:44 2740 1963
2013-06-06 10:08:45 2740 300
2013-06-06 10:08:52 2740 75
2013-06-06 10:08:52 2740 75
2013-06-06 10:08:52 2740 75
2013-06-06 10:08:52 2740 75
2013-06-06 10:08:52 2740 31
2013-06-06 10:09:12 2745 3210
2013-06-06 10:09:12 2745 3381
2013-06-06 10:09:12 2745 1331
2013-06-06 10:09:12 2745 69
2013-06-06 10:09:12 2745 3649
2013-06-06 10:09:12 2745 3000
2013-06-06 10:09:12 2745 5912
2013-06-06 10:09:12 2745 1487
2013-06-06 10:09:28 2745 5000
2013-06-06 10:09:28 2745 5000
2013-06-06 10:09:56 2745 1829
2013-06-06 10:09:57 2745 1918
2013-06-06 10:09:57 2745 1082
2013-06-06 10:10:05 2745 3000
2013-06-06 10:10:05 2745 1724
2013-06-06 10:10:05 2746 2467
2013-06-06 10:10:05 2745 3000
2013-06-06 10:10:05 2745 2911
2013-06-06 10:10:06 2745 1000
2013-06-06 10:10:06 2745 3000
2013-06-06 10:10:06 2745 2911
2013-06-06 10:10:07 2745 2911
2013-06-06 10:10:07 2745 2911
2013-06-06 10:10:08 2745 2911
2013-06-06 10:10:08 2745 2911
2013-06-06 10:10:09 2745 2911
2013-06-06 10:10:10 2745 2911
2013-06-06 10:10:10 2745 2911
2013-06-06 10:10:11 2745 2911
2013-06-06 10:10:12 2745 2911
2013-06-06 10:10:13 2745 2911
2013-06-06 10:10:13 2748 1877
2013-06-06 10:10:14 2745 2911
2013-06-06 10:10:17 2745 1777
2013-06-06 10:10:42 2746 1430
2013-06-06 10:10:42 2745 1134
2013-06-06 10:10:43 2746 1430
2013-06-06 10:10:44 2745 1553
2013-06-06 10:10:44 2745 1430
2013-06-06 10:10:44 2745 1282
2013-06-06 10:10:44 2745 8866
2013-06-06 10:10:45 2745 1926
Hopefully this works....
full.xts.obj is the xts object you create using all the data you have for my.frame1 converted into an xts object
your.code.func <- function(my.xts){
my.val <- my.xts[,1]*my.xts[,2]
my.cumvol <- cumsum(my.xts[,2])
my.cumval <- cumsum(my.val[,1])
my.vwap <- my.cumval/my.cumvol
my.ratio <- (my.xts[,1]/my.vwap - 1)*10000
op <- par(mfrow=c(1,2))
plot(my.ratio)
plot(my.xts[,1])
lines(my.vwap)
par(op)
}
data.list <- split(full.xts.obj,'days')
for(i in c(1:(length(data.list))){
tmp <- data.list[[i]]
your.code.func(tmp)
}
Related
I am trying to print leap years between user given years as a vector.
leap_years<-function(V1,V2){
for(i in V1:V2){
if(i%%4==0 && i%%100!=0 ||i%%400==0)
{print(i)}}}
This gives me right years but as a single units. How to get them in single vector?
If you want to store the results in a vector you will need to store them.
Start by initiating an empty vector, and then instead of printing just append the value to the vector.
I modified your code in that sense:
leap_years<-function(V1,V2){
leap_vect=NULL
for(i in V1:V2){
if(i%%4==0 && i%%100!=0 ||i%%400==0){
leap_vect = append(leap_vect,i)
}
}
return(leap_vect)
}
Actually you can vectorize operations in function leap_years like below
leap_years <- function(V1,V2) {
v <- V1:V2
v[(v%%4==0 & v%%100!=0) | v%%400==0]
}
such that
> leap_years(1000,2000)
[1] 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044 1048 1052 1056 1060
[16] 1064 1068 1072 1076 1080 1084 1088 1092 1096 1104 1108 1112 1116 1120 1124
[31] 1128 1132 1136 1140 1144 1148 1152 1156 1160 1164 1168 1172 1176 1180 1184
[46] 1188 1192 1196 1200 1204 1208 1212 1216 1220 1224 1228 1232 1236 1240 1244
[61] 1248 1252 1256 1260 1264 1268 1272 1276 1280 1284 1288 1292 1296 1304 1308
[76] 1312 1316 1320 1324 1328 1332 1336 1340 1344 1348 1352 1356 1360 1364 1368
[91] 1372 1376 1380 1384 1388 1392 1396 1404 1408 1412 1416 1420 1424 1428 1432
[106] 1436 1440 1444 1448 1452 1456 1460 1464 1468 1472 1476 1480 1484 1488 1492
[121] 1496 1504 1508 1512 1516 1520 1524 1528 1532 1536 1540 1544 1548 1552 1556
[136] 1560 1564 1568 1572 1576 1580 1584 1588 1592 1596 1600 1604 1608 1612 1616
[151] 1620 1624 1628 1632 1636 1640 1644 1648 1652 1656 1660 1664 1668 1672 1676
[166] 1680 1684 1688 1692 1696 1704 1708 1712 1716 1720 1724 1728 1732 1736 1740
[181] 1744 1748 1752 1756 1760 1764 1768 1772 1776 1780 1784 1788 1792 1796 1804
[196] 1808 1812 1816 1820 1824 1828 1832 1836 1840 1844 1848 1852 1856 1860 1864
[211] 1868 1872 1876 1880 1884 1888 1892 1896 1904 1908 1912 1916 1920 1924 1928
[226] 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980 1984 1988
[241] 1992 1996 2000
I'm presently working with data which is UTF-8 encoded. I am trying to apply a grepl to see if certain strings are present in a body of text or not. But to no avail I was unable to get my desired result. (Note: I do know that the string is present).
To use an example, I have the following string of integers:
(NOTE: Despite being numbers the structure of the data is a single character string)
>tancd[[1]]
[1] "1512 1463 1489 1460 1468 1497 32 1488 1457 1500 1460 1497 1506 1462 1494 1462 1512"
I want to see if the above is present in the string below (Which it is):
[Note: the desired string is framed with ***- its not in the actual data]
> dat
[1] "1488 32 1502 1461 1488 1461 1497 1502 1464 1514 1463 1497 32 1511 1493 1465 1512 1460 1497 1503 32 1488 1462 1514 32 1513 1473 1456 1502 1463 1506 32 1489 1468 1456 1506 1463 1512 1456 1489 1460 1497 1514 46 32 1502 1460 1513 1468 1473 1464 1506 1464 1492 32 1513 1473 1462 1492 1463 1499 1468 1465 1492 1458 1504 1460 1497 1501 32 1504 1460 1499 1456 1504 1464 1505 1460 1497 1501 32 1500 1462 1488 1457 1499 1465 1500 32 1489 1468 1460 1514 1456 1512 1493 1468 1502 1464 1514 1464 1503 44 32 1506 1463 1491 32 1505 1493 1465 1507 32 1492 1464 1488 1463 1513 1473 1456 1502 1493 1468 1512 1464 1492 32 1492 1464 1512 1460 1488 1513 1473 1493 1465 1504 1464 1492 44 32 1491 1468 1460 1489 1456 1512 1461 1497 32 ***1512 1463 1489 1468 1460 1497 32 1488 1457 1500 1460 1497 1506 1462 1494 1462 1512*** 46 32 1493 1463 1495 1458 1499 1464 1502 1460 1497 1501 32 1488 1493 1465 1502 1456 1512 1460 1497 1501 44 32 1506 1463 1491 32 1495 1458 1510 1493 1465 1514 46 32 1512 1463 1489 1468 1464 1503 32 1490 1468 1463 1502 1456 1500 1460 1497 1488 1461 1500 32 1488 1493 1465 1502 1461 1512 44 32 1506 1463 1491 32 1513 1473 1462 1497 1468 1463 1506 1458 1500 1462 1492 32 1506 1463 1502 1468 1493 1468 1491 32 1492 1463 1513 1468 1473 1464 1495 1463 1512 46 32 1502 1463 1506 1458 1513 1474 1462 1492 32 1513 1473 1462 1489 1468 1464 1488 1493 1468 32 1489 1464 1504 1464 1497 1493 32 1502 1460 1489 1468 1461 1497 1514 32 1492 1463 1502 1468 1460 1513 1473 1456 1514 1468 1462 1492 44 32 1488 1464 1502 1456 1512 1493 1468 32 1500 1493 1465 44 32 1500 1465 1488 32 1511 1464 1512 1460 1497 1504 1493 1468 32 1488 1462 1514 32 1513 1473 1456 1502 1463 1506 46 32 1488 1464 1502 1463 1512 32 1500 1464 1492 1462 1501 44 32 1488 1460 1501 32 1500 1465 1488 32 1506 1464 1500 1464 1492 32 1506 1463 1502 1468 1493 1468 1491 32 1492 1463 1513 1468 1473 1463 1495 1463 1512 44 32 1495 1463 1497 1468 1464 1489 1460 1497 1503 32 1488 1463 1514 1468 1462 1501 32 1500 1460 1511 1456 1512 1493 1465 1514 46 32 1493 1456 1500 1465 1488 32 1494 1493 1465 32 1489 1468 1460 1500 1456 1489 1463 1491 44 32 1488 1462 1500 1468 1464 1488 32 1499 1468 1464 1500 32 1502 1463 1492 32 1513 1468 1473 1462 1488 1464 1502 1456 1512 1493 1468 32 1495 1458 1499 1464 1502 1460 1497 1501 32 1506 1463 1491 32 1495 1458 1510 1493 1465 1514 44 32 1502 1460 1510 1456 1493 1464 1514 1464 1503 32 1506 1463 1491 32 1513 1473 1462 1497 1468 1463 1506 1458 1500 1462 1492 32 1506 1463 1502 1468 1493 1468 1491 32 1492 1463 1513 1468 1473 1464 1495 1463 1512 46 32 1492 1462 1511 1456 1496 1461 1512 32 1495 1458 1500 1464 1489 1460 1497 1501 32 1493 1456 1488 1461 1489 1464 1512 1460 1497 1501 44 32 1502 1460 1510 1456 1493 1464 1514 1464 1503 32 1506 1463 1491 32 1513 1473 1462 1497 1468 1463 1506 1458 1500 1462 1492 32 1506 1463 1502 1468 1493 1468 1491 32 1492 1463 1513 1468 1473 1464 1495 1463 1512 46 32 1493 1456 1499 1464 1500 32 1492 1463 1504 1468 1462 1488 1457 1499 1464 1500 1460 1497 1501 32 1500 1456 1497 1493 1465 1501 32 1488 1462 1495 1464 1491 44 32 1502 1460 1510 1456 1493 1464 1514 1464 1503 32 1506 1463 1491 32 1513 1473 1462 1497 1468 1463 1506 1458 1500 1462 1492 32 1506 1463 1502 1468 1493 1468 1491 32 1492 1463 1513 1468 1473 1464 1495 1463 1512 46 32 1488 1460 1501 32 1499 1468 1461 1503 44 32 1500 1464 1502 1468 1464 1492 32 1488 1464 1502 1456 1512 1493 1468 32 1495 1458 1499 1464 1502 1460 1497 1501 32 1506 1463 1491 32 1495 1458 1510 1493 1465 1514 44 32 1499 1468 1456 1491 1461 1497 32 1500 1456 1492 1463 1512 1456 1495 1460 1497 1511 32 1488 1462 1514 32 1492 1464 1488 1464 1491 1464 1501 32 1502 1460 1503 32 1492 1464 1506 1458 1489 1461 1512 1464 1492 58"
My Problem:
When I use the grepl I get the following result:
> grepl(tancd[[1]],dat)
[1] FALSE
Similarly if I use string_detect() from the stringr package I get the same result:
> str_detect(tancd[[1]],dat)
[1] FALSE
Is there a way to grepl this set and get the correct desired result?
The string you have between the astrisks is different than the string you are looking for.
string in dat : "1512 1463 1489 1468 1460 1497 32 1488 1457 1500 1460 1497 1506 1462 1494 1462 1512"
string in tancd : "1512 1463 1489 1460 1468 1497 32 1488 1457 1500 1460 1497 1506 1462 1494 1462 1512"
You'll notice that 1468 and 1460 are inverted in tancd. If you fix tancd it should work.
tancd[[1]] <- "1512 1463 1489 1468 1460 1497 32 1488 1457 1500 1460 1497 1506 1462 1494 1462 1512"
grepl(dat, pattern=tancd[[1]])
I have created following heatmap of days in the week and hours in a day;
This is table of values, from which was the map created;
0 1 2 3 4 5 6 7 8 9 10 11 12 13
nedeľa 2028 1236 1019 838 607 461 478 483 615 864 884 787 1192 789
piatok 1873 932 743 560 473 602 839 1203 1268 1286 938 822 1207 857
pondelok 1900 825 712 527 415 542 772 1123 1323 1235 971 737 1129 824
sobota 2050 1267 985 836 652 508 541 650 858 1039 946 789 1204 767
streda 1814 790 619 469 396 561 862 1140 1329 1237 947 763 1225 804
štvrtok 1856 816 696 508 400 534 799 1135 1298 1301 932 731 1093 752
utorok 1691 777 603 464 414 520 845 1118 1175 1174 948 786 1108 762
14 15 16 17 18 19 20 21 22 23
nedeľa 959 1037 1083 1160 1389 1342 1706 1696 2079 1584
piatok 937 1140 1165 1318 1623 1652 1736 1881 2308 1921
pondelok 958 1059 1136 1252 1518 1503 1622 1815 2009 1490
sobota 963 1086 1055 1084 1348 1390 1570 1702 2078 1750
streda 863 1075 1076 1289 1580 1507 1718 1748 2093 1511
štvrtok 831 1044 1131 1258 1510 1537 1668 1776 2134 1579
utorok 908 1071 1090 1274 1553 1496 1696 1816 2044 1458
I wonder if there is some easy and elegant way how to swap color range, so that high values are represented by red color and other way around.
I've used this function;
heatmap (myMatrix, Colv=NA, Rowv=NA)
The default colors for the heatmap function are actually set by the image() function and are
col=heat.colors(12)
If you want to reverse them, just use pass
heatmap(..., col=rev(heat.colors(12)))
where ... are the rest of the parameters you need to pass.
I have a csv data file https://github.com/user59036/first/blob/master/dataFraserRiver.csv
and I want to create two data sets using the function window.
The first set of data is from January 1990 to December 2008 and the second set is from January 2009 to December 2010.
datRiver <- read.csv("dataFraserRiver.csv")
datRiverTest <-ts(datRiver)
window(datRiver,start=c(1990,1),end = c(2008,12),frequency=12)
I kept getting an error:
'frequency' not changed'end' value not changedError in
window.default(x, ...) : 'start' cannot be after 'end'
How should I change my code to get the data? Thanks for any help.
It can work if you remove the year column. And convert the data.frame to a transposed matrix. And lastly converting to vector.
datvec <- c(t(datRiver[-1]))
dat_ts <- ts(datvec, start = c(1912, 1), end = c(2010, 12), frequency = 12)
window(dat_ts, start = c(1990, 1), end = c(2008, 12))
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
# 1990 1210 841 926 3000 5050 8760 6270 3340 1790 1520 2110 1190
# 1991 867 1560 1060 2690 5810 6910 6270 4330 2730 1560 1370 1110
# 1992 1090 1180 1770 2950 4800 5940 3870 2550 1960 2190 1740 853
# 1993 662 668 751 1940 5620 4930 3640 2900 1760 1060 1050 828
# 1994 819 709 1160 3600 5970 5960 5200 2900 1580 1350 857 814
# 1995 654 837 765 1900 4450 5880 4030 3500 1870 1730 2080 1560
# 1996 1120 947 1120 3080 4070 6750 6400 3780 2610 2130 1860 1210
# 1997 1040 938 1080 2580 7420 9580 7310 4440 2490 3270 2510 1320
Why remove the first column? Because how does the function know that those years are not actual values? Why convert to matrix? Because the list format of data frames make it problematic when using as a time-series. Why transpose? Because when a matrix is turned into a vector it is ordered by column, but the data is organized by row. Why convert to vector? Because this format makes for the clearest input for creating time-series objects with ts.
It works for me. Make sure that:
1- You remove the Year column with [,-1] when creating the ts object
2- You specify the start, end and frequency in your ts call
3- You use datRiverTest in your window call instead of datRiver
datRiverTest <-ts(c(t(datRiver[,-1])), start=c(1912,1), end=c(2010,12), frequency = 12)
window(datRiverTest,start=c(1990,1),end = c(2008,12),frequency=12)
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1990 1210 841 926 3000 5050 8760 6270 3340 1790 1520 2110 1190
1991 867 1560 1060 2690 5810 6910 6270 4330 2730 1560 1370 1110
1992 1090 1180 1770 2950 4800 5940 3870 2550 1960 2190 1740 853
1993 662 668 751 1940 5620 4930 3640 2900 1760 1060 1050 828
1994 819 709 1160 3600 5970 5960 5200 2900 1580 1350 857 814
1995 654 837 765 1900 4450 5880 4030 3500 1870 1730 2080 1560
1996 1120 947 1120 3080 4070 6750 6400 3780 2610 2130 1860 1210
1997 1040 938 1080 2580 7420 9580 7310 4440 2490 3270 2510 1320
1998 949 922 1050 1790 5730 4990 3910 2680 1490 1390 1180 839
1999 827 738 759 2390 5220 8910 8640 5250 2630 1610 1940 1230
2000 872 751 728 1980 4270 6740 6520 3730 2840 1920 1760 812
2001 754 580 537 1220 3620 5740 5110 3890 2000 1480 1600 924
2002 922 719 576 1800 5490 9300 6740 2960 2330 2160 1300 863
2003 698 665 735 2060 3700 6450 4430 2720 1630 2280 1560 836
2004 752 656 801 2440 4390 5380 3860 2530 3210 2270 2450 1690
2005 1780 2200 1730 2370 5470 6110 5040 2830 1900 2420 1970 1240
2006 1410 985 814 1910 4470 5900 3750 2270 1380 924 1410 860
2007 766 733 1420 2780 5500 8270 6100 3260 2040 2340 2400 1390
2008 935 847 848 1100 5990 7200 4980 3300 2370 1510 1750 1100
I have the following table, which gives the number of earthquakes in each year (row) by month (column).
> tmp=table(quakes$year,quakes$mon)
> tmp
0 1 2 3 4 5 6 7 8 9 10 11
1973 388 453 451 508 375 533 496 392 349 424 400 406
1974 386 384 385 388 456 414 491 501 385 432 354 420
1975 435 374 397 439 449 629 461 434 386 404 440 470
1976 677 478 474 430 612 514 561 533 600 485 463 481
1977 453 355 508 519 460 477 416 541 449 523 585 489
1978 499 449 730 533 550 578 524 480 535 458 526 566
1979 485 444 771 662 705 661 590 597 514 635 549 549
1980 530 530 668 654 969 779 668 472 452 614 549 463
1981 501 506 545 547 538 524 662 587 690 561 518 650
1982 655 527 632 602 630 658 603 639 640 761 628 772
1983 909 683 775 847 1028 743 823 902 727 770 793 842
1984 798 732 872 943 795 721 782 820 994 947 1056 1033
1985 1016 839 1140 1078 1146 989 1066 1136 1095 1115 1162 1333
1986 1050 867 1217 944 1368 1046 1256 1035 912 1086 1066 871
1987 834 860 1003 884 891 871 959 943 952 1022 1035 1036
1988 990 957 1127 1123 1121 975 1095 1160 929 1079 1092 1063
1989 1133 1106 1144 1297 1235 1060 1175 1312 1200 1458 1137 1305
1990 1247 1176 1404 1489 1431 1321 1713 1496 1160 1277 1307 1569
1991 1476 1226 1369 1388 1387 1380 1327 1378 1253 1530 1301 1469
1992 1362 1292 1622 1715 1915 1649 1941 1722 1518 1501 1653 1634
1993 1435 1428 1821 1691 1970 1767 2502 1957 1903 1852 1628 1522
1994 2095 1409 1466 1520 1760 1702 1473 1494 1625 1889 1673 1265
1995 1656 1590 1444 1798 1931 1691 1445 1574 1640 2005 1917 2316
1996 2297 2310 1513 1290 1335 1675 1545 1450 1615 1604 1690 1614
1997 1441 1570 1890 1919 1618 1269 1582 1463 1463 1645 1892 2120
1998 1905 1592 1773 2021 2068 1786 1971 1776 1724 1749 1761 1562
1999 1752 1740 2093 1713 2145 1891 1679 1628 1487 1799 1584 1321
2000 1451 1340 1587 1702 1710 1941 2221 2125 1724 1863 2735 1857
2001 1945 2007 1856 2091 1724 2091 2039 1915 1817 2124 1917 2008
2002 2101 1996 2291 2202 1981 2126 2001 2091 2733 2411 3316 2205
2003 2053 2139 2604 2475 2526 2950 2655 2841 3030 2794 2709 2643
2004 2680 2861 2866 2692 3157 2767 2090 2274 2313 2168 2449 2883
2005 3253 2096 2842 3028 2562 2492 2340 2215 2347 2887 2176 2245
2006 2086 2007 2509 2739 2738 2445 2548 2405 2157 2399 3128 2407
2007 2822 1954 2361 3206 2351 2257 2566 2779 2682 2324 2072 2311
2008 2333 2666 2732 2595 3303 3024 2743 2795 2096 2726 2337 2427
2009 1512 1266 1223 1171 1124 1158 1162 1355 1112 1623 1085 1034
2010 1371 1630 2032 2120 1402 1419 2747 1885 1548 1550 1651 2186
Then following two commands give me two different plots, the first for the 1973 time series and the second for the 2010 time series:
> dim(tmp)
[1] 38 12
> plot(tmp[1,], type="l")
> plot(tmp[38,], type="l")
I want to combine and show both of these time series on the same plot. Is there a way to plot rows from the table above on the same plot and at the same time identify each time series by the year (row label)?
matplot is good for this sort of thing:
Reverse your rows and columns of your table:
tmp <- table(quakes$mon,quakes$year)
# 1973 1974 1975 1976 1977 1978
#0 388 386 435 677 453 499
#1 453 384 374 478 355 449
#2 451 385 397 474 508 730
#3 508 388 439 430 519 533
#etc
Then use matplot:
vars <- c(1,6)
matplot(tmp[,vars], type="l", lty=1)
legend("topright", colnames(tmp)[vars], lty=1, col=seq_along(vars))
As a general rule I try not to plot using tables, even though it makes sense for a person to read the data that way.
library(ggplot2)
ggplot(data.frame(tmp)) +
geom_line(aes(x = Var2, y = Freq, group = Var1, col = Var1))
The ggplot2 library is great for this sort of group plotting exercise, though it can take a little bit of input to get used to.
It's probably a bad idea to call Var1 and Var2 (which are created when I coerce the table to a data.frame). You can avoid this by aggregating the quakes data frame first, then calling the plot on that.