Rolling window in R - r

I have part of this code which is the initial step of a rolling iteration
for (u in 1:5605){
for (j in 1:498){
work.on.col = j
work.dt = data[u:(u+365),work.on.col] # u:(u+365-1) considers the rolling window
}
}
So basically, this makes a rolling window from u == 1 to u == 5605 for each j. However, this is a step by step (or in this case: a day by day) rolling algorithm. What I want to change it to, is to do so
By 30 days: instead or rolling day by day, it should roll every 30 days (by this I meant here that the rolling window will REMOVE 30 days, then add 30 days to the window, thus rolling the 365 day-long window). OR
By identifying the month. For example, first iteration will start with daily data from January 2000 to January 2001. And the next iteration will start with February 2000 to February 2001, and so on (this approach is more preferable).
Data
> dput(data)
structure(list(Dates = structure(c(820454400, 820540800, 820627200,
820713600, 820800000, 821059200, 821145600, 821232000, 821318400,
821404800, 821664000, 821750400, 821836800, 821923200, 822009600,
822268800, 822355200, 822441600, 822528000, 822614400, 822873600,
822960000, 823046400, 823132800, 823219200, 823478400, 823564800,
823651200, 823737600, 823824000, 824083200, 824169600, 824256000,
824342400, 824428800, 824688000, 824774400, 824860800, 824947200,
825033600, 825292800, 825379200, 825465600, 825552000, 825638400,
825897600, 825984000, 826070400, 826156800, 826243200, 826502400,
826588800, 826675200, 826761600, 826848000, 827107200, 827193600,
827280000, 827366400, 827452800, 827712000, 827798400, 827884800,
827971200, 828057600, 828316800, 828403200, 828489600, 828576000,
828662400, 828921600, 829008000, 829094400, 829180800, 829267200,
829526400, 829612800, 829699200, 829785600, 829872000, 830131200,
830217600, 830304000, 830390400, 830476800, 830736000, 830822400,
830908800, 830995200, 831081600, 831340800, 831427200, 831513600,
831600000, 831686400, 831945600, 832032000, 832118400, 832204800,
832291200, 832550400, 832636800, 832723200, 832809600, 832896000,
833155200, 833241600, 833328000, 833414400, 833500800, 833760000,
833846400, 833932800, 834019200, 834105600, 834364800, 834451200,
834537600, 834624000, 834710400, 834969600, 835056000, 835142400,
835228800, 835315200, 835574400, 835660800, 835747200, 835833600,
835920000, 836179200, 836265600, 836352000, 836438400, 836524800,
836784000, 836870400, 836956800, 837043200, 837129600, 837388800,
837475200, 837561600, 837648000, 837734400, 837993600, 838080000,
838166400, 838252800, 838339200, 838598400, 838684800, 838771200,
838857600, 838944000, 839203200, 839289600, 839376000, 839462400,
839548800, 839808000, 839894400, 839980800, 840067200, 840153600,
840412800, 840499200, 840585600, 840672000, 840758400, 841017600,
841104000, 841190400, 841276800, 841363200, 841622400, 841708800,
841795200, 841881600, 841968000, 842227200, 842313600, 842400000,
842486400, 842572800, 842832000, 842918400, 843004800, 843091200,
843177600, 843436800, 843523200, 843609600, 843696000, 843782400,
844041600, 844128000, 844214400, 844300800, 844387200, 844646400,
844732800, 844819200, 844905600, 844992000, 845251200, 845337600,
845424000, 845510400, 845596800, 845856000, 845942400, 846028800,
846115200, 846201600, 846460800, 846547200, 846633600, 846720000,
846806400, 847065600, 847152000, 847238400, 847324800, 847411200,
847670400, 847756800, 847843200, 847929600, 848016000, 848275200,
848361600, 848448000, 848534400, 848620800, 848880000, 848966400,
849052800, 849139200, 849225600, 849484800, 849571200, 849657600,
849744000, 849830400, 850089600, 850176000, 850262400, 850348800,
850435200, 850694400, 850780800, 850867200, 850953600, 851040000,
851299200, 851385600, 851472000, 851558400, 851644800, 851904000,
851990400, 852076800, 852163200, 852249600, 852508800, 852595200,
852681600, 852768000, 852854400, 853113600, 853200000, 853286400,
853372800, 853459200, 853718400, 853804800, 853891200, 853977600,
854064000, 854323200, 854409600, 854496000, 854582400, 854668800,
854928000, 855014400, 855100800, 855187200, 855273600, 855532800,
855619200, 855705600, 855792000, 855878400, 856137600, 856224000,
856310400, 856396800, 856483200, 856742400, 856828800, 856915200,
857001600, 857088000, 857347200, 857433600, 857520000, 857606400,
857692800, 857952000, 858038400, 858124800, 858211200, 858297600,
858556800, 858643200, 858729600, 858816000, 858902400, 859161600,
859248000, 859334400, 859420800, 859507200, 859766400, 859852800,
859939200, 860025600, 860112000, 860371200, 860457600, 860544000,
860630400, 860716800, 860976000, 861062400, 861148800, 861235200,
861321600, 861580800, 861667200, 861753600, 861840000, 861926400,
862185600, 862272000, 862358400, 862444800, 862531200, 862790400,
862876800, 862963200, 863049600, 863136000, 863395200, 863481600,
863568000, 863654400, 863740800, 8.64e+08, 864086400, 864172800,
864259200, 864345600, 864604800, 864691200, 864777600, 864864000,
864950400, 865209600, 865296000, 865382400, 865468800, 865555200,
865814400, 865900800, 865987200, 866073600, 866160000, 866419200,
866505600, 866592000, 866678400, 866764800, 867024000, 867110400,
867196800, 867283200, 867369600, 867628800, 867715200, 867801600,
867888000, 867974400, 868233600, 868320000, 868406400, 868492800,
868579200, 868838400, 868924800, 869011200, 869097600, 869184000,
869443200, 869529600, 869616000, 869702400, 869788800, 870048000,
870134400, 870220800, 870307200, 870393600, 870652800, 870739200,
870825600, 870912000, 870998400, 871257600, 871344000, 871430400,
871516800, 871603200, 871862400, 871948800, 872035200, 872121600,
872208000, 872467200, 872553600, 872640000, 872726400, 872812800,
873072000, 873158400, 873244800, 873331200, 873417600, 873676800,
873763200, 873849600, 873936000, 874022400, 874281600, 874368000,
874454400, 874540800, 874627200, 874886400, 874972800, 875059200,
875145600, 875232000, 875491200, 875577600, 875664000, 875750400,
875836800, 876096000, 876182400, 876268800, 876355200, 876441600,
876700800, 876787200, 876873600, 876960000, 877046400, 877305600,
877392000, 877478400, 877564800, 877651200, 877910400, 877996800,
878083200, 878169600, 878256000, 878515200, 878601600, 878688000,
878774400, 878860800, 879120000, 879206400, 879292800, 879379200,
879465600, 879724800, 879811200, 879897600, 879984000, 880070400,
880329600, 880416000, 880502400, 880588800, 880675200, 880934400,
881020800, 881107200, 881193600, 881280000, 881539200, 881625600,
881712000, 881798400, 881884800, 882144000, 882230400, 882316800,
882403200, 882489600, 882748800, 882835200, 882921600, 883008000,
883094400, 883353600, 883440000, 883526400, 883612800, 883699200,
883958400, 884044800, 884131200, 884217600, 884304000, 884563200,
884649600, 884736000, 884822400, 884908800, 885168000, 885254400,
885340800, 885427200, 885513600, 885772800, 885859200, 885945600,
886032000, 886118400, 886377600, 886464000, 886550400, 886636800,
886723200, 886982400, 887068800, 887155200, 887241600, 887328000,
887587200, 887673600, 887760000, 887846400, 887932800, 888192000,
888278400, 888364800, 888451200, 888537600, 888796800, 888883200,
888969600, 889056000, 889142400, 889401600, 889488000, 889574400,
889660800, 889747200, 890006400, 890092800, 890179200, 890265600,
890352000, 890611200, 890697600, 890784000, 890870400, 890956800,
891216000, 891302400, 891388800, 891475200, 891561600, 891820800,
891907200, 891993600, 892080000, 892166400, 892425600, 892512000,
892598400, 892684800, 892771200, 893030400, 893116800, 893203200,
893289600, 893376000, 893635200, 893721600, 893808000, 893894400,
893980800, 894240000, 894326400, 894412800, 894499200, 894585600,
894844800, 894931200, 895017600, 895104000, 895190400, 895449600,
895536000, 895622400, 895708800, 895795200, 896054400, 896140800,
896227200, 896313600, 896400000, 896659200, 896745600, 896832000,
896918400, 897004800, 897264000, 897350400, 897436800, 897523200,
897609600, 897868800, 897955200, 898041600, 898128000, 898214400,
898473600, 898560000, 898646400, 898732800, 898819200, 899078400,
899164800, 899251200, 899337600, 899424000, 899683200, 899769600,
899856000, 899942400, 900028800, 900288000, 900374400, 900460800,
900547200, 900633600, 900892800, 900979200, 901065600, 901152000,
901238400, 901497600, 901584000, 901670400, 901756800, 901843200,
902102400, 902188800, 902275200, 902361600, 902448000, 902707200,
902793600, 902880000, 902966400, 903052800, 903312000, 903398400,
903484800, 903571200, 903657600, 903916800, 904003200, 904089600,
904176000, 904262400, 904521600, 904608000, 904694400, 904780800,
904867200), class = c("POSIXct", "POSIXt"), tzone = "UTC"), `1` = c(9.3424,
9.3424, 9.3983, 9.5102, 9.3144, 9.4822, 9.3983, 9.0537, 9.1381,
9.1099, 9.0537, 8.9975, 8.7444, 8.8007, 8.8007, 8.7163, 8.8569,
8.9412, 8.9131, 9.0537, 9.0537, 9.4474, 9.5036, 9.6723, 9.6723,
9.7566, 9.5879, 9.7566, 9.841, 9.8129, 9.9535, 9.841, 9.6442,
9.8972, 9.7285, 9.7285, 9.6161, 9.2787, 9.3911, 9.363, 9.2224,
9.1099, 9.2787, 9.3911, 9.4474, 9.841, 9.7285, 9.7004, 9.8691,
9.5598, 9.5036, 9.5317, 9.363, 9.4755, 9.2224, 9.1099, 9.2505,
9.1381, 9.2787, 9.2224, 9.1381, 9.2224, 9.1099, 9.1381, 9.1662,
9.4192, 9.3911, 9.3911, 9.4474, 9.4474, 9.7004, 9.5317, 9.2787,
9.3326, 9.3892, 9.3326, 9.2761, 9.2195, 9.2761, 9.4458, 9.3609,
9.4458, 9.163, 9.2761, 9.1912, 9.3044, 9.1912, 9.1912, 9.0215,
8.9084, 8.965, 8.9367, 8.9367, 8.9933, 9.0781, 9.2478, 9.163,
9.2761, 9.2478, 9.3609, 9.3892, 9.3609, 9.5023, 9.3892, 9.672,
9.672, 9.5872, 9.5306, 9.7003, 9.7568, 9.6437, 9.672, 9.9265,
9.8417, 9.87, 9.8134, 9.7568, 9.672, 9.7568, 9.7003, 9.7286,
9.7851, 9.7851, 9.672, 9.7851, 9.7851, 9.8417, 9.8134, 9.8983,
9.8417, 9.8417, 9.8134, 9.87, 9.87, 9.672, 9.5306, 9.4175, 9.5872,
9.896, 10.0666, 9.896, 9.8676, 9.7538, 10.2657, 10.095, 10.095,
9.9244, 9.9529, 10.0382, 10.1519, 10.0382, 10.0666, 10.0097,
10.1235, 10.2941, 10.351, 10.4363, 10.2941, 10.4079, 10.5785,
10.55, 10.4647, 10.4363, 10.5216, 10.7775, 10.806, 10.806, 10.6922,
10.8344, 10.6922, 10.5785, 10.55, 10.4363, 10.2941, 10.2657,
10.2657, 10.351, 10.4363, 10.5216, 10.5785, 10.6922, 10.6922,
10.6353, 10.7207, 10.8628, 10.9197, 10.8913, 10.806, 11.005,
10.8344, 10.8628, 10.9197, 11.005, 11.0619, 11.2041, 11.2041,
11.2325, 11.2325, 11.2894, 11.46, 11.46, 11.5453, 11.2325, 11.1734,
11.5163, 11.8306, 11.7163, 11.6877, 11.9164, 12.0878, 11.8878,
11.6306, 11.602, 11.6592, 11.4306, 11.3448, 11.5734, 11.4306,
11.5734, 11.4306, 11.4877, 11.6877, 12.0592, 12.0878, 12.2021,
12.1735, 12.0021, 12.0021, 12.2307, 12.2307, 12.1735, 12.2878,
12.3164, 12.5165, 12.7451, 13.0308, 12.8594, 12.7451, 12.7451,
12.7451, 12.6308, 12.4307, 12.1735, 12.0878, 12.0592, 12.3164,
12.1735, 11.9449, 11.6877, 11.7449, 11.5449, 11.602, 11.5734,
11.9735, 12.0592, 12.0021, 12.0592, 12.0592, 12.0878, 12.0878,
11.9449, 11.602, 11.602, 11.5163, 11.7449, 11.6306, 11.8878,
11.7163, 11.8306, 12.0592, 12.0284, 12.258, 12.2293, 12.7173,
12.6599, 12.6599, 12.6599, 12.947, 12.4877, 12.5164, 12.3154,
12.2867, 12.2006, 12.459, 12.4877, 12.459, 12.5738, 12.4303,
12.5451, 12.8035, 12.8322, 12.8609, 13.0905, 13.435, 13.3776,
13.3776, 13.6934, 13.5212, 13.3202, 13.2054, 13.3489, 13.435,
13.2915, 13.2054, 12.9183, 13.0905, 12.8322, 13.4924, 13.2054,
13.5499, 13.8082, 13.5786, 13.5786, 13.435, 13.2915, 13.7508,
13.6647, 13.4924, 13.3489, 13.2628, 13.6647, 13.5499, 13.6073,
13.3202, 13.3202, 12.8896, 12.7748, 12.8322, 12.8322, 13.1193,
12.9183, 12.9757, 12.7173, 13.0044, 12.4062, 12.7524, 13.0409,
13.4448, 13.5025, 13.3871, 13.5602, 13.9353, 13.8199, 13.6756,
13.5891, 13.6468, 13.993, 14.0796, 14.0796, 14.3392, 14.6277,
14.5989, 14.4546, 14.4258, 14.4835, 14.7143, 14.8297, 14.9739,
15.4933, 15.2913, 15.2625, 15.0028, 14.6277, 14.6277, 14.772,
14.772, 14.772, 14.57, 14.5412, 14.5412, 14.4546, 14.5989, 14.5123,
14.4546, 14.772, 15.0028, 14.9162, 15.1471, 15.3202, 15.3779,
15.2336, 15.0894, 14.9451, 15.3779, 15.4067, 14.9739, 15.7097,
15.4789, 15.2048, 15.6375, 15.4067, 15.2192, 15.349, 15.7962,
15.7962, 15.4789, 15.6231, 15.3346, 15.1759, 14.8776, 14.8921,
15.0369, 14.9935, 14.5879, 14.385, 14.3416, 14.6313, 14.8631,
14.7327, 14.7762, 14.6313, 14.8631, 15.0369, 15.1673, 14.8052,
14.6603, 14.7037, 14.6458, 14.8341, 14.8196, 14.8486, 14.8196,
14.5589, 14.7472, 14.0808, 14.472, 14.2981, 14.3706, 14.1533,
14.0663, 13.8201, 13.8635, 13.9649, 13.878, 13.8925, 13.8925,
14.414, 14.5299, 14.5009, 14.3416, 14.0953, 14.0808, 13.849,
13.7911, 14.3706, 14.3271, 14.7037, 14.6023, 14.7762, 14.9355,
15.0224, 14.921, 14.6313, 14.5589, 14.7327, 14.9355, 14.8196,
14.8486, 14.95, 14.9066, 15.0659, 15.2832, 15.1818, 15.0514,
14.9257, 14.9257, 15.0857, 14.7948, 14.8238, 14.8675, 14.7657,
14.8238, 14.7802, 14.3729, 14.3147, 13.5582, 14.2565, 14.1401,
14.0092, 14.271, 14.4893, 14.6638, 14.8384, 14.562, 14.5329,
14.5911, 14.562, 14.6056, 14.7948, 14.9984, 15.0857, 14.9548,
15.1439, 15.2312, 15.3476, 15.1439, 15.3476, 15.333, 15.333,
15.1293, 15.6531, 15.6821, 15.8713, 15.7258, 15.8276, 15.6821,
15.9585, 15.7694, 15.5512, 15.5076, 15.784, 15.9731, 15.6821,
15.1875, 15.0421, 15.3767, 15.1002, 15.013, 15.013, 15.1875,
15.333, 15.5803, 15.2603, 15.2603, 15.6967, 15.784, 15.5221,
15.624, 15.8422, 15.6531, 16.1913, 16.3123, 16.3415, 16.2247,
16.3708, 16.3708, 16.6774, 16.5168, 16.4438, 16.3269, 16.1663,
16.5168, 16.5752, 16.7943, 16.546, 16.6336, 16.473, 16.4438,
16.5022, 16.7651, 16.7505, 17.1886, 17.1009, 17.2616, 17.3054,
17.3054, 17.5537, 17.4952, 17.2616, 17.3492, 17.5098, 17.4806,
17.466, 17.4076, 17.4806, 17.5683, 17.5391, 17.3346, 17.466,
17.7289, 17.6559, 17.9626, 18.1086, 18.24, 18.1962, 18.2108,
18.2254, 18.0502, 18.0064, 18.1962, 18.0356, 17.9334, 17.4222,
17.6121, 17.6267, 17.32, 17.5975, 17.7581, 18.0794, 18.2692,
18.0356, 17.9918, 17.7289, 17.8457, 17.8457, 17.5653, 17.6387,
17.3601, 16.8909, 17.536, 17.1401, 17.0522, 16.9495, 16.9202,
16.8469, 16.7443, 16.4657, 16.5683, 17.1548, 17.0961, 16.9935,
16.8469, 16.5536, 16.7296, 17.0082, 16.9789, 17.1988, 17.2134,
17.0375, 17.492, 17.712, 17.6093, 17.9319, 17.6826, 17.6826,
17.6826, 17.536, 17.4774, 17.5653, 17.4041, 17.1841, 17.1841,
17.0082, 17.1255, 17.6533, 17.448, 17.448, 17.5947, 17.536, 18.0052,
18.0932, 18.2985, 18.7383, 19.1782, 18.8263, 18.8849, 19.1195,
19.5301, 19.5301, 19.8233, 19.8526, 19.1782, 19.4421, 19.706,
19.706, 20.0579, 19.9406, 20.0872, 20.1459, 20.5858, 20.9201,
21.1849, 20.7141, 20.9789, 21.126, 21.0083, 20.4199, 20.9789,
20.4787, 20.5964, 20.5964, 20.0374, 20.155, 20.2727, 19.5666,
19.9491, 19.1252, 19.1841, 19.0664, 19.037, 19.037, 19.2723,
19.4783, 19.5372, 19.2429, 19.8902, 20.1256, 20.2139, 20.2433,
20.4199, 20.361, 20.3316, 20.6847, 19.9491, 19.5077, 18.1248,
18.5956, 18.3308, 18.9193, 18.6545), `3` = c(7.8478, 8.1167,
7.6421, 7.4048, 5.3479, 5.3004, 4.8099, 4.8732, 4.9681, 5.0473,
4.6438, 4.6042, 4.5315, 4.5156, 4.7057, 4.9117, 5.0068, 5.1494,
4.9593, 5.0068, 4.88, 4.7533, 4.3096, 4.3413, 4.5631, 4.6107,
4.6265, 4.5156, 4.9117, 4.9117, 4.8008, 4.7216, 4.6978, 4.5394,
4.4681, 4.4681, 4.6899, 4.7691, 4.6741, 4.6582, 4.4839, 4.3413,
4.2779, 4.2463, 4.1353, 4.1037, 4.1512, 4.0244, 4.1195, 4.072,
4.1829, 4.0878, 4.4205, 4.3096, 4.3572, 4.3255, 4.4047, 4.3255,
4.3413, 3.9769, 3.969, 4.1037, 4.0561, 4.0403, 4.0878, 4.167,
4.2051, 4.2527, 4.1257, 4.1257, 4.2685, 4.3003, 4.1733, 4.1575,
4.4272, 4.4272, 4.4907, 4.3955, 4.5383, 4.4431, 4.8001, 4.8398,
5.0778, 5.4586, 5.3634, 5.3, 5.4586, 5.411, 5.2524, 5.2841, 5.1413,
5.173, 5.0937, 4.9985, 5.1095, 5.2682, 5.3952, 5.411, 5.6967,
5.4586, 5.4745, 5.3317, 5.3158, 5.3158, 5.1095, 5.1095, 4.8081,
4.7287, 4.7763, 4.7128, 4.5859, 4.5542, 4.6652, 4.451, 4.4907,
4.6335, 4.6335, 4.57, 4.7287, 4.4431, 4.3479, 4.3637, 4.57, 4.5859,
4.57, 4.4114, 4.3003, 4.4193, 4.4828, 4.5542, 4.5859, 4.578,
4.4812, 4.4812, 4.4653, 4.4335, 4.4335, 4.3064, 4.1316, 4.1316,
3.8217, 3.8297, 4.0045, 4.4812, 4.1634, 4.1475, 4.068, 3.8614,
4.0998, 4.0362, 3.8773, 3.8614, 3.8614, 3.925, 3.925, 3.9091,
4.211, 4.2269, 4.1634, 4.1634, 4.1475, 4.068, 4.1316, 4.1951,
4.1316, 4.0998, 4.3382, 4.7354, 4.5447, 4.5447, 4.4653, 4.5606,
4.7672, 4.5765, 4.4335, 4.4335, 4.4812, 4.3699, 4.2428, 4.2905,
4.3143, 4.1951, 4.2269, 4.1792, 4.0998, 4.1792, 4.2905, 4.2428,
4.5845, 4.8308, 4.7831, 4.6877, 4.4494, 4.9102, 4.8149, 4.7418,
4.7736, 4.694, 4.5508, 4.4235, 4.3042, 4.519, 4.6145, 4.5667,
4.694, 4.7895, 4.71, 4.71, 4.694, 4.6463, 4.5508, 4.3917, 4.4235,
4.4235, 4.3122, 4.2962, 4.1689, 4.3281, 4.4076, 4.3599, 4.2962,
4.2326, 4.3917, 4.4554, 4.4713, 4.694, 4.9645, 5.0998, 5.2191,
5.4101, 5.2828, 5.2828, 5.1396, 5.0759, 5.0282, 5.0282, 5.0282,
5.0282, 5.0282, 5.0282, 5.5215, 5.3623, 5.3942, 5.3464, 5.1555,
5.235, 5.1396, 5.0918, 4.9964, 4.9964, 4.9645, 5.0918, 5.2032,
5.3146, 5.1873, 5.0123, 4.9645, 4.9645, 4.8532, 4.7259, 4.7736,
4.7577, 4.7577, 4.9168, 4.9805, 5.1555, 5.3623, 5.3783, 5.2669,
4.9964, 5.0123, 4.9709, 4.7479, 4.7718, 4.7638, 4.8275, 4.7319,
4.8435, 4.6523, 4.5885, 4.5407, 4.6523, 4.6045, 4.7638, 4.8275,
4.7957, 4.6523, 4.4292, 4.5089, 4.6523, 4.8116, 4.6602, 4.9311,
5.0187, 4.7797, 4.7797, 5.0665, 5.0506, 5.0187, 4.7001, 4.6523,
4.8275, 4.7319, 4.5248, 4.6523, 4.6841, 4.6682, 4.6523, 4.5089,
4.2938, 4.5089, 4.3416, 4.238, 4.238, 4.238, 4.4292, 4.4451,
4.9231, 5.1621, 5.2577, 5.2258, 5.2896, 5.4489, 5.2481, 5.2481,
5.1205, 5.0886, 5.2003, 5.3598, 5.4874, 5.5353, 5.5672, 5.4874,
5.3518, 5.2641, 5.3917, 5.3598, 5.296, 5.296, 5.1365, 5.0886,
5.0727, 5.1365, 5.1843, 4.8334, 4.9291, 4.9131, 4.9929, 5.3279,
5.615, 5.9341, 5.7905, 5.9181, 5.615, 5.5193, 5.5911, 5.5831,
5.4874, 5.6549, 5.7107, 5.631, 5.5911, 5.8703, 5.9022, 5.9181,
5.9181, 6.2371, 6.0936, 5.8064, 5.6948, 5.7666, 5.1684, 5.28,
5.4156, 5.5034, 5.296, 5.296, 5.3279, 5.296, 5.2641, 5.28, 5.304,
5.1365, 4.5941, 4.5782, 4.5542, 4.5303, 4.5144, 4.4984, 4.5064,
4.4745, 4.4346, 4.6324, 4.7442, 4.7442, 4.8081, 4.9199, 4.7762,
4.6164, 4.6324, 4.8161, 4.7921, 4.7762, 4.7362, 4.7362, 4.7362,
4.8081, 4.7362, 4.7442, 4.9119, 4.8241, 4.7762, 4.6971, 4.7287,
4.7762, 4.8078, 4.9659, 5.0925, 5.045, 4.9106, 5.0925, 5.0213,
5.1715, 5.3613, 5.1241, 5.2506, 5.2111, 5.4957, 5.2506, 5.4562,
5.4088, 5.1636, 4.8157, 5.0529, 4.9818, 4.9818, 5.1399, 5.1953,
5.1794, 5.2822, 5.2269, 5.219, 5.1874, 5.6381, 5.7251, 5.733,
5.7092, 5.646, 5.7883, 6.2944, 6.5949, 6.5474, 6.4447, 6.4288,
6.3102, 6.3482, 6.3798, 6.3086, 6.3561, 6.269, 6.4115, 6.4669,
6.4907, 6.5223, 6.3719, 6.5698, 6.5856, 6.5065, 6.3007, 6.0632,
6.3719, 6.3323, 6.2215, 6.2057, 6.2532, 5.8574, 6.1463, 6.182,
6.0793, 6.032, 6.1741, 6.1188, 6.0714, 6.0478, 6.032, 5.9451,
5.953, 5.7162, 5.7793, 6.1267, 6.1583, 5.3214, 5.4319, 5.3846,
5.424, 5.203, 5.3451, 5.274, 5.274, 5.3056, 5.4082, 5.2582, 5.3214,
5.2582, 5.1793, 5.3688, 5.2582, 5.1319, 4.9345, 4.4529, 4.5792,
4.3819, 4.4213, 4.374, 4.6582, 4.6977, 4.5871, 4.6819, 4.6819,
4.7687, 4.8477, 5.083, 5.2173, 5.2173, 5.1225, 5.1936, 5.3675,
5.2885, 5.3359, 5.2173, 5.1541, 5.0909, 4.917, 4.9644, 4.9644,
4.9644, 5.0118, 4.9407, 4.8695, 4.8379, 4.7272, 4.7272, 4.7984,
4.7589, 4.8695, 4.8537, 4.8458, 4.9644, 4.9881, 5.0592, 5.1383,
5.0988, 5.1067, 5.2173, 5.494, 5.494, 5.4782, 5.5415, 5.5494,
5.5336, 5.5454, 5.2806, 5.4071, 5.5098, 5.5889, 5.4545, 5.5177,
5.5494, 5.5256, 5.6284, 5.5177, 5.66, 5.664, 5.6126, 5.5494,
5.6205, 5.5177, 5.3913, 5.5573, 5.4703, 5.4545, 5.5731, 5.6205,
6.0711, 5.7549, 5.7391, 5.7154, 5.7928, 5.9036, 5.9511, 5.8482,
5.6662, 5.69, 5.7374, 5.7374, 5.9353, 5.9115, 6.1094, 6.1252,
6.1648, 6.331, 6.2123, 6.418, 6.2914, 6.4259, 6.3468, 6.1806,
6.4101, 6.3389, 6.3785, 6.3151, 6.2518, 6.236, 6.3072, 6.4101,
5.9828, 5.8245, 5.7374, 5.7295, 5.7454, 5.69, 5.7612, 5.6583,
5.5792, 5.4921, 5.4921, 5.2389, 5.2784, 5.3022, 5.0569, 4.9777,
5.1914, 5.0648, 5.1518, 5.1756, 5.1281, 5.5079, 5.318, 5.1677,
5.0648, 5.0173, 5.2864, 5.2547, 5.1835, 4.9936, 5.0806, 5.1281,
5.3259, 5.6187, 5.3734, 5.413, 5.3734, 5.269, 5.2452, 5.2452,
5.2056, 5.1659, 5.2769, 5.1739, 5.0946, 5.055, 4.9441, 4.9837,
5.0471, 5.0629, 5.1343, 5.0233, 5.0075, 4.9283, 4.8015, 4.8015,
4.7856, 4.5717, 4.3419, 4.0963, 4.0963, 4.0963, 4.0963, 4.0963,
4.0963, 4.0963, 4.0963, 4.0963, 4.0963, 4.0963, 4.0963, 4.0963,
4.0963, 4.0963, 4.0963, 4.0963, 4.0963, 4.0963, 3.407, 3.4466,
3.3278, 3.5021, 3.1693, 3.1455, 3.098), `5` = c(13.4319, 13.6184,
13.805, 13.8983, 13.8983, 13.805, 13.5252, 13.2453, 12.8722,
12.7789, 12.6857, 13.4319, 13.2453, 13.5252, 13.7117, 13.8983,
13.805, 13.8983, 13.805, 13.8983, 13.9915, 14.1781, 14.2154,
14.3089, 14.2154, 14.2154, 14.4024, 14.3089, 14.5895, 14.496,
14.3089, 14.3089, 14.3089, 14.4024, 14.496, 14.496, 14.2154,
14.2154, 14.4024, 14.4024, 14.496, 14.3089, 14.683, 14.4024,
14.496, 14.5895, 14.4024, 14.4024, 14.4024, 13.9348, 13.7478,
14.0284, 14.0284, 14.0284, 13.8413, 13.7478, 13.5607, 13.9348,
13.9348, 13.9348, 14.1219, 13.8413, 13.9348, 13.8413, 13.7478,
13.5607, 13.9348, 14.1219, 14.4024, 14.4024, 13.9348, 13.6543,
13.4672, 13.5607, 13.8413, 14.0284, 13.9348, 14.1219, 14.3089,
14.496, 14.2154, 14.3089, 14.3089, 14.3089, 14.1219, 14.3089,
14.1219, 14.3463, 14.1588, 14.065, 14.2526, 13.8775, 13.7837,
14.065, 14.1588, 14.1588, 14.065, 14.3463, 14.4401, 14.5339,
14.4401, 14.6276, 14.6276, 14.6276, 15.1902, 15.1902, 14.8152,
14.7214, 14.7214, 14.6276, 14.5339, 14.5339, 14.7214, 14.4401,
14.8152, 14.8152, 14.8152, 14.6276, 14.4401, 14.4401, 14.3463,
14.5339, 14.3463, 14.6276, 14.8152, 14.8152, 14.7214, 14.4401,
14.3463, 14.3463, 14.3463, 14.2526, 14.1588, 14.1588, 13.9713,
13.7837, 13.8775, 14.065, 13.9713, 13.3149, 13.0336, 12.9398,
12.5648, 12.8461, 12.6585, 12.5648, 12.6585, 12.6585, 12.6585,
12.8461, 12.7523, 13.1274, 13.3149, 13.3149, 13.5024, 13.5962,
13.5962, 13.915, 13.727, 13.6329, 13.5389, 13.5389, 13.6329,
13.821, 14.0184, 14.3146, 14.3146, 14.3146, 14.512, 14.3146,
14.4133, 14.512, 14.0184, 14.3146, 14.0184, 14.0184, 14.0184,
13.9197, 14.0184, 14.0184, 14.2159, 14.2159, 14.1172, 14.4133,
14.3146, 14.4133, 14.3146, 14.512, 14.8082, 14.7095, 14.6108,
14.7095, 14.9069, 15.0056, 15.0056, 15.2031, 15.1044, 15.4992,
15.4992, 15.7954, 15.9929, 15.598, 15.598, 16.0916, 16.289, 17.1775,
17.0788, 16.6839, 16.5852, 16.9801, 17.4737, 17.375, 17.1775,
16.5852, 16.6839, 16.3877, 16.8813, 16.9801, 17.1775, 17.2762,
17.2762, 17.8686, 17.4144, 17.3155, 17.2166, 17.2166, 17.3155,
17.3155, 17.3155, 17.7113, 17.6123, 17.9092, 17.6123, 17.5134,
17.4144, 17.5134, 17.1176, 17.4144, 17.4144, 17.4144, 16.9197,
17.0187, 16.8208, 16.6229, 16.6229, 16.8208, 17.2166, 17.3155,
17.2166, 16.9197, 16.6229, 17.4144, 17.4144, 17.7113, 17.7113,
18.1071, 18.1071, 18.1071, 18.206, 18.0081, 17.9092, 17.4144,
17.4144, 17.4144, 17.4144, 17.2166, 17.6123, 17.5134, 17.4144,
17.6123, 17.6123, 18.206, 17.8102, 17.3155, 16.9197, 16.8208,
16.6229, 16.5239, 15.8313, 15.7324, 15.2376, 15.8313, 15.7324,
15.8313, 15.6334, 15.6334, 15.4355, 15.2772, 15.2772, 15.178,
15.178, 15.178, 15.178, 15.0788, 15.0788, 15.0788, 15.0788, 14.8804,
14.7812, 15.0788, 15.3764, 15.2772, 15.0788, 14.8804, 14.682,
14.682, 14.5828, 14.4836, 14.3844, 14.8804, 14.9796, 14.8804,
14.7812, 14.3844, 14.3844, 14.5828, 14.3844, 14.0868, 13.8884,
13.9876, 14.2852, 14.2852, 14.4836, 14.3844, 14.3844, 14.186,
14.0868, 13.7892, 13.9876, 13.69, 13.8884, 13.9876, 14.0868,
14.2852, 13.8884, 13.69, 13.7892, 13.9876, 13.9876, 14.2852,
14.2852, 14.4836, 14.4836, 14.5828, 14.5828, 14.186, 14.2852,
14.5828, 14.3844, 14.2852, 14.5828, 14.7812, 15.0193, 14.8204,
14.522, 14.522, 15.0193, 15.4172, 15.5166, 15.7155, 15.6161,
15.6161, 15.815, 15.7155, 16.0139, 16.0139, 15.9145, 15.815,
15.9145, 15.9145, 15.7155, 15.815, 15.6161, 16.4118, 16.5113,
16.6107, 17.4065, 16.6107, 16.4118, 16.2129, 16.0139, 16.5113,
17.4065, 17.2075, 17.5059, 17.6054, 17.7049, 18.1027, 17.7049,
18.8487, 18.6995, 18.4508, 18.6498, 18.9979, 18.9979, 18.5006,
18.6, 18.5006, 18.6498, 18.7492, 18.6, 18.3017, 18.6498, 18.053,
17.6054, 17.8043, 18.3017, 18.5006, 18.4011, 18.2519, 17.8043,
17.9038, 17.9038, 17.9038, 17.8541, 17.9535, 17.9038, 17.9436,
18.0433, 18.1429, 18.1928, 17.8937, 17.4451, 17.8464, 17.4277,
17.2184, 17.48, 17.7417, 17.5847, 18.3174, 18.4221, 18.2651,
18.3697, 18.1604, 18.1081, 18.1081, 18.4744, 18.4221, 18.3174,
18.1604, 18.5791, 18.8931, 18.9978, 18.9978, 19.6781, 19.3118,
19.7305, 19.3641, 19.6258, 19.9398, 19.6781, 19.8875, 20.1491,
20.2538, 19.9398, 20.0445, 20.0445, 19.9921, 19.9921, 19.9921,
20.1491, 20.2538, 20.0968, 20.0445, 19.9921, 19.9921, 19.9398,
19.5734, 19.7305, 19.5211, 19.5734, 19.7305, 19.9398, 19.7305,
19.4688, 17.794, 19.2071, 18.5267, 18.4221, 18.6314, 19.0501,
18.9454, 19.1966, 18.9344, 18.7246, 18.5672, 18.0952, 18.0952,
18.0952, 18.0427, 18.3574, 18.2001, 18.4623, 18.3574, 18.3574,
18.0952, 17.7805, 17.6231, 17.6231, 17.9378, 18.0427, 17.7805,
17.5707, 17.6231, 17.9378, 17.8329, 17.4658, 17.256, 17.4133,
17.3084, 18.0427, 18.1476, 18.2525, 18.2001, 18.0952, 18.0427,
17.4658, 17.4133, 17.4133, 17.6231, 17.7805, 17.9378, 18.2001,
18.2001, 18.2001, 18.2525, 17.8329, 18.9344, 18.7246, 18.4099,
18.7246, 18.6197, 18.5148, 18.4623, 18.4099, 18.4099, 18.4623,
18.4099, 18.5148, 18.5148, 18.5148, 18.2001, 17.9903, 18.2001,
17.6756, 18.0952, 18.305, 18.4518, 18.3993, 18.557, 18.925, 18.8724,
18.9775, 19.0827, 18.8724, 18.8724, 18.7673, 18.9775, 18.925,
18.925, 18.6096, 18.7147, 18.8724, 18.8724, 18.8724, 19.0301,
19.1878, 19.4507, 19.0827, 19.4507, 19.3981, 19.6084, 19.6609,
19.8187, 19.7661, 19.0301, 18.5044, 18.6096, 18.6621, 18.4518,
18.5044, 18.7147, 18.3993, 18.189, 18.189, 18.189, 18.4518, 18.6096,
18.9775, 18.8198, 18.3467, 18.189, 18.0839, 18.2416, 18.2416,
18.0839, 18.2416, 18.5044, 18.557, 18.6096, 18.2941, 18.5044,
18.1364, 18.1364, 18.189, 17.9787, 18.189, 18.1364, 18.0839,
17.9262, 17.8736, 17.7684, 17.7579, 17.6525, 17.6525, 17.4945,
17.4418, 17.1783, 17.0202, 16.8621, 16.704, 16.704, 16.8621,
16.8094, 16.4406, 16.4406, 16.2825, 15.8609, 16.3352, 15.9136,
16.4406, 16.3352, 16.1771, 15.7029, 17.7052, 16.8621, 16.8094,
16.8094, 17.0729, 16.9148, 16.3879, 16.546, 17.4418, 17.231,
17.231, 16.8094, 16.9675, 17.1783, 16.8621, 16.6514, 16.3879,
16.3352, 16.5987, 16.3879, 16.3879, 16.6514, 16.019, 15.9136,
15.334, 15.334, 15.0178, 15.1759, 15.0178, 14.9124, 14.8598,
14.7017, 14.5963, 14.7017, 14.4382, 14.5436, 13.964, 14.122,
14.4382, 14.7544, 14.4382, 14.3328, 13.9113, 14.1115, 13.9529,
14.0058, 15.01, 15.01, 15.01, 14.7457, 14.3758, 14.2172, 14.5343,
14.4286, 14.8171, 14.6506, 14.4841, 14.3731, 14.0401, 13.4852,
13.8737, 13.3187, 13.9292, 13.8182, 13.4852, 13.3187)), row.names = c(NA,
-700L), class = c("tbl_df", "tbl", "data.frame"))

Below r1 is the mean of the last 365 rows. (Note that that is what the question asked for but there are not 365 days of data in a year since it seems weekends are missing so this is not the mean of the last year.)
r2 is the mean of the last 365 rows every 30 days. Again I doubt that that is what you wanted even though that is what was asked for.
r3 is the mean of the last 12 months. Presumably this is what you actually need.
Each of these is a zoo object. It is probably best to leave them as zoo objects so you can use the rest of that package easily and not convert them to data frames but if you do want to convert then just use fortify.zoo(r1), etc.
library(zoo)
z <- zoo(data[-1], data[[1]])
r1 <- rollapplyr(z, 365, mean, fill = NA)
r2 <- rollapplyr(z, 365, mean, by = 30, fill = NA)
z.ym <- aggregate(z, as.yearmon, sum)
z.n <- aggregate(z, as.yearmon, length)
r3 <- rollsumr(z.ym, 12, fill = NA) / rollsumr(z.n, 12, fill = NA)

How about this (notice I switched the order of the loops)
If you are iterating over a number:
for (j in 1:498){
for (u in seq(0,5605-30,30))
{
work.on.col = j
work.dt = data[u:u+30,work.on.col] #u:u+30 iterates i.e: 0:30, 30:60,...etc. It only keeps a 30-day rolling window.
}
}
If you are iterating over a date (this would only work if you had your rownames as dates):
for (j in 1:498){
for (u in sapply(seq.Date(as.Date(min(data$Dates)), as.Date(max(data$Dates)),
"day")," [[",30))
{
work.on.col = j
work.dt = data[u:u+30,work.on.col]
}
}

Related

The best way to filter timestamps with hours [duplicate]

This question already has answers here:
Subsetting data.table set by date range in R
(3 answers)
Subset a dataframe between 2 dates
(8 answers)
Closed 4 years ago.
structure(list(id = c(14735, 11589, 1165, 7864, 9151, 6662, 26, 6638, 7635, 10204, 10588, 11923, 2119, 2487, 11571, 6759, 9591,
1592, 12725, 5086, 3039, 10576, 2526, 1127, 583, 12879, 5686, 13405, 1375, 7547, 11479, 9220, 8040, 13848, 14996, 4256, 1879,
2653, 15220, 1896, 4547, 2505, 1105, 3625, 10896, 9806, 1154, 2626, 2215, 5957, 3522, 8531, 8867, 1501, 2415, 14009, 13056,
13740, 1751, 540, 8896, 4771, 8457, 5383, 2176, 8611, 5072, 1828, 3884, 5364, 12617, 11887, 14267, 12735, 2261, 8962, 12501, 9586,
7129, 3925, 373, 4987, 3410, 13304, 10276, 7975, 8456, 3752, 111, 14384, 10901, 4234, 11273, 13196, 5764, 10902, 3631, 9814,
14781, 5726), full_date = structure(c(1522781128, 1522108662, 1519981076, 1521121363, 1521457099, 1520859176, 1519631141, 1520856439,
1521056830, 1521753388, 1521853223, 1522173544, 1520160750, 1520238731, 1522105027, 1520873428, 1521581248, 1520074992, 1522326287, 1520600253,
1520300281, 1521850210, 1520242830, 1519972727, 1519776890, 1522350676, 1520695451, 1522446321, 1520025013, 1521042071, 1522085537, 1521481296,
1521154393, 1522521269, 1522939333, 1520464381, 1520117285, 1520254658, 1523208654, 1520119807, 1520509952, 1520241232, 1519954606, 1520375033,
1521937117, 1521645125, 1519978700, 1520252273, 1520176769, 1520750012, 1520362171, 1521290786, 1521377710, 1520061816, 1520221068, 1522546134,
1522389100, 1522506558, 1520098043, 1519764349, 1521384566, 1520541898, 1521271618, 1520642366, 1520169730, 1521307430, 1520598946, 1520109560,
1520416134, 1520639385, 1522308872, 1522167432, 1522609927, 1522327583, 1520186995, 1521399556, 1522283646, 1521580399, 1520947600, 1520421124, 1519712304, 1520583404, 1520349813, 1522426307, 1521776496, 1521143409,
1521271618, 1520395530, 1519645678, 1522639423, 1521939121, 1520461036, 1522043512, 1522409242, 1520707332, 1521939139, 1520376024, 1521647387,
1522804886, 1520700876), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
real_time = c("18:45:28", "23:57:42", "8:57:56", "13:42:43",
"10:58:19", "12:52:56", "7:45:41", "12:7:19", "19:47:10",
"21:16:28", "1:0:23", "17:59:4", "10:52:30", "8:32:11", "22:57:7",
"16:50:28", "21:27:28", "11:3:12", "12:24:47", "12:57:33",
"1:38:1", "0:10:10", "9:40:30", "6:38:47", "0:14:50", "19:11:16",
"15:24:11", "21:45:21", "21:10:13", "15:41:11", "17:32:17",
"17:41:36", "22:53:13", "18:34:29", "14:42:13", "23:13:1",
"22:48:5", "12:57:38", "17:30:54", "23:30:7", "11:52:32",
"9:13:52", "1:36:46", "22:23:53", "0:18:37", "15:12:5", "8:18:20",
"12:17:53", "15:19:29", "6:33:32", "18:49:31", "12:46:26",
"12:55:10", "7:23:36", "3:37:48", "1:28:54", "5:51:40", "14:29:18",
"17:27:23", "20:45:49", "14:49:26", "20:44:58", "7:26:58",
"0:39:26", "13:22:10", "17:23:50", "12:35:46", "20:39:20",
"9:48:54", "23:49:45", "7:34:32", "16:17:12", "19:12:7",
"12:46:23", "18:9:55", "18:59:16", "0:34:6", "21:13:19",
"13:26:40", "11:12:4", "6:18:24", "8:16:44", "15:23:33",
"16:11:47", "3:41:36", "19:50:9", "7:26:58", "4:5:30", "11:47:58",
"3:23:43", "0:52:1", "22:17:16", "5:51:52", "11:27:22", "18:42:12",
"0:52:19", "22:40:24", "15:49:47", "1:21:26", "16:54:36")), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"))
I want to filter above data and find observations that happened between two hours, let's say 09:30:00 and 16:15:30. What's the best way to do that, maybe with lubridate or hms packages?
As I said in comments this can be solved such as
start_data = "18:45:28"
end_date = "19:45:28"
# df - your data.frame
df[df$real_time > start_data & df$real_time < end_date , ]
Output:
id full_date real_time
26 12879 2018-03-29 19:11:16 19:11:16
51 3522 2018-03-06 18:49:31 18:49:31
73 14267 2018-04-01 19:12:07 19:12:7
75 2261 2018-03-04 18:09:55 18:9:55
76 8962 2018-03-18 18:59:16 18:59:16

What is the format of "{123, affdsf, 223, 22, dgbwa, 33333}"?

I have the following format, please advise how to convert it to a list in R?
"{1948, 2507, 2510, 7030, 7110, 9009, 00027, 00206, 00399, 00717, 00814, 00828, 00848, 00917, 01050, 01105, 01144, 02130, 02768, 03037, 03752, 03754, 04070, 04110, 05050, 05255, 05289, 05564, 05595, 06100, 06330, 06671, 07041, 07119, 07137, 07273, 07313, 07454, 07871, 08104, 08714, 08726, 08995, 09059, 09073, 09525, 09949, 09981, 10092, 10439, 10782, 11185, 11507, 11712, 11806, 11858, 11980, 12067, 12113, 12139, 12643, 13820, 14534, 15007, 15014, 15549, 15953, 16151, 16174, 16634, 16733, 16888, 17111, 17207, 17377, 17721, 17900, 18118, 18400, 18686, 18880, 19080, 19342, 19444, 19772, 19790, 19891, 20091, 20245, 20402, 20811, 21114, 21345, 21811, 21881, 22222, 22311, 22320, 22831, 22969, 23251, 23572, 23734, 23862, 23889, 24034, 24463, 25172, 25688, 26143, 26221, 26803, 26850, 26898, 27497, 28291, 28343, 29411, 29419, 30024, 30561, 30923, 31345, 31351, 31555, 31927, 32198, 32861, 33020, 33040, 33095, 33188, 33311, 33368, 33377, 33475, 33519, 33574, 33592, 34207, 34235, 34272, 34484, 34854, 34872, 34875, 34876, 34880, 35222, 35292, 35344, 36177, 36266, 37038, 37060, 37548, 37686, 37700, 38139, 39368, 39369, 39633, 40132, 40698, 40704, 40744, 40819, 41311, 41971, 42102, 42616, 43055, 43211, 43234, 43428, 43494, 43934, 44117, 44252, 44272, 44301, 44336, 44619, 44866, 44888, 45049, 45197, 45412, 45718, 46694, 46736, 47000, 48046, 48540, 49078, 49109, 49216, 49388, 49464, 50056, 50155, 50217, 50477, 50692, 51122, 51445, 51946, 52475, 52537, 52982, 54011, 54031, 54160, 54963, 55000, 55537, 56080, 56163, 56282, 56760, 56787, 57102, 57727, 57871, 58101, 58558, 58882, 59902, 60225, 60397, 60501, 60619, 60703, 60890, 61075, 61894, 61944, 62322, 62337, 62380, 62413, 62729, 62766, 62923, 63010, 63234, 63977, 64127, 65359, 65428, 65542, 65750, 65863, 66184, 66636, 66712, 67201, 67439, 67953, 68133, 68854, 69251, 69959, 70107, 70725, 70768, 71081, 71099, 71948, 72013, 72377, 72400, 72420, 72735, 73000, 73015, 73142, 73223, 73455, 73717, 74049, 74492, 74854, 74941, 75142, 75399, 75464, 75587, 75618, 75642, 75887, 76357, 76651, 77199, 77302, 77456, 77579, 77601, 77649, 77668, 77694, 77745, 78006, 78010, 78178, 78335, 78656, 78729, 78808, 78824, 78844, 78945, 79416, 79471, 79915, 80077, 80111, 80189, 80262, 80409, 80470, 80529, 80539, 80838, 81272, 81513, 81658, 81740, 81743, 81762, 81843, 82001, 82070, 82106, 82342, 82472, 82719, 83670, 84009, 84151, 84299, 84430, 84450, 84460, 84945, 86411, 86443, 86446, 86668, 86942, 87286, 87317, 87624, 87785, 88023, 88517, 88696, 88787, 88868, 88977, 89206, 90108, 90440, 90734, 90802, 90849, 90920, 90931, 91011, 91031, 91133, 91777, 91949, 92162, 92494, 93012, 93172, 94300, 94517, 95142, 95410, 95559, 95859, 96112, 97255, 97787, 97986, 98240, 98817, 99050, 99198, 99222, 99241, 99295, 99326, 99335, 99503, 99603, 99643, 99803, 99968}"
THIS IS NOT A DUPLICATE OF convert json to list in a vectorized way in R
IT'S COMPLETELY DIFFERENT BECAUSE THE FORMAT IS ABSOLUTELY DIFFERENT.
Try this one line code:
as.numeric(sapply(strsplit(substr(j,2,nchar(j)-1),split = ","),trimws))
[1] 1948 2507 2510 7030 7110 9009 27 206 399 717 814 828 848 917 1050 1105 1144
[18] 2130 2768 3037 3752 3754 4070 4110 5050 5255 5289 5564 5595 6100 6330 6671 7041 7119
[35] 7137 7273 7313 7454 7871 8104 8714 8726 8995 9059 9073 9525 9949 9981 10092 10439 10782
[52] 11185 11507 11712 11806 11858 11980 12067 12113 1213 ..
Your input:
j<-"{1948, 2507, 2510, 7030, 7110, 9009, 00027, 00206, 00399, 00717, 00814, 00828, 00848, 00917, 01050, 01105, 01144, 02130, 02768, 03037, 03752, 03754, 04070, 04110, 05050, 05255, 05289, 05564, 05595, 06100, 06330, 06671, 07041, 07119, 07137, 07273, 07313, 07454, 07871, 08104, 08714, 08726, 08995, 09059, 09073, 09525, 09949, 09981, 10092, 10439, 10782, 11185, 11507, 11712, 11806, 11858, 11980, 12067, 12113, 12139, 12643, 13820, 14534, 15007, 15014, 15549, 15953, 16151, 16174, 16634, 16733, 16888, 17111, 17207, 17377, 17721, 17900, 18118, 18400, 18686, 18880, 19080, 19342, 19444, 19772, 19790, 19891, 20091, 20245, 20402, 20811, 21114, 21345, 21811, 21881, 22222, 22311, 22320, 22831, 22969, 23251, 23572, 23734, 23862, 23889, 24034, 24463, 25172, 25688, 26143, 26221, 26803, 26850, 26898, 27497, 28291, 28343, 29411, 29419, 30024, 30561, 30923, 31345, 31351, 31555, 31927, 32198, 32861, 33020, 33040, 33095, 33188, 33311, 33368, 33377, 33475, 33519, 33574, 33592, 34207, 34235, 34272, 34484, 34854, 34872, 34875, 34876, 34880, 35222, 35292, 35344, 36177, 36266, 37038, 37060, 37548, 37686, 37700, 38139, 39368, 39369, 39633, 40132, 40698, 40704, 40744, 40819, 41311, 41971, 42102, 42616, 43055, 43211, 43234, 43428, 43494, 43934, 44117, 44252, 44272, 44301, 44336, 44619, 44866, 44888, 45049, 45197, 45412, 45718, 46694, 46736, 47000, 48046, 48540, 49078, 49109, 49216, 49388, 49464, 50056, 50155, 50217, 50477, 50692, 51122, 51445, 51946, 52475, 52537, 52982, 54011, 54031, 54160, 54963, 55000, 55537, 56080, 56163, 56282, 56760, 56787, 57102, 57727, 57871, 58101, 58558, 58882, 59902, 60225, 60397, 60501, 60619, 60703, 60890, 61075, 61894, 61944, 62322, 62337, 62380, 62413, 62729, 62766, 62923, 63010, 63234, 63977, 64127, 65359, 65428, 65542, 65750, 65863, 66184, 66636, 66712, 67201, 67439, 67953, 68133, 68854, 69251, 69959, 70107, 70725, 70768, 71081, 71099, 71948, 72013, 72377, 72400, 72420, 72735, 73000, 73015, 73142, 73223, 73455, 73717, 74049, 74492, 74854, 74941, 75142, 75399, 75464, 75587, 75618, 75642, 75887, 76357, 76651, 77199, 77302, 77456, 77579, 77601, 77649, 77668, 77694, 77745, 78006, 78010, 78178, 78335, 78656, 78729, 78808, 78824, 78844, 78945, 79416, 79471, 79915, 80077, 80111, 80189, 80262, 80409, 80470, 80529, 80539, 80838, 81272, 81513, 81658, 81740, 81743, 81762, 81843, 82001, 82070, 82106, 82342, 82472, 82719, 83670, 84009, 84151, 84299, 84430, 84450, 84460, 84945, 86411, 86443, 86446, 86668, 86942, 87286, 87317, 87624, 87785, 88023, 88517, 88696, 88787, 88868, 88977, 89206, 90108, 90440, 90734, 90802, 90849, 90920, 90931, 91011, 91031, 91133, 91777, 91949, 92162, 92494, 93012, 93172, 94300, 94517, 95142, 95410, 95559, 95859, 96112, 97255, 97787, 97986, 98240, 98817, 99050, 99198, 99222, 99241, 99295, 99326, 99335, 99503, 99603, 99643, 99803, 99968}"
This code removes first and last character of the string ("{" and "}" characters), splits values by "," and removes whitespaces using trimws. After that it moves the format to number.
If it happens your data actually is json, stick with the rjson package. This answer is assuming your data is not json (since rjson::fromjson throws an error on your data)
Try:
string <- "{1948, 2507, 2510, 7030, 7110, 9009, 00027, 00206, 00399, 00717, 00814, 00828, 00848, 00917, 01050, 01105, 01144, 02130, 02768, 03037, 03752, 03754, 04070, 04110, 05050, 05255, 05289, 05564, 05595, 06100, 06330, 06671, 07041, 07119, 07137, 07273, 07313, 07454, 07871, 08104, 08714, 08726, 08995, 09059, 09073, 09525, 09949, 09981, 10092, 10439, 10782, 11185, 11507, 11712, 11806, 11858, 11980, 12067, 12113, 12139, 12643, 13820, 14534, 15007, 15014, 15549, 15953, 16151, 16174, 16634, 16733, 16888, 17111, 17207, 17377, 17721, 17900, 18118, 18400, 18686, 18880, 19080, 19342, 19444, 19772, 19790, 19891, 20091, 20245, 20402, 20811, 21114, 21345, 21811, 21881, 22222, 22311, 22320, 22831, 22969, 23251, 23572, 23734, 23862, 23889, 24034, 24463, 25172, 25688, 26143, 26221, 26803, 26850, 26898, 27497, 28291, 28343, 29411, 29419, 30024, 30561, 30923, 31345, 31351, 31555, 31927, 32198, 32861, 33020, 33040, 33095, 33188, 33311, 33368, 33377, 33475, 33519, 33574, 33592, 34207, 34235, 34272, 34484, 34854, 34872, 34875, 34876, 34880, 35222, 35292, 35344, 36177, 36266, 37038, 37060, 37548, 37686, 37700, 38139, 39368, 39369, 39633, 40132, 40698, 40704, 40744, 40819, 41311, 41971, 42102, 42616, 43055, 43211, 43234, 43428, 43494, 43934, 44117, 44252, 44272, 44301, 44336, 44619, 44866, 44888, 45049, 45197, 45412, 45718, 46694, 46736, 47000, 48046, 48540, 49078, 49109, 49216, 49388, 49464, 50056, 50155, 50217, 50477, 50692, 51122, 51445, 51946, 52475, 52537, 52982, 54011, 54031, 54160, 54963, 55000, 55537, 56080, 56163, 56282, 56760, 56787, 57102, 57727, 57871, 58101, 58558, 58882, 59902, 60225, 60397, 60501, 60619, 60703, 60890, 61075, 61894, 61944, 62322, 62337, 62380, 62413, 62729, 62766, 62923, 63010, 63234, 63977, 64127, 65359, 65428, 65542, 65750, 65863, 66184, 66636, 66712, 67201, 67439, 67953, 68133, 68854, 69251, 69959, 70107, 70725, 70768, 71081, 71099, 71948, 72013, 72377, 72400, 72420, 72735, 73000, 73015, 73142, 73223, 73455, 73717, 74049, 74492, 74854, 74941, 75142, 75399, 75464, 75587, 75618, 75642, 75887, 76357, 76651, 77199, 77302, 77456, 77579, 77601, 77649, 77668, 77694, 77745, 78006, 78010, 78178, 78335, 78656, 78729, 78808, 78824, 78844, 78945, 79416, 79471, 79915, 80077, 80111, 80189, 80262, 80409, 80470, 80529, 80539, 80838, 81272, 81513, 81658, 81740, 81743, 81762, 81843, 82001, 82070, 82106, 82342, 82472, 82719, 83670, 84009, 84151, 84299, 84430, 84450, 84460, 84945, 86411, 86443, 86446, 86668, 86942, 87286, 87317, 87624, 87785, 88023, 88517, 88696, 88787, 88868, 88977, 89206, 90108, 90440, 90734, 90802, 90849, 90920, 90931, 91011, 91031, 91133, 91777, 91949, 92162, 92494, 93012, 93172, 94300, 94517, 95142, 95410, 95559, 95859, 96112, 97255, 97787, 97986, 98240, 98817, 99050, 99198, 99222, 99241, 99295, 99326, 99335, 99503, 99603, 99643, 99803, 99968}"
string as list of characters:
string_as_list_char <- as.list(strsplit(gsub('\\{|\\}', '', string), ", "))[[1]]
or converted to numeric:
string_as_list_num <- as.list(as.numeric(strsplit(gsub('\\{|\\}', '', string), ", ")[[1]]))

Approximate a shape outline using constrained B-splines

I'm looking for a possibility to generate a constrained spline in order to approximate a shape (in my case, a footprint outline). As raw data, I have a table with several hundred xy-coordinate pairs, which have been collected from the boundary of the footprint. The spline should only approximate the data points (the spline does not need to pass the data points). I want to be able to smooth the spline to certain degrees. Also, I need to be able to constrain the spline: Defining several critical data points which the spline has to pass.
The R package "cobs" (COnstrained B-Splines, https://cran.r-project.org/web/packages/cobs/index.html) comes very close to providing a solution, offering parameters to constrain the spline as wanted. However, this package does not spline through an ordered sequence of data points, which of course is crucial when you want the spline to follow the boundary of a shape. I tried to spline x and y coordinates separately, but after recombining them two distinct shapes appear in the plot, so this does not seem to work (Or I got something wrong?). Is anybody aware of a solution?
Update: working example (dinosaur footprint outline)
data.txt:
structure(list(V1 = c(124.9, 86.44, 97.22, 81.34, 49.09, 57.18,
-77.6, -191.95, -284.67, -383.18, -379.27, -492.85, -547.72,
-600.67, -713.29, -814.36, -868.27, -926.99, -958.76, -1025.18,
-1077.16, -1105.07, -1126.25, -1112.77, -1087.74, -989.56, -911.59,
-859.61, -745.06, -656.5, -682.01, -637.25, -601.71, -539.09,
-394.79, -219.17, -170.17, -201.48, -122.52, -43.56, 127.97,
344.42, 539.09, 686.11, 987.63, 1253.31, 1283.15, 1536.32, 1741.14,
1832.35, 1700.3, 1787.43, 1911.31, 2017.49, 2097.81, 2135.93,
2093.73, 2066.96, 2063.78, 2022.94, 1978.69, 1919.44, 1904.03,
1895.37, 1854.22, 1810.23, 1771.09, 1741.48, 1642.45, 1553.96,
1472.96, 1396.04, 1141.65, 1085.82, 1055.02, 1358.24, 1325.94,
1031.91, 1287.14, 1265.36, 931.15, 872.12, 811.48, 755.65, 738.32,
697.41, 682.49, 647.35, 628.25, 620.09, 629.62, 675.22, 709.25,
718.78, 717.42, 551.09, 535.21, 540.98, 534.73, 546.76, 811.96,
823.03, 822.07, 607.4, 626.18, 637.73, 659.87, 756.13, 753.72,
735.91, 720.99, 676.71, 576.6, 508.26, 339.8, 179.53, 121.16,
45.6, 12.93, -9.87, -12.59, 16, 27.91, 37.78, 49.35, 8.51, 2.72,
-1.02, 59.22, 58.2, 51.73, 54.45, 0.96, 10.59, 138.62, 149.69,
144.87, 142.26, 146.34, 125.24, 124.9, 86.44, 97.22, 81.34, 49.09,
57.18, -77.6, -191.95, -284.67, -383.18, -379.27, -492.85, -547.72,
-600.67, -713.29, -814.36, -868.27, -926.99, -958.76, -1025.18,
-1077.16, -1105.07, -1126.25, -1112.77, -1087.74, -989.56, -911.59,
-859.61, -745.06, -656.5, -682.01, -637.25, -601.71, -539.09,
-394.79, -219.17, -170.17, -201.48, -122.52, -43.56, 127.97,
344.42, 539.09, 686.11, 987.63, 1253.31, 1283.15, 1536.32, 1741.14,
1832.35, 1700.3, 1787.43, 1911.31, 2017.49, 2097.81, 2135.93,
2093.73, 2066.96, 2063.78, 2022.94, 1978.69, 1919.44, 1904.03,
1895.37, 1854.22, 1810.23, 1771.09, 1741.48, 1642.45, 1553.96,
1472.96, 1396.04, 1141.65, 1085.82, 1055.02, 1358.24, 1325.94,
1031.91, 1287.14, 1265.36, 931.15, 872.12, 811.48, 755.65, 738.32,
697.41, 682.49, 647.35, 628.25, 620.09, 629.62, 675.22, 709.25,
718.78, 717.42, 551.09, 535.21, 540.98, 534.73, 546.76, 811.96,
823.03, 822.07, 607.4, 626.18, 637.73, 659.87, 756.13, 753.72,
735.91, 720.99, 676.71, 576.6, 508.26, 339.8, 179.53, 121.16,
45.6, 12.93, -9.87, -12.59, 16, 27.91, 37.78, 49.35, 8.51, 2.72,
-1.02, 59.22, 58.2, 51.73, 54.45, 0.96, 10.59, 138.62, 149.69,
144.87, 142.26, 146.34, 125.24), V2 = c(-446.8, -415.83, -394.43,
-259.19, -104.69, -4.03, 58.59, -80.26, 52.11, -48.33, -142.23,
-176.89, -233.68, -321.28, -416.57, -457.97, -458.93, -429.09,
-422.35, -450.27, -431.98, -379.03, -260.63, -123.94, -2.65,
269.76, 455.55, 548.92, 616.3, 691.38, 756.84, 888.72, 1016.97,
1157.18, 1198.02, 1101.37, 1025.14, 929.84, 852.25, 766.48, 717.47,
733.81, 784.18, 835.91, 1225.63, 1198.68, 925.3, 742.4, 814.13,
732.45, 586.79, 394.84, 212.42, 28.64, -111.58, -337.56, -490.03,
-526.07, -528.82, -547.2, -551.97, -552.3, -585.51, -551.34,
-543.16, -526.1, -494.11, -466.88, -355.93, -274.94, -215.04,
-114.3, -194.21, -103.73, -3.62, 104.2, 230.8, 154.25, 380.55,
416.62, 260.07, 295.75, 295.75, 251.47, 220.67, 225.96, 180.72,
121.52, 4.14, -127.23, -176.24, -332.11, -408.35, -494.11, -573.75,
-582.62, -678.88, -730.38, -788.62, -831.94, -846.38, -895.95,
-934.46, -968.15, -1033.12, -1097.62, -1150.08, -1157.3, -1254.04,
-1340.2, -1441.75, -1500.47, -1550.52, -1605.39, -1681.44, -1709.84,
-1715.22, -1672.34, -1607, -1522.59, -1440.57, -1421.18, -1345.62,
-1247.95, -1190.77, -1181.58, -1071.65, -1037.62, -1010.39, -998.82,
-986.57, -937.9, -887.29, -842.05, -831.46, -774.66, -703.91,
-573.75, -533.59, -448.16, -446.8, -415.83, -394.43, -259.19,
-104.69, -4.03, 58.59, -80.26, 52.11, -48.33, -142.23, -176.89,
-233.68, -321.28, -416.57, -457.97, -458.93, -429.09, -422.35,
-450.27, -431.98, -379.03, -260.63, -123.94, -2.65, 269.76, 455.55,
548.92, 616.3, 691.38, 756.84, 888.72, 1016.97, 1157.18, 1198.02,
1101.37, 1025.14, 929.84, 852.25, 766.48, 717.47, 733.81, 784.18,
835.91, 1225.63, 1198.68, 925.3, 742.4, 814.13, 732.45, 586.79,
394.84, 212.42, 28.64, -111.58, -337.56, -490.03, -526.07, -528.82,
-547.2, -551.97, -552.3, -585.51, -551.34, -543.16, -526.1, -494.11,
-466.88, -355.93, -274.94, -215.04, -114.3, -194.21, -103.73,
-3.62, 104.2, 230.8, 154.25, 380.55, 416.62, 260.07, 295.75,
295.75, 251.47, 220.67, 225.96, 180.72, 121.52, 4.14, -127.23,
-176.24, -332.11, -408.35, -494.11, -573.75, -582.62, -678.88,
-730.38, -788.62, -831.94, -846.38, -895.95, -934.46, -968.15,
-1033.12, -1097.62, -1150.08, -1157.3, -1254.04, -1340.2, -1441.75,
-1500.47, -1550.52, -1605.39, -1681.44, -1709.84, -1715.22, -1672.34,
-1607, -1522.59, -1440.57, -1421.18, -1345.62, -1247.95, -1190.77,
-1181.58, -1071.65, -1037.62, -1010.39, -998.82, -986.57, -937.9,
-887.29, -842.05, -831.46, -774.66, -703.91, -573.75, -533.59,
-448.16)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,
-280L))
require(cobs)
xy <- dget(data.txt)
#Cumchord function (from Claude, 2008): Cumulative chordal distance vector
cumchord<-function(M)
{cumsum(sqrt(apply((M-rbind(M[1,],
M[-(dim(M)[1]),]))^2,1,sum)))}
z <- cumchord(xy)
#Calculating B-spline for x and y values separately
x <- cobs(z,xy[,1],nknots=50)
y <- cobs(z,xy[,2],nknots=50)
#Plot spline
plot(xy)
lines(x$fitted,y$fitted)
Image of resulting plot
Following the comments thread, here are some graphs. I use Momocs since I'm familiar with it and it will shorten examples.
I brief, your problem is that there are two outlines in your outline.
I also include original use of spline by Julien Claude, and two additional examples with bezier curves and elliptic Fourier transforms. All 4 can be used to describe an outline (and reconstruct it) and it's probably worth gathering them here.
The picture gathers the original shape and these 4 methods
Now the code. It's not particularly long but quite repetitive.
# devtools::install_github("vbonhomme/Momocs")
library(Momocs) # version 1.0.3
xy <- structure(list(
V1 = c(124.9, 86.44, 97.22, 81.34, 49.09, 57.18, -77.6, -191.95, -284.67, -383.18, -379.27, -492.85, -547.72, -600.67, -713.29, -814.36, -868.27, -926.99, -958.76, -1025.18, -1077.16, -1105.07, -1126.25, -1112.77, -1087.74, -989.56, -911.59, -859.61, -745.06, -656.5, -682.01, -637.25, -601.71, -539.09, -394.79, -219.17, -170.17, -201.48, -122.52, -43.56, 127.97, 344.42, 539.09, 686.11, 987.63, 1253.31, 1283.15, 1536.32, 1741.14, 1832.35, 1700.3, 1787.43, 1911.31, 2017.49, 2097.81, 2135.93, 2093.73, 2066.96, 2063.78, 2022.94, 1978.69, 1919.44, 1904.03, 1895.37, 1854.22, 1810.23, 1771.09, 1741.48, 1642.45, 1553.96, 1472.96, 1396.04, 1141.65, 1085.82, 1055.02, 1358.24, 1325.94, 1031.91, 1287.14, 1265.36, 931.15, 872.12, 811.48, 755.65, 738.32, 697.41, 682.49, 647.35, 628.25, 620.09, 629.62, 675.22, 709.25, 718.78, 717.42, 551.09, 535.21, 540.98, 534.73, 546.76, 811.96, 823.03, 822.07, 607.4, 626.18, 637.73, 659.87, 756.13, 753.72, 735.91, 720.99, 676.71, 576.6, 508.26, 339.8, 179.53, 121.16, 45.6, 12.93, -9.87, -12.59, 16, 27.91, 37.78, 49.35, 8.51, 2.72, -1.02, 59.22, 58.2, 51.73, 54.45, 0.96, 10.59, 138.62, 149.69, 144.87, 142.26, 146.34, 125.24, 124.9, 86.44, 97.22, 81.34, 49.09, 57.18, -77.6, -191.95, -284.67, -383.18, -379.27, -492.85, -547.72, -600.67, -713.29, -814.36, -868.27, -926.99, -958.76, -1025.18, -1077.16, -1105.07, -1126.25, -1112.77, -1087.74, -989.56, -911.59, -859.61, -745.06, -656.5, -682.01, -637.25, -601.71, -539.09, -394.79, -219.17, -170.17, -201.48, -122.52, -43.56, 127.97, 344.42, 539.09, 686.11, 987.63, 1253.31, 1283.15, 1536.32, 1741.14, 1832.35, 1700.3, 1787.43, 1911.31, 2017.49, 2097.81, 2135.93, 2093.73, 2066.96, 2063.78, 2022.94, 1978.69, 1919.44, 1904.03, 1895.37, 1854.22, 1810.23, 1771.09, 1741.48, 1642.45, 1553.96, 1472.96, 1396.04, 1141.65, 1085.82, 1055.02, 1358.24, 1325.94, 1031.91, 1287.14, 1265.36, 931.15, 872.12, 811.48, 755.65, 738.32, 697.41, 682.49, 647.35, 628.25, 620.09, 629.62, 675.22, 709.25, 718.78, 717.42, 551.09, 535.21, 540.98, 534.73, 546.76, 811.96, 823.03, 822.07, 607.4, 626.18, 637.73, 659.87, 756.13, 753.72, 735.91, 720.99, 676.71, 576.6, 508.26, 339.8, 179.53, 121.16, 45.6, 12.93, -9.87, -12.59, 16, 27.91, 37.78, 49.35, 8.51, 2.72, -1.02, 59.22, 58.2, 51.73, 54.45, 0.96, 10.59, 138.62, 149.69, 144.87, 142.26, 146.34, 125.24),
V2 = c(-446.8, -415.83, -394.43, -259.19, -104.69, -4.03, 58.59, -80.26, 52.11, -48.33, -142.23, -176.89, -233.68, -321.28, -416.57, -457.97, -458.93, -429.09, -422.35, -450.27, -431.98, -379.03, -260.63, -123.94, -2.65, 269.76, 455.55, 548.92, 616.3, 691.38, 756.84, 888.72, 1016.97, 1157.18, 1198.02, 1101.37, 1025.14, 929.84, 852.25, 766.48, 717.47, 733.81, 784.18, 835.91, 1225.63, 1198.68, 925.3, 742.4, 814.13, 732.45, 586.79, 394.84, 212.42, 28.64, -111.58, -337.56, -490.03, -526.07, -528.82, -547.2, -551.97, -552.3, -585.51, -551.34, -543.16, -526.1, -494.11, -466.88, -355.93, -274.94, -215.04, -114.3, -194.21, -103.73, -3.62, 104.2, 230.8, 154.25, 380.55, 416.62, 260.07, 295.75, 295.75, 251.47, 220.67, 225.96, 180.72, 121.52, 4.14, -127.23, -176.24, -332.11, -408.35, -494.11, -573.75, -582.62, -678.88, -730.38, -788.62, -831.94, -846.38, -895.95, -934.46, -968.15, -1033.12, -1097.62, -1150.08, -1157.3, -1254.04, -1340.2, -1441.75, -1500.47, -1550.52, -1605.39, -1681.44, -1709.84, -1715.22, -1672.34, -1607, -1522.59, -1440.57, -1421.18, -1345.62, -1247.95, -1190.77, -1181.58, -1071.65, -1037.62, -1010.39, -998.82, -986.57, -937.9, -887.29, -842.05, -831.46, -774.66, -703.91, -573.75, -533.59, -448.16, -446.8, -415.83, -394.43, -259.19, -104.69, -4.03, 58.59, -80.26, 52.11, -48.33, -142.23, -176.89, -233.68, -321.28, -416.57, -457.97, -458.93, -429.09, -422.35, -450.27, -431.98, -379.03, -260.63, -123.94, -2.65, 269.76, 455.55, 548.92, 616.3, 691.38, 756.84, 888.72, 1016.97, 1157.18, 1198.02, 1101.37, 1025.14, 929.84, 852.25, 766.48, 717.47, 733.81, 784.18, 835.91, 1225.63, 1198.68, 925.3, 742.4, 814.13, 732.45, 586.79, 394.84, 212.42, 28.64, -111.58, -337.56, -490.03, -526.07, -528.82, -547.2, -551.97, -552.3, -585.51, -551.34, -543.16, -526.1, -494.11, -466.88, -355.93, -274.94, -215.04, -114.3, -194.21, -103.73, -3.62, 104.2, 230.8, 154.25, 380.55, 416.62, 260.07, 295.75, 295.75, 251.47, 220.67, 225.96, 180.72, 121.52, 4.14, -127.23, -176.24, -332.11, -408.35, -494.11, -573.75, -582.62, -678.88, -730.38, -788.62, -831.94, -846.38, -895.95, -934.46, -968.15, -1033.12, -1097.62, -1150.08, -1157.3, -1254.04, -1340.2, -1441.75, -1500.47, -1550.52, -1605.39, -1681.44, -1709.84, -1715.22, -1672.34, -1607, -1522.59, -1440.57, -1421.18, -1345.62, -1247.95, -1190.77, -1181.58, -1071.65, -1037.62, -1010.39, -998.82, -986.57, -937.9, -887.29, -842.05, -831.46, -774.66, -703.91, -573.75, -533.59, -448.16)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, -280L))
### First thing first: double outline ---------------------
coo_plot(xy)
ldk_labels(xy) # blurry since superimposed
coo_plot(xy[1:140, ], lwd=3) # first shape
coo_draw(xy[-(1:140), ], border="white") # second shape
# so from here, we will use the first 140th points from xy and name it 'shp' to avoid confusion
# if you're bored with dinos footprints, you can use beer bottles with shp <- bot[9] for a guinness
shp <- xy[1:140, ]
### 1.Natural splines ---------------------
shp_cumchord <- coo_perimcum(shp) # cumchord equivalent
shp_spline <- cbind(spline(shp_cumchord, shp[, 1], method="natural", n=120)$y,
spline(shp_cumchord, shp[, 2], method="natural", n=120)$y)
coo_plot(shp, main="natural spline", zoom=1.2)
coo_draw(shp_spline, border="blue", lwd=2)
### 2. B-splines with cobs ---------------------
library(cobs)
shp_bspline <- cbind(cobs(shp_cumchord, shp[, 1], nknots=50)$fitted,
cobs(shp_cumchord, shp[, 2], nknots=50)$fitted)
coo_plot(shp, main = "bspline", zoom=1.2)
coo_draw(shp_bspline, border="blue")
### 3. Bezier curves ---------------------
# built in function so it's shorter
shp_bezier <- shp %>% bezier() %>% bezier_i()
coo_plot(shp, main = "bezier", zoom=1.2)
coo_draw(shp_bezier, border="blue")
### 4. elliptic Fourier transforms ---------------------
# another built in function
shp_eft <- shp %>% efourier() %>% efourier_i()
coo_plot(shp, main = "bspline", zoom=1.2)
coo_draw(shp_eft, border="blue")
### 5. A panel of original shape and 4 methods ---------
Out(list(original=shp,
nat_spline=shp_spline, bspline=shp_bspline,
bezier=shp_bezier, eft=shp_eft)) %>%
panel(names=TRUE, dim=c(1, 5))

R forecast function not picking up seasonality

I am having trouble picking up the seasonality the seems to be implied in the data. I think (though its just a guess that its using additive and not multiplicative seasonality). I am using the forecast function and thought it would automatically pick what I need based on a lecture from Dr. Hyndman. The following snipet of code plots the chart and I would have expected the forecast to be higher then it is. Am I missing a model parameter or something? Any help would be appreciated.
sw<-c(2280, 1754, 1667, 1359, 1285, 1379, 2166, 1053, 1076, 1149, 1277, 1577, 1639, 1719, 1592, 2306, 3075, 2897, 1875, 1966, 2927, 3528, 2948, 2890, 3947, 3913, 3885, 4148, 5293, 5752, 6001, 7719, 5512, 6782, 6320, 6425, 6406, 7237, 8655, 9269, 12447, 13470, 13469, 13949, 17753, 17653, 14531, 14496, 13643, 12652, 12665, 10629, 8962, 8198, 6833, 5027, 4407, 4449, 4399, 5896, 6589, 3786, 4386, 4847, 5597, 5407, 4800, 7803, 9255, 10423, 5523, 8121, 6944, 8434, 9847, 9292, 9794, 10195, 10124, 11310, 12245, 12798, 14611, 15402, 13532, 16154, 15101, 14755, 17139, 16475, 19935, 19980, 25173, 28568, 27839, 28991, 27073, 29615, 25849, 27910, 27067, 21303, 20544, 15188, 13706, 9277, 10815, 7228, 4608, 4409, 9866, 8471, 8223, 6445, 6641, 6833, 11421, 8945, 8127, 10380, 12005, 13272, 9431, 12144, 14934, 14052, 11712, 14888, 15824, 17275, 18067, 19839, 21192, 22763, 22976, 23721, 22681, 20131, 19965, 20539, 19517, 22022, 23076, 30574, 40247, 43111, 39577, 40724, 44982, 44388, 46372, 43153, 36821, 32258, 31256, 27153, 23180, 18252, 16381, 13220, 12500, 10727, 9636, 8892, 8644, 9482, 9170, 10937, 12299, 15781, 11477, 16524, 16752, 18072, 14776, 13388, 18056, 19815, 21263, 22046, 26415, 24247, 25403, 30058, 26331, 32533, 31891, 35973, 27558, 24554, 25692, 25955, 24284, 24930, 28354, 34840, 40055, 42099, 42768, 48279, 50086, 56466, 42244, 51451, 44583, 39091, 33391, 29452, 25533)
swts <- ts(sw, frequency=52, start=c(2006,30))
swfc <- forecast(swts,h=52)
plot(swfc)
Did you data have multiple seasonal periods? If so you could check the tbats function.
Anyway, your seasonal period is greater than 12, so forecast is using a stl decomposition to adjust your seasonal data. Maybe you wanna check ?stlf for more info on what parameters you can change, or try a BoxCox transformation:
lambda <- BoxCox.lambda(sw)
swfc <- forecast(swts,h=52, lambda = lambda, robust = TRUE)
plot(swfc)

Calculating Time Weighted Rate of Return in R

Is there an R function or library that will give me the monthly (or any other specified timeframe) time weighted rate of return (twrr) for my portfolio?
I am including a dput dump of sample data below of the date and portfolio ending balance below. Not sure why the dates were dput'ed the way they were, but the first date 12053 is '2003-01-01' and the last date 12195 is '2003-05-23'.
portfolio.df <- structure(
list(
Date = structure(c(12053, 12054, 12055, 12058,
12059, 12060, 12061, 12062, 12065, 12066, 12067, 12068, 12069,
12073, 12074, 12075, 12076, 12079, 12080, 12081, 12082, 12083,
12086, 12087, 12088, 12089, 12090, 12093, 12094, 12095, 12096,
12097, 12101, 12102, 12103, 12104, 12107, 12108, 12109, 12110,
12111, 12114, 12115, 12116, 12117, 12118, 12121, 12122, 12123,
12124, 12125, 12128, 12129, 12130, 12131, 12132, 12135, 12136,
12137, 12138, 12139, 12142, 12143, 12144, 12145, 12146, 12149,
12150, 12151, 12152, 12153, 12156, 12157, 12158, 12159, 12163,
12164, 12165, 12166, 12167, 12170, 12171, 12172, 12173, 12174,
12177, 12178, 12179, 12180, 12181, 12184, 12185, 12186, 12187,
12188, 12191, 12192, 12193, 12194, 12195),
class = "Date"),
Ending_Balance = c(56250000L,
56852500L, 57080000L, 57355000L, 57477500L, 56817500L, 57885000L,
57810000L, 57732500L, 57670000L, 57520000L, 57285000L, 57270000L,
56655000L, 55802500L, 56337500L, 55642500L, 54510000L, 54987500L,
55802500L, 56065000L, 56865000L, 56635000L, 56497500L, 56640000L,
56155000L, 55757500L, 55972500L, 55865000L, 55535000L, 55885000L,
56840000L, 56902500L, 56945000L, 56622500L, 57012500L, 57200000L,
58072500L, 57612500L, 57447500L, 57157500L, 57032500L, 57405000L,
57502500L, 56785000L, 57007500L, 56342500L, 55697500L, 56655000L,
56900000L, 57002500L, 57465000L, 57467500L, 57382500L, 57982500L,
56562500L, 58065000L, 58935000L, 58502500L, 58200000L, 57767500L,
57757500L, 58055000L, 58305000L, 58277500L, 58295000L, 59047500L,
58907500L, 59125000L, 59072500L, 59107500L, 59315000L, 59690000L,
58957500L, 59407500L, 59385000L, 59965000L, 60297500L, 59890000L,
59822500L, 60367500L, 60407500L, 60380000L, 60815000L, 61155000L,
61080000L, 61132500L, 61265000L, 60912500L, 61107500L, 61445000L,
61345000L, 61137500L, 61035000L, 60707500L, 61340000L, 61365000L,
61402500L, 61640000L, 61675000L)),
.Names = c("Date", "Ending_Balance"),
row.names = c(NA, 100L),
class = "data.frame")

Resources