Averaging time-stamped data hourly in R - r

I've average distance travelled data meters for several days and times of the year. Here's how the dataset I'm working with looks like:
> print(datanet)
Date & Time [Local] meters
1: 2017-06-01 00:00:14 2.333355
2: 2017-06-01 01:00:13 6.952414
3: 2017-06-01 02:00:30 61.727543
4: 2017-06-01 03:00:15 235.873883
5: 2017-06-01 04:00:15 138.136375
---
1428: 2017-07-30 19:00:21 40.602983
1429: 2017-07-30 20:00:47 34.292888
1430: 2017-07-30 21:00:20 303.478297
1431: 2017-07-30 22:00:18 5.741059
Now, I would like to transform this table so that it provides the average distance travelled for each hour of the day from 0 to 23, based on data from multiple days. Here's the code I've been using for that purpose (additionally includes an sd column):
data_travel<-datanet %>%
mutate(
date = ymd_hms(`Date & Time [Local]`),
hour = hour(date)
) %>%
group_by(hour) %>%
summarise(
avg_meters = mean(meters),
sd_meters = sd(meters)
)
This works, but sadly the last hour of the day 23 always shows NA values:
> head(data_travel[19:24,])
# A tibble: 6 x 3
hour avg_meters sd_meters
<int> <dbl> <dbl>
1 18 57.4 109.
2 19 96.5 177.
3 20 121. 248.
4 21 141. 299.
5 22 76.4 86.4
6 23 NA NaN
Does somebody have an idea of how I could modify this code so that I also get the average distance travelled avg_meters and sd for hour 23? Any input is appreciated!
> dput(datanet[1:700,])
structure(list(`Date & Time [Local]` = structure(c(1464732013,
1464735613, 1464739229, 1464742813, 1464746413, 1464750018, 1464753629,
1464757213, 1464760813, 1464764430, 1464768013, 1464771629, 1464775214,
1464778818, 1464782429, 1464786013, 1464789623, 1464793214, 1464796813,
1464800413, 1464804029, 1464807612, 1464811213, 1464814830, 1464818411,
1464822013, 1464825629, 1464829213, 1464832812, 1464836417, 1464840021,
1464843612, 1464847211, 1464850830, 1464854412, 1464858030, 1464861629,
1464865211, 1464868814, 1464872416, 1464876030, 1464879611, 1464883212,
1464886811, 1464890430, 1464894017, 1464897611, 1464901217, 1464904830,
1464908419, 1464912011, 1464915610, 1464919207, 1464922850, 1464926410,
1464930004, 1464933623, 1464937249, 1464940811, 1464944410, 1464948011,
1464951629, 1464955218, 1464958811, 1464962430, 1464966018, 1464969610,
1464973214, 1464976848, 1464980410, 1464984010, 1464987610, 1464991230,
1464994811, 1464998409, 1465002003, 1465005610, 1465009222, 1465012817,
1465016402, 1465020030, 1465023618, 1465027214, 1465030846, 1465034419,
1465038020, 1465041690, 1465045212, 1465048849, 1465052409, 1465056029,
1465059617, 1465063209, 1465066830, 1465070410, 1465074009, 1465077630,
1465081240, 1465084804, 1465088401, 1465092010, 1465095622, 1465099222,
1465102852, 1465106449, 1465110010, 1465113612, 1465117270, 1465120849,
1465124408, 1465128011, 1465131617, 1465135230, 1465138848, 1465142408,
1465146008, 1465149612, 1465153250, 1465156808, 1465160427, 1465164016,
1465167601, 1465171229, 1465174830, 1465178415, 1465182054, 1465185656,
1465189209, 1465192807, 1465196422, 1465200029, 1465203732, 1465207230,
1465210848, 1465214408, 1465218050, 1465221629, 1465225216, 1465228823,
1465232415, 1465236015, 1465239615, 1465243256, 1465246830, 1465250414,
1465254008, 1465257629, 1465261214, 1465264814, 1465268449, 1465272007,
1465275630, 1465279255, 1465282845, 1465286460, 1465290013, 1465293617,
1465297226, 1465300814, 1465304418, 1465308029, 1465311614, 1465315214,
1465318814, 1465322429, 1465326007, 1465329607, 1465333213, 1465336813,
1465340422, 1465344013, 1465347615, 1465351230, 1465354813, 1465358413,
1465362014, 1465365630, 1465369216, 1465372830, 1465376413, 1465380013,
1465383606, 1465387230, 1465390817, 1465394430, 1465398013, 1465401613,
1465405213, 1465408829, 1465412411, 1465416013, 1465419613, 1465423229,
1465426812, 1465430412, 1465434011, 1465437614, 1465441224, 1465444812,
1465448413, 1465452013, 1465455629, 1465459213, 1465462817, 1465466430,
1465470011, 1465473611, 1465477211, 1465480830, 1465484412, 1465488012,
1465491611, 1465495230, 1465498820, 1465502426, 1465506011, 1465509606,
1465513212, 1465516830, 1465520419, 1465524003, 1465527611, 1465531227,
1465534850, 1465538403, 1465542010, 1465545612, 1465549229, 1465552811,
1465556425, 1465560011, 1465563611, 1465567227, 1465570819, 1465574430,
1465578018, 1465581610, 1465585210, 1465588830, 1465592418, 1465596009,
1465599602, 1465603209, 1465606829, 1465610450, 1465614009, 1465617609,
1465621210, 1465624817, 1465628422, 1465632017, 1465635610, 1465639210,
1465642829, 1465646417, 1465650008, 1465653630, 1465657209, 1465660813,
1465664451, 1465668003, 1465671609, 1465675228, 1465678816, 1465682409,
1465686002, 1465689608, 1465693229, 1465696816, 1465700401, 1465704008,
1465707629, 1465711217, 1465714809, 1465718429, 1465722016, 1465725608,
1465729201, 1465732830, 1465736415, 1465740024, 1465743630, 1465747215,
1465750811, 1465754430, 1465758016, 1465761607, 1465765230, 1465768828,
1465772415, 1465776016, 1465779613, 1465783260, 1465786808, 1465790414,
1465794014, 1465797659, 1465801216, 1465804807, 1465808430, 1465812030,
1465815614, 1465819220, 1465822816, 1465826408, 1465830029, 1465833613,
1465837215, 1465840829, 1465844414, 1465848008, 1465851627, 1465855213,
1465858814, 1465862414, 1465866029, 1465869607, 1465873207, 1465876814,
1465880429, 1465884015, 1465887630, 1465891207, 1465894815, 1465898429,
1465902014, 1465905614, 1465909218, 1465912824, 1465916413, 1465920013,
1465923618, 1465927229, 1465930813, 1465934413, 1465938013, 1465941613,
1465945229, 1465948811, 1465952412, 1465956022, 1465959626, 1465963214,
1465966830, 1465970412, 1465974030, 1465977606, 1465981230, 1465984813,
1465988429, 1465992012, 1465995613, 1465999229, 1466002812, 1466006404,
1466010012, 1466013626, 1466017211, 1466020830, 1466024412, 1466028012,
1466031619, 1466035211, 1466038813, 1466042430, 1466046004, 1466049612,
1466053212, 1466056830, 1466060420, 1466064026, 1466067603, 1466071211,
1466074829, 1466078410, 1466082019, 1466085619, 1466089211, 1466092804,
1466096429, 1466100011, 1466103611, 1466107230, 1466110821, 1466114422,
1466118009, 1466121611, 1466125234, 1466128803, 1466132412, 1466136027,
1466139610, 1466143210, 1466146834, 1466150410, 1466154034, 1466157609,
1466161210, 1466164834, 1466168403, 1466172007, 1466175620, 1466179202,
1466182802, 1466186433, 1466190020, 1466193609, 1466197209, 1466200802,
1466204433, 1466208009, 1466211609, 1466215202, 1466218854, 1466222419,
1466226009, 1466229634, 1466233208, 1466236811, 1466240426, 1466244008,
1466247608, 1466251234, 1466254826, 1466258409, 1466262008, 1466265609,
1466269233, 1466272808, 1466276408, 1466280011, 1466283647, 1466287216,
1466290808, 1466294408, 1466298033, 1466301601, 1466305210, 1466308825,
1466312407, 1466316009, 1466319634, 1466323208, 1466326833, 1466330408,
1466334007, 1466337633, 1466341210, 1466344834, 1466348419, 1466352015,
1466355616, 1466359223, 1466362820, 1466366415, 1466370014, 1466373622,
1466377205, 1466380815, 1466384414, 1466388033, 1466391608, 1466395206,
1466398833, 1466402415, 1466406017, 1466409634, 1466413214, 1466416817,
1466420431, 1466424008, 1466427615, 1466431234, 1466434813, 1466438434,
1466442013, 1466445614, 1466449207, 1466452835, 1466456413, 1466460013,
1466463613, 1466467213, 1466470834, 1466474413, 1466478014, 1466481633,
1466485223, 1466488806, 1466492416, 1466496035, 1466499613, 1466503206,
1466506835, 1466510412, 1466514012, 1466517635, 1466521213, 1466524823,
1466528413, 1466532010, 1466535605, 1466539234, 1466542812, 1466546405,
1466550012, 1466553612, 1466557234, 1466560813, 1466564412, 1466568035,
1466571605, 1466575214, 1466578835, 1466582412, 1466586012, 1466589605,
1466593222, 1466596811, 1466600412, 1466604005, 1466607629, 1466611212,
1466614811, 1466618430, 1466622019, 1466625612, 1466629230, 1466632820,
1466636411, 1466640010, 1466643629, 1466647213, 1466650811, 1466654430,
1466658019, 1466661619, 1466665218, 1466668812, 1466672430, 1466676011,
1466679610, 1466683220, 1466686830, 1466690419, 1466694010, 1466697614,
1466701247, 1466704818, 1466708411, 1466712028, 1466715618, 1466719203,
1466722810, 1466726423, 1466730010, 1466733609, 1466737223, 1466740817,
1466744403, 1466748031, 1466751617, 1466755210, 1466758811, 1466762411,
1466766030, 1466769619, 1466773212, 1466776847, 1466780417, 1466784009,
1466787609, 1466791231, 1466794818, 1466798409, 1466802010, 1466805609,
1466809230, 1466812817, 1466816401, 1466820009, 1466823660, 1466827217,
1466830831, 1466834409, 1466838008, 1466841619, 1466845216, 1466848808,
1466852404, 1466856021, 1466859616, 1466863232, 1466866808, 1466870408,
1466874011, 1466877630, 1466881215, 1466884818, 1466888417, 1466892019,
1466895616, 1466899207, 1466902809, 1466906430, 1466910016, 1466913611,
1466917226, 1466920821, 1466924416, 1466928008, 1466931622, 1466935231,
1466938815, 1466942431, 1466946015, 1466949629, 1466953215, 1466956808,
1466960415, 1466964030, 1466967614, 1466971215, 1466974812, 1466978430,
1466982014, 1466985614, 1466989208, 1466992830, 1466996413, 1467000014,
1467003614, 1467007230, 1467010814, 1467014415, 1467018015, 1467021630,
1467025214, 1467028830, 1467032415, 1467036030, 1467039614, 1467043206,
1467046815, 1467050430, 1467054012, 1467057613, 1467061213, 1467064814,
1467068430, 1467072013, 1467075608, 1467079250, 1467082814, 1467086414,
1467090007, 1467093629, 1467097214, 1467100824, 1467104413, 1467108013,
1467111630, 1467115213, 1467118813, 1467122429, 1467126006, 1467129613,
1467133213, 1467136829, 1467140412, 1467144013, 1467147613, 1467151214,
1467154829, 1467158410, 1467162027, 1467165605, 1467169212, 1467172813,
1467176428, 1467180012, 1467183619, 1467187220, 1467190812, 1467194405,
1467198012, 1467201629, 1467205218, 1467208830, 1467212411, 1467216004,
1467219611, 1467223211, 1467226829, 1467230411, 1467234012, 1467237612,
1467241229, 1467244811, 1467248405), class = c("POSIXct", "POSIXt"
), tzone = ""), meters = c(7.24497992499657, 4.87741163537199,
9.08560044628181, 80.6842320881314, 238.606484922097, 157.204921816723,
625.23872908032, 219.35778781259, 12.6588736944506, 93.8090439559674,
319.445131807673, 67.8036768396769, 804.804836152127, 109.434600933436,
129.949236899749, 105.911149760734, 27.9531918089091, 11.27836453714,
457.093853355937, 26.5240927781247, 19.7015020304213, 14.3532653640863,
1.25853679670009, 0.150718694512225, 1.70366003911483, 2.63870002711148,
127.037462401145, 961.452700995197, 215.04628486518, 48.3476802703997,
56.4299311045402, 71.0567210386123, 53.2157129067539, 80.4040760406296,
236.078682140782, 406.948035573002, 92.6423364709784, 403.797511366086,
323.858212895809, 65.9783289318472, 26.7161400634748, 21.4406886404941,
44.6906704150594, 36.0784092780547, 66.4678272178005, 68.0358199816987,
2.1476323514823, 3.01587341033808, 1.57380761082474, 1.71653324348141,
18.8397076847765, 184.268772826548, 61.2103183204004, 82.9010640232318,
43.7120771884048, 40.4214303580113, 220.354835462908, 77.5844706628055,
10.6522275628958, 64.6401569172547, 170.237028243589, 235.781539666942,
206.150503465281, 25.3213069661311, 36.7436253838348, 9.83110790227874,
23.3459053606757, 3.45271958972457, 1.96114320043511, 20.4049146593214,
15.2372682099889, 20.3543121890185, 42.7350584816069, 12.1313207862892,
1.11708614676525, 191.836648404227, 33.3046462595366, 166.168666618136,
31.1722631768611, 133.717766242875, 12.0334817161546, 62.2359071313657,
16.7484729490856, 109.549479467076, 438.080739581294, 37.0971614841641,
105.391252306762, 122.494788370234, 88.6622245013997, 24.3191344096727,
5.117649955497, 51.9358625225939, 47.9478783281661, 6.96463276369705,
1.75025309899143, 31.176657161161, 10.1169843733554, 26.5346636683759,
15.9584899969855, 337.838831129694, 59.7693703670957, 46.7853809521572,
16.799710673628, 39.1979373391332, 122.408881979713, 266.855999717221,
63.8055787186155, 57.9900269187913, 120.78876572575, 82.1213040340665,
105.298734249817, 161.923229191297, 28.9509612131438, 0.248722765246352,
6.42826019283635, 4.80096922046293, 7.66924438494585, 3.77931970556652,
6.16226345339552, 1.89180927192504, 2.92660299028088, 4.47513027348909,
5.3772236196912, 258.79885256986, 76.3673624568927, 227.248769639605,
119.571707120552, 35.7102849958032, 36.9949248244319, 137.90048603805,
96.6658682838857, 259.080913058415, 105.606050276669, 56.1002922989478,
85.2381765021222, 191.363093870704, 55.7981801107081, 12.9578924739909,
26.3419895578265, 14.4503596334286, 15.6675803413194, 21.9669267962415,
63.1276880372023, 5.54867147176836, 9.0179124542279, 7.13599657582419,
69.6648263961824, 352.989183299746, 263.287397250075, 253.766882591523,
209.967849272818, 73.9692977527144, 98.0159993160327, 170.190795021595,
282.190504225449, 78.6666650047386, 27.0630775295066, 332.829084995611,
194.938072897224, 102.422860453484, 17.7992858642505, 13.1266890679012,
4.58091256610204, 6.40555894626406, 2.66715489350561, 19.248878078399,
14.0807810821772, 4.57816759344819, 4.40196859830686, 10.7329317290172,
32.4528952520776, 138.596548507858, 125.547606032588, 46.3652014291144,
16.3797234392651, 10.249071010749, 248.440266699442, 304.347056271548,
154.412296810916, 46.8081932028809, 226.453483692211, 431.805061221221,
111.437754042661, 217.641929376792, 25.1923986792615, 12.2256823484931,
12.2949586884092, 31.0958526630604, 85.5575841303107, 22.7975660566324,
30.5216893316272, 35.1775681936213, 9.50846937534727, 9.76657486076715,
0.579469646956765, 266.224607309967, 188.727073099707, 292.559096086872,
9.49743703683714, 107.113753739463, 175.681846441223, 19.379648871926,
88.2778253322274, 410.496903513497, 8.97162022276979, 32.4619475881012,
73.646386222969, 75.348171202073, 90.8515841171699, 10.6200903372279,
3.02379387011622, 32.825022046837, 65.550868227495, 24.9833819842075,
77.5493346217115, 2.10544392843427, 2.32222002989636, 3.12605565461408,
1.02518236594501, 341.46710436094, 151.588096225148, 353.933570258634,
124.173972566209, 60.4110080957218, 38.5295043269143, 154.717374816579,
10.642332114307, 112.19511336859, 178.656934678561, 144.883837500965,
193.991868696415, 202.99316836535, 77.6189915466929, 0.871460936515423,
1.63576829944789, 47.5439446635587, 60.241399209101, 92.7059630247652,
1.71653312232677, 3.28998417221502, 16.9888823353554, 1.073227079111,
31.3529682130551, 98.2633746496518, 146.311948071212, 277.215271024987,
30.6004645511119, 49.4907657584358, 17.6377880041836, 517.661457540348,
581.555783536356, 1010.85341607138, 101.36421835411, 101.587448595859,
144.303729077564, 91.3938747436922, 149.518556866971, 36.3308699793953,
7.80121835979054, 23.0312990229266, 13.41048184825, 20.339107047676,
3.08847373655867, 31.7536206163432, 80.5523050297356, 5.57519986215111,
12.7301911126705, 265.400347490029, 96.389278202961, 96.4450196328944,
269.701595926116, 40.1994744716222, 185.194247845766, 132.799182823423,
92.6508846479433, 31.7196753780259, 82.6725380176083, 149.907487149117,
259.995942351777, 136.962271891916, 47.7342981878729, 28.0369643012698,
23.5176540297538, 85.9823208879668, 69.1793641133218, 2.10460736024825,
2.47507980743031, 1.46820616137708, 8.50065507425538, 9.43358037894557,
15.0643556352927, 160.034358372113, 401.192221112903, 208.507212166668,
16.9012427928657, 70.1561153179486, 282.055943502233, 95.7582280566781,
20.4921115795782, 224.297864971227, 248.751359316637, 63.3008262529409,
202.381548954774, 160.240208598145, 89.2596850671307, 2.41266014760612,
2.24662537954621, 92.0812846530431, 64.0782727558696, 102.539355421441,
133.192603215476, 2.19161459176181, 2.71657087565203, 2.60259248593429,
9.95223133490391, 175.102558714441, 102.128569321102, 45.1350310564478,
60.7860248161304, 166.511239966959, 32.3622524770883, 38.1126859517567,
169.914906248272, 165.51479143087, 21.6290787183546, 154.863792200668,
224.650919723327, 172.786068029272, 41.6201741515014, 40.966552071361,
60.9998953906058, 4.00012706993277, 79.9578066806101, 183.917814759389,
103.086558986388, 6.96826209073272, 5.74403906883466, 5.4856515067938,
28.6736690417882, 238.403484773501, 231.70110714268, 126.348996131178,
61.4905557699149, 104.389974082626, 246.69389506543, 79.3069202652704,
24.117595869327, 48.4779179700019, 69.4483313003939, 127.606317513607,
78.8710394107804, 98.1528155665254, 128.061282053331, 3.19606373207204,
1.87066709355931, 30.3658567894746, 67.1163251638405, 32.6323314265454,
0.228514489291685, 2.59419308146204, 6.96463324671082, 31.4857435059086,
32.2152666766542, 219.54173457463, 100.503943180343, 71.9061154834901,
91.2830509779235, 155.03560958018, 98.391102232677, 27.4736388446992,
34.7344995015586, 85.1266347031687, 74.6245520597207, 24.0060703654787,
139.030754853487, 171.244448038257, 112.193936557097, 2.39299360914121,
19.4583131438491, 58.7443921590234, 14.0595780932243, 216.575414578798,
127.459683665046, 26.4468118443774, 15.7904069541748, 3.42889322639363,
18.9364897049987, 137.67183581274, 122.967226389734, 119.894430382828,
104.442282446888, 190.221376118256, 131.941577184786, 353.399746658368,
448.477151206068, 133.358287719838, 261.649707603195, 81.4006720251283,
343.002058701936, 163.91259720329, 197.994167334045, 7.30944634184061,
7.26330302571758, 32.2642168570983, 330.892281390864, 76.9551034586096,
16.5345940654105, 93.4833973060589, 2.44622725450917, 1.07860556492395,
1.93778399725422, 77.9570596409594, 94.0913633507909, 56.7576348472931,
409.330409688539, 51.8605115857434, 101.399915620214, 186.284262562234,
150.206902386026, 73.7756320461831, 29.7407716653824, 148.98703547435,
220.790713913921, 244.515597043242, 18.6165511888937, 9.90234122916165,
33.2562502415159, 156.414444934157, 13.9779124860658, 48.9122633130083,
170.372846084158, 157.591584502432, 41.3319249226387, 14.1513887931616,
6.44142678100409, 228.471931226893, 110.023055666621, 33.5052868559644,
173.194133068492, 32.3931156891464, 44.5888695638585, 57.8480536590698,
130.274156166872, 79.3730952009515, 46.5247317494093, 113.821030825373,
345.300064595988, 152.140595169695, 17.0421460982582, 6.84417297878845,
12.5696620896434, 36.6545290246341, 74.6452657675716, 217.14457420751,
165.496335573275, 22.3871316935213, 6.61421665015, 20.0410769731539,
23.7057126539467, 168.517094664878, 110.986727962072, 83.3281747496762,
7.35167504947444, 76.5528141698817, 20.6384141732761, 87.00310582216,
402.411224410847, 145.210679361704, 55.1206401339897, 446.103457643039,
95.317801637148, 198.306682822754, 88.7652010770343, 4.4779529467687,
55.2357786872407, 118.400174413319, 163.550512253059, 103.789889510405,
70.1485296476271, 44.9031868790507, 5.42285572703366, 23.2710355323781,
7.96212769344129, 37.560076557467, 210.670533114753, 104.544578996089,
438.121243591053, 56.4263114090557, 30.3428605030646, 102.704223497357,
88.0554172082872, 29.8261947342531, 21.3578133423672, 125.139532314134,
199.2412154636, 423.415414756748, 155.583267038193, 2.14393350563694,
2.77638044593597, 25.6375785864043, 176.271312482445, 188.095709294767,
162.049988299195, 28.6407159601821, 341.238744680548, 6.70617205440293,
0.685237342195351, 72.5497248411768, 411.366138460536, 14.570194300977,
35.8331305489166, 336.776755084208, 43.10843602833, 343.590748922672,
306.997839886018, 110.223009494854, 246.067728815614, 229.160642943454,
80.1932202086262, 223.436937319274, 7.33591020042729, 3.91210855110157,
0.301852508403949, 1.9831582085811, 100.093808999808, 136.307291596312,
56.0971664553408, 35.91142300096, 35.9638499452433, 2.71553679684771,
4.6621428025371, 140.593157625054, 274.807479865085, 221.786077846005,
61.483885141769, 20.3881339787884, 30.7758272593722, 96.0458882470437,
246.919746334924, 22.0033859138399, 198.28605047425, 103.814293419658,
46.5348985729046, 29.182847412964, 75.8648063336849, 13.8241139461049,
17.1286557911254, 88.1810161018373, 3.36013813866121, 69.639752829193,
677.723130883346, 41.0286431704323, 73.2159389655071, 2.97198914835809,
2.2955645498568, 77.4338889708046, 165.144080335453, 28.249849842644,
697.335948561217, 26.764915418294, 14.2190768683659, 91.7637857701146,
27.5440244171723, 16.8445374971489, 23.8566496302873, 0.981943140947041,
78.5997636834095, 162.138462101107, 44.1672067123073, 1.57379999898296,
4.52584675687701, 0.97658119252377, 0.477360675618112, 129.151103972441,
70.8307163818214, 275.971859788928, 127.881236082799, 1.170287338146,
0.866454371283992, 434.919703422169, 93.8451377376139, 207.10904118958,
46.8316256828644, 150.387134794503, 278.399451505872, 198.814569340003,
115.184928188408, 36.753170014185, 129.106822541989, 168.482550085438,
92.1323337766019, 250.394018594269, 37.1881210650176, 26.0619948566024,
2.1015758585879, 1.9337619910658, 2.5374401085012, 6.88084629859044,
240.364952743281, 23.3347433113824, 12.8301991435217, 104.664097883855,
11.9543330584122, 299.693093901171, 457.205452556256, 166.486441167246,
479.147896086039, 601.250116553193, 324.328442697521, 329.307886840488,
231.36130846456, 34.9248512789383, 159.724908476382, 310.307623928807,
665.667745992218, 440.34793375254, 47.0987434639045, 3.27176941323539,
9.70137304643561, 10.0607743796965, 3.08631438745061, 85.9070751173181,
114.552594829497, 56.4422079169895, 72.8598000828185, 49.5713482843566,
2.63222698246548, 278.660159682918, 374.155025716734, 614.896477070897,
84.7023024801914, 112.319999024275, 18.5987593461749, 82.9077768700278,
154.845742871174, 125.567795777075, 66.4893506450749, 126.741063662877,
82.2411837719443, 756.335890510717, 73.1151790189073, 3.5922959646701,
8.41573329417288, 4.41473763601453, 2.12491629471561, 57.10162180489,
51.9326111578832, 58.8698849597487, 64.1069702907545, 25.653260586019,
2.25452824419408, 133.78927961757, 341.7548293499, 14.113318950603,
64.7040755111393, 74.5271989167769, 407.725534601351, 309.316524308558,
20.2280966869265, 23.8884632436018, 20.0051667649045, 23.3715363806949,
21.8895053097727, 284.299801015909, 133.058636731235, 9.13435639512076,
8.93531290420054, 6.97575634977357, 38.5847487365879, 322.899303421944,
7.43662008052574, 31.3472739232612, 90.172402886085, 13.1780473878919,
11.8113106256799, 95.4868454357865, 111.151536039587, 62.3434590358668,
25.3913623754508, 152.028407407367, 140.924105429548, 110.376776160796,
21.9269046506022, 282.56001268775, 26.9210719144184, 88.3343050027196,
291.612562587322, 164.906755082596, 116.426543798048, 16.3087551310383,
6.52751999940019, 9.01631759743765, 16.1907026689521, 16.5315572289726,
1.88226712179479, 18.0388366074334, 48.3907627589146, 1.49068315465064,
9.44594654212787, 730.702774263893)), row.names = c(NA, -700L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000002641ef0>)
```

Your meters field might have NA values.
Hence adding na.rm = T to mean and sd functions will solve this.

Related

ARDL model in R

I have this data frame on which I want to apply the ARDL model but every time I run it, it gives me an error. If anyone could please help me or point out what I am doing wrong would be highly appreciated. Error:'list' object cannot be coerced to type 'double' If I remove the prediction part of the code in the loop out then it runs otherwise no.
Data:
structure(list(Industrialproduction = c(1.65801981343852, 1.79541527049647,
-0.0326429293424051, 0.104752527715549, -0.992082392187777, -2.26823002723453,
-2.33809212404366, -3.02972688245404, -2.14713572609871, -1.29947561814794,
0.104752527715549, 0.228175565411677, 0.305023871901719, 0.218860619170459,
0.216531882610155, 0.139683576120113, 0.25146293101472, 0.249134194454415,
0.626389517223712, 1.13405408737005, 0.58214352257793, -0.0629165046263609,
-0.619484542539089, -0.652086854383349, 0.591458468819148, 2.0259601899666,
1.73021064680795, 0.561184893535192, 0.207216936368938, -0.489075295162048
), Householdconsumption = c(-1.5532531908672, -1.52804903107083,
-1.51957878064746, -1.50015918211582, -1.4800165134261, -1.47578138821441,
-1.46235294242126, -1.45274643889231, -1.43477298067686, -1.42299726667364,
-1.41225451003912, -1.39892935998284, -1.38694705450587, -1.37909657850372,
-1.36525494976309, -1.34924411054818, -1.33457611591258, -1.32538279533112,
-1.31143787085362, -1.30255443748276, -1.29181168084824, -1.27838323505509,
-1.27476788426463, -1.25586476441735, -1.24336598025603, -1.22942105577852,
-1.21051793593124, -1.1953334626113, -1.17581056834279, -1.15804370160108
), Investmentgrowth = c(1.47348593810751, 2.17792802452104, 2.57620375293532,
3.11977876989162, 2.03003410582649, 1.238671909303, 0.670447905897604,
0.0127091622297187, -0.222104866574793, -0.1974558801257, -0.215618291193452,
-0.31551155206609, 0.0762776009668517, 0.37206543835596, 0.593906316397791,
0.867639797490343, 1.12321086751514, 0.272172177483322, 0.191738642754705,
0.619852617923151, 0.675637166202676, 1.31910544403161, 1.23348264899792,
0.702880782804304, 1.61748791157326, 0.308496999618827, 0.395417109728784,
0.290334588551075, -0.659300047277115, -0.117022345397083), ConsumerPriceIndex = c(-2.03033282052684,
-1.98504568168304, -1.91626049909952, -1.88507218649954, -1.82055937550506,
-1.75220143008044, -1.72955786065854, -1.7026419196476, -1.65991820375721,
-1.60053223866958, -1.57447077197644, -1.53473771619839, -1.51166690961758,
-1.46381634782035, -1.46381634782035, -1.45057199589433, -1.43860935544502,
-1.40400314557381, -1.3800778646752, -1.36341561547795, -1.35188021218754,
-1.31727400231633, -1.29933004164237, -1.28394950392183, -1.24549815962049,
-1.19380246339312, -1.19380246339312, -1.18183982294381, -1.16816823385889,
-1.14595190159589), Employment = c(-1.02390309005026, -0.997493370038682,
-0.974856467171615, -0.96353801573808, -0.978629284316127, -1.00881182147222,
-1.07294971292891, -1.14840605581914, -1.21631676442034, -1.28045465587703,
-1.32950127875568, -1.38232071877884, -1.41250325593493, -1.45023142738004,
-1.5030508674032, -1.55587030742636, -1.58228002743794, -1.56341594171538,
-1.49173241596967, -1.43136734165749, -1.38232071877884, -1.34081973018921,
-1.30686437588861, -1.2691362044435, -1.24272648443192, -1.22386239870936,
-1.20499831298681, -1.19745267869778, -1.18990704440876, -1.17481577583072
), Unemploymentrate = c(-0.815370914670033, -0.815370914670033,
-0.772756573338144, -0.687527890674367, -0.517070525346812, -0.303998818687369,
0.0795302532996298, 0.463059325286628, 0.803974055941737, 1.05966010393307,
1.18750312792874, 1.3153461519244, 1.40057483458818, 1.44318917592007,
1.57103219991573, 1.6988752239114, 1.74148956524329, 1.82671824790707,
1.86933258923895, 1.91194693057084, 1.99717561323462, 1.95456127190273,
1.91194693057084, 1.86933258923895, 1.78410390657518, 1.74148956524329,
1.74148956524329, 1.74148956524329, 1.74148956524329, 1.78410390657518
), Stockmarketindex = c(-1.66493184730628, -1.66463355820282,
-1.6694061838583, -1.6887949755837, -1.67417880951378, -1.66493184730628,
-1.64285845364968, -1.63748924978726, -1.63152346771791, -1.61601243433759,
-1.62615426385549, -1.62078505999307, -1.60616889392316, -1.58976299323243,
-1.56082895019607, -1.53875555653946, -1.51548900646899, -1.47790457943206,
-1.48088747046674, -1.47074564094884, -1.41615873501425, -1.43345950301538,
-1.40631519459982, -1.36544958742475, -1.32577713666355, -1.32816344949129,
-1.3189164872838, -1.2792440365226, -1.17722916313666, -1.1805103432748
), Economicgrowth = c(-0.419475860074214, 1.77103185704958, -1.24091625399564,
0.219422224086891, -0.693289324714688, -1.14964509911548, -0.282569127753977,
-0.738924902154767, -0.282569127753977, -0.14566239543374, 0.26505780152697,
-0.191297972873819, -0.14566239543374, 0.26505780152697, -0.14566239543374,
0.0825154917666544, 0.630142421047602, 0.128151069206733, 0.26505780152697,
0.173786646646812, 0.0825154917666544, -0.647653747274609, 0.0368799143265754,
0.675777998487681, 0.128151069206733, 0.72141357592776, -0.191297972873819,
-0.14566239543374, 0.26505780152697, 0.0825154917666544), Consumptiongrowth = c(1.49948934326176,
2.80290969177971, 2.43863500922448, 3.0742184397245, 3.29809558837824,
1.78028441106475, 1.98708618397371, 1.30407115418265, 1.06311862978413,
1.41221520056623, 1.19213257985578, 1.30976294609757, 0.943590999570694,
0.717816586945313, 0.803193465669196, 0.856316856875168, 0.915132039996066,
0.932207415740842, 0.877186760563229, 0.550857357440829, 0.359233696305002,
0.484453118433365, 0.0879249483602165, 0.410459823539332, 0.437021519142318,
0.419946143397541, 0.898056664251289, 0.717816586945313, 0.896159400279647,
0.962563639287112), Governmentexpenditure = c(-1.40005492084802,
-1.38413625639177, -1.36971935273328, -1.35410104043658, -1.33277603710839,
-1.31355349889707, -1.28802356533516, -1.2715041965598, -1.26880102712384,
-1.2558858842631, -1.24627461515744, -1.24417215004058, -1.22765278126522,
-1.21503799056404, -1.2159390470427, -1.20692848225614, -1.18259995733244,
-1.17989678789647, -1.18139854869423, -1.17058587095036, -1.17509115334364,
-1.14956121978173, -1.13814783771876, -1.13093938588951, -1.13274149884682,
-1.12853656861309, -1.1210277646243, -1.11622213007147, -1.09820100049835,
-1.07867811012748), Longtermgovernmentbondyield = c(1.40229182288022,
1.52084996657255, 1.52084996657255, 2.1284604529957, 2.10623080105339,
1.8221852484572, 1.74561644732258, 1.8221852484572, 1.79254571253412,
2.04695172920723, 2.32358739782265, 2.3705166630342, 1.87899435897644,
1.81477536447643, 1.26150402724559, 1.27879375653405, 1.22939452999558,
1.13306603824557, 1.19728503274558, 1.07872688905325, 1.0515573144571,
1.28126371786097, 1.19234511009173, 1.10342650232249, 1.21704472336097,
1.14294588355326, 1.05649723711094, 1.08119685038018, 0.811971065745526,
0.752691993899364), BankRate = c(1.46586697149636, 1.35154387389459,
1.66960408302535, 1.97274804858215, 2.29546045675764, 2.29546045675764,
2.09326712428386, 1.92386866579365, 1.57025981463491, 1.25236650053715,
1.3619748134568, 1.79713275011286, 1.62283175002842, 1.46140252936374,
1.11755703763521, 0.767494705927617, 1.00504892351727, 0.836192873884299,
0.743461821176297, 0.639548801257612, 0.614285065637952, 0.59615609267884,
1.0233656533885, 0.831874464905546, 1.44483819733896, 1.35112663631211,
1.16530987895099, 1.12197975600959, 1.27587783831036, 0.9050787987531
), ConsumerConfidenceIndex = c(0.846829650502804, 1.60472118016078,
0.469774413325411, -0.621585188209513, -1.60430771883233, -1.68373613752742,
-1.63528611498984, -1.66413766753932, -1.70435864332681, -1.66902678210495,
-1.59673510529459, -1.59602090595954, -0.999706472923775, 0.0694026684451194,
0.236084189726601, 0.627318384290896, 0.856229774103751, 1.00626414911988,
1.18224076468833, 1.53172580694677, 1.23795881575313, 0.203420073079754,
0.031697144727374, 0.197538431497049, -0.4751060554715, -0.723547646218374,
-0.38459179268613, -0.144363494292252, -0.345373846847121, -0.204466519215588
), RealPersonalDisposableIncome = c(-1.61847984374121, -1.5861635599299,
-1.57225712600034, -1.49848239094777, -1.53946529757862, -1.54497109045523,
-1.52492940490883, -1.54175692919285, -1.53951841454553, -1.57492545682787,
-1.59099234107728, -1.58530147051583, -1.59103377902389, -1.5730264259375,
-1.58525138856384, -1.58529970465885, -1.58192644597089, -1.52325368596072,
-1.53501582928257, -1.52925478110938, -1.50631522176011, -1.51590689703409,
-1.46151870482198, -1.40729486382567, -1.3789215787237, -1.3693640758863,
-1.36306692499173, -1.34201378673398, -1.29684320450097, -1.22228202649678
), PersonalDisposableIncome = c(-1.63374935499688, -1.61912533368493,
-1.59170800880699, -1.55197024453754, -1.5482842190616, -1.53111502598353,
-1.51272050030567, -1.496600325591, -1.47962148432076, -1.47543914833698,
-1.46603002900108, -1.44363362658138, -1.43430337270118, -1.41678955114361,
-1.41553667304378, -1.4009808145215, -1.39475625982839, -1.36509006246891,
-1.3676348839483, -1.35648303480995, -1.33818372341837, -1.33674498698592,
-1.3094343652563, -1.28373893947937, -1.27703100460648, -1.26432160075519,
-1.25275608335059, -1.23583637919461, -1.22110869865322, -1.18194367906969
), SPPricechange = c(1.26871538917159, 0.851034944363406, -0.128615553459435,
0.296659081254357, -2.70557732809419, 0.554861538044873, 0.509296398611252,
-0.98675901279262, 0.782687235212975, -0.391880803520353, -1.49050694319765,
-0.890565940654977, -0.386818010249951, -0.642489070405266, 0.0637705908158519,
0.256156735091138, 0.709276732792142, 0.689025559710533, 0.129586903331082,
-0.351378457357135, 0.213122992292719, 0.314378857700765, -0.217214435691474,
0.43841729282562, -0.202026055880268, 0.177683439399903, -1.00701018587423,
1.01304432901628, 1.20796186992677, 0.228311372103926), HouseStarts = c(-0.752212186140825,
1.99649271779397, 1.8436488194287, 1.69950335430372, -0.603096187735683,
0.0865653048881011, -0.685109986858511, -1.57110754404907, -1.21198651455668,
-0.257644124763769, -0.214151958562269, -0.962217217228067, -0.0128453607153271,
0.914159096036641, 0.712852498189699, 0.0766242383277583, 1.13783309364436,
1.96915478475302, 1.07942932760234, 0.407164701459157, 0.581133366265157,
1.28073592544928, 0.633323965706957, -0.424156989649512, -0.316047890805783,
1.23848639256783, 0.96137916219827, 0.337577235536758, -0.0811901933176841,
1.76039238698582), HouseCompleted = c(0.727121448038834, 1.51512215827386,
1.27770235093872, 4.62342810215839, 1.63310378032383, 1.60397251562013,
1.87343671412934, 2.03511523323486, 1.11893695830357, 0.288695914248185,
0.362980639242614, 0.395025030416681, -0.395888806288709, -0.695940832736795,
-0.378410047466491, 0.467853192175925, -0.349278782762793, 0.483875387762959,
0.46930975541111, 1.47288182445349, 0.598943883342565, 0.362980639242614,
0.829080874501776, 1.38985772004796, 0.143039590729697, 0.281413098072261,
0.138669901024142, 0.973280634785079, -0.238579976888742, 0.502810709820363
), TermSpread = c(-0.982486215892947, -1.35440528815526, -1.77852352845439,
-2.1243430166983, -2.91385543325514, -2.47668740094681, -1.8046231124728,
-0.891137671828519, 0.83143487338641, 1.14462988160731, -0.499643911552399,
-0.127724839290084, 0.43994111311029, 0.727036537312779, 0.916258521446237,
0.629163097243749, 0.37469215306427, 0.857534457404817, 0.968457689483053,
0.968457689483053, 1.18377925763492, 1.444775097819, 0.335542777036658,
0.981507481492256, -1.19780778404481, -0.89766256783312, -0.656241415662847,
-0.538793287580011, -1.4718534162381, -0.1799240073269), BuildingPermits = c(-0.757294212433685,
1.98548228249596, 1.83296803797682, 1.68913346590998, -0.608499827536959,
0.0796742026103982, -0.690336739230158, -1.5744233761582, -1.21607689919859,
-0.263792835859544, -0.220394473597999, -0.966846304496574, -0.0195220539874191,
0.905483038787227, 0.704610619176647, 0.0697545769506165, 1.12867461613232,
1.95820331193156, 1.0703968153811, 0.399582130138359, 0.573175579184539,
1.27126923499168, 0.625253613898393, -0.429946565660888, -0.322070636610762,
1.22911082593761, 0.95260126067119, 0.330144750519887, -0.0877194803984184,
1.74989117307615), Date = structure(c(289094400, 297043200, 304992000,
312854400, 320716800, 328665600, 336614400, 344476800, 352252800,
360201600, 368150400, 376012800, 383788800, 391737600, 399686400,
407548800, 415324800, 423273600, 431222400, 439084800, 446947200,
454896000, 462844800, 470707200, 478483200, 486432000, 494380800,
502243200, 510019200, 517968000), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), NORTH = c(4.06976744186047, 5.51675977653633,
7.2799470549305, 4.75015422578655, 4.59363957597172, 3.15315315315317,
1.2008733624454, -0.377562028047452, -0.108283703302655, 0.650406504065032,
0.969305331179318, 0.106666666666688, 3.09003729355352, 2.11886304909562,
2.32793522267207, 5.68743818001977, -1.46934955545156, 3.95611702127658,
5.19438987619354, -0.0912012507600199, 2.81677896109541, 3.97412590369087,
1.30118326353028, 3.31553807249226, 1.32872294960955, 2.93700394923507,
0.908853875665812, 1.81241002546971, -1.3414545718222, 4.81772747317361
), YORKSANDTHEHUMBER = c(4.0121120363361, 5.45851528384282, 9.52380952380951,
6.04914933837431, 3.03030303030299, 5.42099192618225, 2.78993435448577,
-0.53219797764768, 1.97966827180309, 1.15424973767052, 0.466804979253115,
-1.96179659266907, 2.42232754081095, 0.719794344473031, -0.306278713629415,
3.37941628264209, 2.74393263992076, 3.91920555341303, 1.91585099967527,
0.892125625853447, 2.91888477848958, 3.78293078507868, 0.109815847271484,
6.83486625601216, 0.722691730511011, 3.56008625759656, -0.227160867754524,
2.69419041475355, -1.17134094520194, 2.78546324684064), NORTHWEST = c(6.57894736842105,
6.95256660168939, 6.50060753341436, 5.5904164289789, 4.59211237169096,
4.70041322314051, 2.96003946719288, -1.38955438428365, 0.242954324586984,
2.18128938439167, -0.853889943073994, -2.15311004784691, 0.929095354523226,
2.51937984496125, 0.189035916824195, 2.21698113207546, 2.51499769266268,
3.5066396578888, 1.77437592415414, 0.948636868643719, 4.60125296308836,
3.95775160859537, -0.237455720347246, 4.218042765725, 2.79306600771276,
2.22545984338008, 0.709042970141798, 0.258269945161875, 0.663420142564747,
2.23655612423752), EASTMIDS = c(4.98489425981872, 8.20143884892085,
6.91489361702127, 5.22388059701494, 5.61465721040189, 4.64465584778958,
2.03208556149733, 0.314465408805028, 2.82131661442007, 0, 2.79471544715448,
-0.939199209095414, -1.14770459081835, 2.97829379101462, -0.68627450980392,
3.40572556762095, 3.42243436754175, 4.89223242719342, 0.730408764905171,
2.10107893242476, 2.31025926242835, 5.01798109893785, 0.382256908497274,
4.64894882982943, 3.04374194526571, 2.25491999264298, 0.651125980286367,
1.40105078809108, 2.87265165133409, 3.59418899472349), WESTMIDS = c(4.65838509316771,
4.74777448071216, 8.66855524079319, 6.56934306569344, 3.22896281800389,
3.17535545023698, 0.643086816720257, -1.36923779096303, 1.61962054604351,
2.00364298724953, -0.491071428571428, -2.78151637505608, 0, 2.39963082602676,
0.540784136998647, 1.83774092335275, 4.66989436619718, 1.82498633362771,
2.51909973157134, 0.644511581067457, 3.9503702221333, 3.15724626520867,
0.548671245147809, 4.19837410445824, 3.20983256145349, 1.12526319422872,
1.4028740144042, 0.434226470984247, -0.194389516372279, 2.32714328889485
), EASTANGLIA = c(6.74525212835624, 8.58895705521476, 8.47457627118643,
10.7291666666667, 4.8447789275635, 4.84522207267835, -0.299529311082601,
1.45922746781116, 0.88832487309645, 0.29350104821803, -0.877926421404701,
1.64487557992411, -2.69709543568468, 3.49680170575694, 3.25504738360115,
2.39425379090184, 2.98519095869059, 4.36691137516082, 3.57868020304568,
1.66275772744776, 3.79450451070863, 4.52162951167727, 2.28203256419209,
4.17054552224914, 3.2439678284182, 4.76643873164257, 0.955633279171614,
2.91614381581101, 0.848198902642676, 5.02010671012167), OUTERSEAST = c(6.7110371602884,
7.53638253638255, 9.47317544707589, 8.56512141280351, 3.82269215128102,
2.11515863689776, 1.64940544687381, -1.73584905660378, 1.34408602150539,
1.78097764304659, 0.446760982874161, -1.26019273535953, 0.150150150150159,
3.11094452773611, 1.4176663031625, 2.54480286738352, 5.56448794127927,
4.89371564797033, 3.88257575757575, 1.85961713764815, 5.54859495256845,
4.29879599796508, 2.00525702517411, 3.63679834232127, 3.44509381728699,
3.46664684309643, 1.93988743863012, 2.50440502760482, 2.96578121060713,
4.47634947134114), OUTERMET = c(4.54545454545458, 6.58505698607005,
7.36633663366336, 7.08225746956843, 4.3747847054771, 1.68316831683168,
1.00616682895164, -1.28534704370181, 2.01822916666665, 0.797702616464613,
0.949667616334271, -0.940733772342415, 1.10794555238999, 2.19160926737633,
2.84926470588237, 2.62138814417631, 5.02467343976781, 5.65213786241397,
3.22555328833776, 3.73552294786995, 5.05948745510956, 4.28797321179426,
2.86300392436674, 2.60339894216597, 4.28031183318191, 3.43199821714381,
3.34554286721641, 3.04770569170409, 1.65167650683293, 4.62120252591965
), LONDON = c(8.11719500480309, 10.3065304309196, 6.32299637535239,
7.65151515151515, 1.30190007037299, 2.1535255296978, -0.204012240734436,
-0.306643952299836, 0.786056049213951, 1.18684299762631, 1.00536193029493,
-2.85335102853352, 2.76639344262296, 2.06048521103356, 1.23738196027352,
2.70183338694115, 3.30410272471031, 5.76322570865546, 4.73255747291176,
1.98428989791171, 6.03563952552197, 4.88977753030802, 2.12581135535556,
4.43247330120026, 5.42986425339366, 3.96781115879828, 3.43247538648888,
4.0668901660281, 4.09587727708534, 4.81707991010573), SOUTHWEST = c(6.17577197149644,
7.71812080536912, 7.63239875389407, 9.45489628557649, 2.46804759806079,
2.19354838709679, 1.72558922558922, 0.248241621845247, 1.48576145274456,
2.03334688897925, -0.677560781187733, -2.3274478330658, 1.80772391125718,
2.42130750605327, 1.85185185185186, 0.928433268858785, 5.95247221157533,
4.38447346525341, 3.30272049904696, 2.25107353730542, 3.86823714688802,
2.04371722787289, 3.04596811639065, 4.19057346270538, 2.45646407565451,
2.17525889239081, 2.83400809716597, 1.58015962290428, 2.77894958869438,
4.08650146221331), WALES = c(6.09418282548476, 8.35509138381203,
7.40963855421687, 7.01065619742007, 1.15303983228513, 3.47150259067357,
-0.150225338007013, 0.852557673019058, 0.944803580308295, -1.13300492610835,
0.946686596910786, -2.17176702862782, 3.98587285570131, 0.485201358563789,
3.62143891839691, 1.63094128611373, 1.61852361302152, 4.32251951450617,
1.28887158859911, 0.68747598104105, 3.71925360474978, 4.66941979801284,
1.44927536231884, 1.05121293800539, 1.67663757954501, 2.9419480568152,
-0.422309596621509, 2.67987715706347, 0.0249243368346056, 2.03260714794249
), SCOTLAND = c(5.15222482435597, 4.12026726057908, 5.40106951871658,
8.67579908675796, -0.280112044817908, 2.94943820224719, 1.04592996816735,
1.21512151215122, 1.33392618941751, 3.59806932865292, 0.974163490046604,
0.125838926174496, 1.46627565982404, 3.42691990090835, -0.838323353293421,
1.97262479871176, 3.40702724042636, 4.30649410147751, 2.44866586142527,
1.93997856377279, 2.09581887638873, 4.22573890357352, 0.833278440155458,
4.15155969296095, 2.01655899140689, 1.93980755633434, 0.325693606755129,
0.796561260069754, -0.381713535919834, 2.90974405029185), NIRELAND = c(4.54545454545454,
4.94752623688156, 4.42857142857145, 2.96397628818967, 6.06731620903454,
0.0835073068893502, -1.66875260742594, -2.96987696224015, -1.18058592041975,
-0.884955752212393, -1.74107142857143, -0.545206724216265, 1.96436729100047,
-0.224014336917564, -1.84104176021554, 1.6010978956999, 1.42278253039172,
1.97993429814437, 1.29287828660979, 1.61158623060724, 2.28387751649466,
1.84005954349984, 1.79057208981284, 2.22177901874749, 2.88757950598978,
-0.731975575530031, 3.07939176281808, -0.0593031875463392, -1.05696484201158,
3.40717418194087), UK = c(5.76890543055322, 7.20302836425676,
7.39543442582184, 7.22885986848197, 3.23472252213347, 2.95766398929048,
1.20271423347285, -0.554061107319231, 0.98913965036942, 1.55113136643479,
0.373986300291293, -1.61195434757029, 1.59052858167903, 2.07573082205217,
1.17628969016684, 2.44680851063832, 2.84453345201007, 4.10010457610617,
2.88208396840793, 1.58922558922557, 3.67559326527908, 3.90013106997858,
1.36611181194425, 4.12505691303686, 2.02017257462689, 2.93167985827357,
1.54068234183715, 2.12149379408387, 0.594313861969269, 3.83755588673622
)), row.names = c(NA, 30L), class = "data.frame")
Code:
library(tidyverse)
library(GGally)
library(Amelia)
library(inspectdf)
library(ggcorrplot)
library(ggplot2)
library(reshape2)
library(tseries)
library(dplyr)
library(caret)
library(tidyverse)
library(ARDL)
library(dLagM)
library(forecast)
in_sampleARDL <- data %>%
dplyr::filter(Date < '2020-03-01')
out_sampleARDL <-data %>%
dplyr::filter(Date >= '2020-03-01')
# Model Building
# Create the formulas
indep_vars <- expression(Industrialproduction, Householdconsumption, Investmentgrowth, ConsumerPriceIndex, Employment, Unemploymentrate,
Stockmarketindex, Economicgrowth, Consumptiongrowth, Governmentexpenditure, Longtermgovernmentbondyield,
BankRate, ConsumerConfidenceIndex, RealPersonalDisposableIncome, PersonalDisposableIncome, SPPricechange,
HouseStarts, HouseCompleted, TermSpread, BuildingPermits)
dep_vars <- expression(NORTH, YORKSANDTHEHUMBER, NORTHWEST, EASTMIDS, WESTMIDS, EASTANGLIA, OUTERSEAST, OUTERMET, LONDON,
SOUTHWEST, WALES, SCOTLAND, NIRELAND, UK)
# Formulae with diff()
formulae <- unlist(lapply(dep_vars, \(x) lapply(indep_vars, \(y) bquote(.(x)~diff(.(y))))))
length(formulae)
# Without diff()
formulae2 <- unlist(lapply(dep_vars, \(x) lapply(indep_vars, \(y) bquote(.(x)~.(y)))))
length(formulae2)
result <- vector('list', length = length(formulae))
names(result) <- formulae2
# Loop for H = 4
for (i in seq_along(formulae)){
# auto_ardl
result[[i]][[1]] <- auto_ardl(formula(formulae2[[i]]),
data = in_sampleARDL, max_order = 4, selection = 'BIC')
# prediction
result[[i]][[2]] <- forecast(ardlDlm(formula = formula(formulae[[i]]), data = in_sampleARDL, p = 3),
x = out_sampleARDL |> select(sub("\\s~.*", "", formula(formulae[[i]]))) |> pull(), h = 4)
# error
result[[i]][[3]] <- mean((out_sampleARDL |> select(sub("\\s~.*", "", formula(formulae[[i]]))) |> pull() |> (\(x) x[1:4])() - result[[i]][[2]][["forecasts"]])^2)
# set names
names(result[[i]]) <- c('auto_ardl','forecast','error')
}
print(result[[i]])
traceback() shows that this error is coming from the forecast call, and that it occurs with i==1, so look at the first parameter to forecast :: ardlDlm(formula = formula(formulae[[i]]), data = in_sampleARDL, p = 3) and realize that it is not something that forecast is designed to work with. forecast was expecting an atomic numeric vector.
Looking at the output of ardlDlm(formula = formula(formulae[[1]]), data = in_sampleARDL, p = 3), it appears that you really want numeric vectors contained in the $data leaf of that much longer list and in particular probably want only the i-th column, so try this:
for (i in seq_along(formulae)){
# auto_ardl
result[[i]][[1]] <- auto_ardl(formula(formulae2[[i]]),
data = in_sampleARDL,
max_order = 4, selection = 'BIC')
# prediction
#
result[[i]][[2]] <- forecast(ardlDlm(formula = formula(formulae[[i]]),
#---------------extract one col------------------\/-\/-\/-
data = in_sampleARDL, p = 3)$data[[i]],
x = out_sampleARDL |>
select(sub("\\s~.*", "", formula(formulae[[i]]))) |>
pull(), h = 4)
# error
result[[i]][[3]] <- mean((out_sampleARDL |> select(sub("\\s~.*",
"", formula(formulae[[i]]))) |>
pull() |> (\(x) x[1:4])() -
result[[i]][[2]][["forecasts"]])^2)
# set names
names(result[[i]]) <- c('auto_ardl','forecast','error')
}
Note that you only printed the last value in the much longer result object. The last such value looks like:
print(result[[i]])
$auto_ardl
$auto_ardl$best_model
Time series regression with "ts" data:
Start = 5, End = 30
Call:
dynlm::dynlm(formula = full_formula, data = data, start = start,
end = end)
Coefficients:
(Intercept) L(UK, 1) BuildingPermits L(BuildingPermits, 1) L(BuildingPermits, 2)
1.59718 -0.04719 0.90441 -0.04269 0.19583
L(BuildingPermits, 3) L(BuildingPermits, 4)
0.63773 0.02544
$auto_ardl$best_order
[1] 1 4
$auto_ardl$top_orders
UK BuildingPermits BIC
1 1 4 90.18992
2 2 4 93.11884
3 3 4 96.02905
4 1 3 98.36867
5 4 4 99.15721
6 3 3 100.20359
7 2 3 100.53056
8 2 2 104.78506
9 1 2 104.85999
10 1 1 106.10666
$forecast
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN
$error
[1] NaN

Plotting Conditionally Summed Data (base R or ggplot)

I started with a dataframe containing info on West Nile cases in Canada from 2012-2015. 600 observations of 10 variables in total.
> head(mosquitoes)
Years Weeks Province Avg.Temp Avg..Precepitation Wind Number.of.cases Number.of.Dead.Birds Mosquito.Pools.Tested Google.Trend.Searches
1 2015 17 Alberta 48 0.01 8 0 0 0 1
2 2015 18 Alberta 46 0.03 10 0 0 0 2
3 2015 19 Alberta 44 0.07 8 0 0 0 2
4 2015 20 Alberta 51 0.00 9 0 0 0 2
5 2015 21 Alberta 56 0.01 9 0 0 0 4
6 2015 22 Alberta 58 0.10 7 0 0 0 1
Here is the entire data set....sorry it's large.
Years,Weeks,Province,Avg Temp ,Avg. Precepitation,Wind,Number of cases,Number of Dead Birds,Mosquito Pools Tested,Google Trend Searches
2015,17,Alberta,48,0.01,8,0,0,0,1
2015,18,Alberta,46,0.03,10,0,0,0,2
2015,19,Alberta,44,0.07,8,0,0,0,2
2015,20,Alberta,51,0,9,0,0,0,2
2015,21,Alberta,56,0.01,9,0,0,0,4
2015,22,Alberta,58,0.1,7,0,0,0,1
2015,23,Alberta,61,0.05,8,0,0,0,1
2015,24,Alberta,55,0.08,9,0,0,0,1
2015,25,Alberta,63,0.02,6,0,0,0,4
2015,26,Alberta,67,0.16,8,0,0,0,5
2015,27,Alberta,65,0.02,8,0,0,0,3
2015,28,Alberta,62,0.09,10,0,0,0,7
2015,29,Alberta,66,0.01,8,0,0,0,2
2015,30,Alberta,62,0.02,7,0,0,0,3
2015,31,Alberta,64,0.21,7,0,0,0,6
2015,32,Alberta,66,0.07,7,0,0,0,4
2015,33,Alberta,55,0.13,8,0,0,0,4
2015,34,Alberta,63,0,6,0,0,0,1
2015,35,Alberta,52,0.11,9,0,0,0,4
2015,36,Alberta,54,0.02,7,0,0,0,2
2015,37,Alberta,48,0.06,8,0,0,0,2
2015,38,Alberta,52,0.03,9,0,0,0,3
2015,39,Alberta,49,0.03,9,0,0,0,3
2015,40,Alberta,51,0,8,0,0,0,2
2015,41,Alberta,48,0,8,0,0,0,2
2014,17,Alberta,43,0.05,8,0,0,0,1
2014,18,Alberta,44,0.06,9,0,0,0,3
2014,19,Alberta,37,0.03,9,0,0,0,3
2014,20,Alberta,48,0.01,8,0,0,0,1
2014,21,Alberta,57,0.01,10,0,0,0,2
2014,22,Alberta,53,0.06,8,0,0,0,4
2014,23,Alberta,53,0.04,10,0,0,0,6
2014,24,Alberta,53,0.04,10,0,0,0,6
2014,25,Alberta,54,0.24,9,0,0,0,4
2014,26,Alberta,59,0.03,9,0,0,0,7
2014,27,Alberta,64,0.02,11,0,0,0,19
2014,28,Alberta,65,0.03,10,0,0,0,33
2014,29,Alberta,67,0.01,9,0,0,0,18
2014,30,Alberta,62,0.08,10,0,0,0,14
2014,31,Alberta,68,0,10,0,0,0,10
2014,32,Alberta,63,0.16,8,0,0,0,11
2014,33,Alberta,66,0.01,7,0,0,0,19
2014,34,Alberta,58,0.05,8,0,0,0,17
2014,35,Alberta,58,0.04,7,0,0,0,8
2014,36,Alberta,54,0.01,7,0,0,0,12
2014,37,Alberta,41,0.15,8,0,0,0,3
2014,38,Alberta,58,0,5,0,0,0,3
2014,39,Alberta,60,0.02,6,0,0,0,4
2014,40,Alberta,48,0.03,11,0,0,0,5
2014,41,Alberta,51,0,6,0,0,0,3
2013,17,Alberta,42,0,12,0,0,0,3
2013,18,Alberta,42,0.01,11,0,0,0,2
2013,19,Alberta,57,0,11,0,0,0,2
2013,20,Alberta,55,0.01,10,0,0,0,9
2013,21,Alberta,50,0.23,11,0,0,0,7
2013,22,Alberta,52,0.08,6,0,0,0,8
2013,23,Alberta,55,0.15,10,0,0,0,10
2013,24,Alberta,53,0.08,10,0,0,0,4
2013,25,Alberta,57,0.3,11,0,0,0,9
2013,26,Alberta,61,0.01,9,0,0,0,17
2013,27,Alberta,65,0.08,10,0,0,0,27
2013,28,Alberta,59,0.07,8,0,0,0,19
2013,29,Alberta,62,0.01,10,0,0,0,21
2013,30,Alberta,62,0.06,10,0,0,0,18
2013,31,Alberta,57,0.03,7,0,0,0,13
2013,32,Alberta,60,0.07,8,0,0,0,10
2013,33,Alberta,67,0,8,3,0,0,2
2013,34,Alberta,63,0,8,5,0,0,12
2013,35,Alberta,64,0.03,10,4,0,0,20
2013,36,Alberta,64,0.13,8,2,1,0,15
2013,37,Alberta,63,0,9,5,0,0,9
2013,38,Alberta,57,0.06,11,2,0,0,11
2013,39,Alberta,47,0,10,0,0,0,4
2013,40,Alberta,44,0,11,0,0,0,5
2013,41,Alberta,45,0.06,8,0,0,0,5
2012,17,Alberta,49,0.06,7,0,0,0,2
2012,18,Alberta,42,0.13,9,0,0,0,2
2012,19,Alberta,48,0,9,0,0,0,6
2012,20,Alberta,53,0.01,10,0,0,0,2
2012,21,Alberta,49,0.08,8,0,0,0,2
2012,22,Alberta,52,0,9,0,0,0,2
2012,23,Alberta,54,0.28,9,0,0,0,4
2012,24,Alberta,56,0.21,12,0,0,0,7
2012,25,Alberta,56,0.05,8,0,0,0,5
2012,26,Alberta,59,0.14,8,0,0,0,3
2012,27,Alberta,61,0.21,9,0,0,0,22
2012,28,Alberta,69,0,8,0,0,0,32
2012,29,Alberta,65,0.09,10,0,0,0,16
2012,30,Alberta,64,0.02,10,0,0,0,15
2012,31,Alberta,63,0.03,10,0,0,0,20
2012,32,Alberta,68,0,10,0,0,0,25
2012,33,Alberta,62,0.07,10,4,0,0,36
2012,34,Alberta,62,0.05,10,2,0,0,100
2012,35,Alberta,61,0.01,10,0,0,0,76
2012,36,Alberta,57,0,12,1,0,0,29
2012,37,Alberta,57,0,12,2,0,0,30
2012,38,Alberta,59,0,9,0,0,0,14
2012,39,Alberta,58,0.01,9,0,0,0,11
2012,40,Alberta,43,0.07,12,0,0,0,10
2012,41,Alberta,43,0.02,13,0,0,0,7
2015,17,British Columbia,53,0.03,10,0,0,0,5
2015,18,British Columbia,53,0.01,6,0,0,0,5
2015,19,British Columbia,58,0.01,7,0,0,0,5
2015,20,British Columbia,60,0,7,0,0,0,4
2015,21,British Columbia,62,0,7,0,0,0,6
2015,22,British Columbia,60,0.03,7,0,0,0,9
2015,23,British Columbia,62,0,13,0,0,0,9
2015,24,British Columbia,62,0.02,8,0,0,0,10
2015,25,British Columbia,66,0,9,0,0,0,7
2015,26,British Columbia,70,0,12,0,0,0,5
2015,27,British Columbia,67,0.01,9,0,0,0,11
2015,28,British Columbia,66,0,10,0,0,0,9
2015,29,British Columbia,65,0.04,9,0,0,0,14
2015,30,British Columbia,65,0.04,6,0,0,0,7
2015,31,British Columbia,65,0.02,9,0,0,0,7
2015,32,British Columbia,66,0.04,9,0,0,0,9
2015,33,British Columbia,65,0,9,0,0,0,11
2015,34,British Columbia,64,0.1,7,0,0,0,6
2015,35,British Columbia,57,0.12,10,0,0,0,4
2015,36,British Columbia,61,0.02,9,0,0,0,9
2015,37,British Columbia,58,0.09,9,0,0,0,9
2015,38,British Columbia,55,0.04,9,0,0,0,3
2015,39,British Columbia,52,0,6,0,0,0,3
2015,40,British Columbia,56,0.08,6,0,0,0,3
2015,41,British Columbia,51,0.04,7,0,0,0,7
2014,17,British Columbia,49,0.07,10,0,0,0,3
2014,18,British Columbia,54,0.03,8,0,0,0,4
2014,19,British Columbia,53,0.18,9,0,0,0,4
2014,20,British Columbia,60,0,8,0,0,0,6
2014,21,British Columbia,59,0.06,7,0,0,0,6
2014,22,British Columbia,56,0.09,7,0,0,0,6
2014,23,British Columbia,59,0,8,0,0,0,8
2014,24,British Columbia,60,0.03,10,0,0,0,7
2014,25,British Columbia,58,0.09,9,0,0,0,8
2014,26,British Columbia,62,0.05,7,0,0,0,10
2014,27,British Columbia,64,0.01,8,0,0,0,7
2014,28,British Columbia,66,0.01,8,0,0,0,19
2014,29,British Columbia,68,0,9,0,0,0,13
2014,30,British Columbia,63,0.06,8,0,0,0,12
2014,31,British Columbia,67,0,6,0,0,0,16
2014,32,British Columbia,66,0,7,0,0,0,25
2014,33,British Columbia,67,0.08,7,0,0,0,17
2014,34,British Columbia,65,0,6,0,0,0,13
2014,35,British Columbia,66,0,7,0,0,0,30
2014,36,British Columbia,61,0.05,7,0,0,0,9
2014,37,British Columbia,60,0,6,0,0,0,11
2014,38,British Columbia,61,0.02,6,0,0,0,3
2014,39,British Columbia,62,0.12,9,0,0,0,8
2014,40,British Columbia,56,0.04,6,0,0,0,9
2014,41,British Columbia,58,0.03,5,0,0,0,7
2013,17,British Columbia,50,0.03,7,0,0,0,14
2013,18,British Columbia,50,0,12,0,0,0,8
2013,19,British Columbia,59,0.03,6,0,0,0,5
2013,20,British Columbia,56,0.07,8,0,0,0,7
2013,21,British Columbia,54,0.04,8,0,0,0,4
2013,22,British Columbia,55,0.09,7,0,0,0,8
2013,23,British Columbia,60,0.01,9,0,0,0,14
2013,24,British Columbia,58,0.01,7,0,0,0,16
2013,25,British Columbia,62,0.04,8,0,0,0,10
2013,26,British Columbia,63,0.1,7,0,0,0,17
2013,27,British Columbia,67,0,8,0,0,0,29
2013,28,British Columbia,63,0,8,0,0,0,30
2013,29,British Columbia,66,0,9,0,0,0,20
2013,30,British Columbia,64,0,8,0,0,0,34
2013,31,British Columbia,64,0.02,8,0,0,0,11
2013,32,British Columbia,66,0,6,0,0,1,13
2013,33,British Columbia,66,0.02,8,0,0,1,16
2013,34,British Columbia,63,0.01,8,0,0,1,16
2013,35,British Columbia,65,0.17,7,0,1,1,12
2013,36,British Columbia,64,0.06,6,0,0,1,8
2013,37,British Columbia,63,0,6,0,0,1,14
2013,38,British Columbia,60,0.19,6,0,0,1,6
2013,39,British Columbia,54,0.23,10,0,0,1,6
2013,40,British Columbia,51,0.15,9,0,0,1,6
2013,41,British Columbia,51,0.01,8,0,0,1,8
2012,17,British Columbia,53,0.05,8,0,0,0,5
2012,18,British Columbia,50,0.11,7,0,0,0,6
2012,19,British Columbia,52,0,9,0,0,0,7
2012,20,British Columbia,54,0,10,0,0,0,8
2012,21,British Columbia,55,0.06,8,0,0,0,9
2012,22,British Columbia,57,0.07,7,0,0,0,8
2012,23,British Columbia,53,0.07,8,0,0,0,4
2012,24,British Columbia,57,0.04,8,0,0,0,4
2012,25,British Columbia,58,0.13,8,0,0,0,7
2012,26,British Columbia,60,0.04,8,0,0,0,8
2012,27,British Columbia,59,0.03,7,0,0,0,22
2012,28,British Columbia,66,0,6,0,0,0,30
2012,29,British Columbia,66,0.05,8,0,0,0,30
2012,30,British Columbia,63,0.03,8,0,0,0,38
2012,31,British Columbia,65,0,8,0,0,0,60
2012,32,British Columbia,67,0.01,8,0,0,0,34
2012,33,British Columbia,69,0,7,0,0,0,63
2012,34,British Columbia,63,0,8,0,0,0,100
2012,35,British Columbia,62,0,7,0,0,0,51
2012,36,British Columbia,62,0,7,0,0,0,32
2012,37,British Columbia,58,0.01,8,0,0,0,24
2012,38,British Columbia,60,0,6,0,0,0,13
2012,39,British Columbia,57,0,6,0,0,0,13
2012,40,British Columbia,53,0,8,0,0,0,6
2012,41,British Columbia,52,0.09,5,0,0,0,8
2015,17,Manitoba,56,0,10,0,0,0,4
2015,18,Manitoba,48,0,13,0,0,0,4
2015,19,Manitoba,46,0,10,0,0,0,4
2015,20,Manitoba,52,0,14,0,0,0,4
2015,21,Manitoba,57,0,10,0,0,12,4
2015,22,Manitoba,60,0,12,0,0,4,8
2015,23,Manitoba,67,0,9,0,0,87,8
2015,24,Manitoba,59,0,9,0,0,82,8
2015,25,Manitoba,66,0,7,0,0,44,8
2015,26,Manitoba,68,0,7,0,0,75,11
2015,27,Manitoba,66,0,10,0,0,73,17
2015,28,Manitoba,70,0,7,0,0,132,8
2015,29,Manitoba,69,0,9,0,0,139,17
2015,30,Manitoba,70,0,11,0,0,204,4
2015,31,Manitoba,63,0,9,0,0,275,13
2015,32,Manitoba,73,0,9,0,0,195,23
2015,33,Manitoba,62,0,10,0,0,228,13
2015,34,Manitoba,62,0,11,0,0,69,12
2015,35,Manitoba,73,0,11,1,0,92,10
2015,36,Manitoba,57,0,10,1,0,113,8
2015,37,Manitoba,60,0,11,2,0,34,4
2015,38,Manitoba,61,0,13,1,0,0,4
2015,39,Manitoba,53,0,13,0,0,0,6
2015,40,Manitoba,48,0,11,0,0,0,6
2015,41,Manitoba,44,0,11,0,0,0,6
2014,17,Manitoba,42,0,11,0,0,0,4
2014,18,Manitoba,42,0,14,0,0,0,0
2014,19,Manitoba,46,0,9,0,0,0,0
2014,20,Manitoba,45,0,10,0,0,0,0
2014,21,Manitoba,57,0,12,0,0,0,0
2014,22,Manitoba,66,0,8,0,0,0,0
2014,23,Manitoba,62,0,10,0,0,0,5
2014,24,Manitoba,60,0,11,0,0,0,13
2014,25,Manitoba,62,0,12,0,0,0,9
2014,26,Manitoba,66,0,10,0,0,0,7
2014,27,Manitoba,65,0,15,0,0,0,9
2014,28,Manitoba,67,0,11,0,0,0,36
2014,29,Manitoba,63,0,11,0,0,0,24
2014,30,Manitoba,68,0,9,0,0,0,53
2014,31,Manitoba,65,0,8,0,0,7,41
2014,32,Manitoba,71,0,8,0,0,7,48
2014,33,Manitoba,68,0,8,1,0,14,14
2014,34,Manitoba,67,0,8,2,0,19,18
2014,35,Manitoba,61,0,11,2,0,22,9
2014,36,Manitoba,60,0,8,0,0,24,4
2014,37,Manitoba,50,0,11,0,0,24,11
2014,38,Manitoba,52,0,10,0,0,24,4
2014,39,Manitoba,65,0,13,0,0,24,15
2014,40,Manitoba,47,0,16,0,0,24,4
2014,41,Manitoba,39,0,13,0,0,24,4
2013,17,Manitoba,36,0.01,12,0,0,0,4
2013,18,Manitoba,38,0.11,9,0,0,0,4
2013,19,Manitoba,49,0.02,12,0,0,0,4
2013,20,Manitoba,56,0.02,10,0,0,0,5
2013,21,Manitoba,55,0.05,14,0,0,0,4
2013,22,Manitoba,58,0.16,15,0,0,0,4
2013,23,Manitoba,57,0.01,9,0,0,0,9
2013,24,Manitoba,63,0.03,10,0,0,0,16
2013,25,Manitoba,66,0.1,9,0,0,0,23
2013,26,Manitoba,69,0.24,10,0,0,0,14
2013,27,Manitoba,72,0,6,0,0,0,23
2013,28,Manitoba,70,0.06,10,0,0,1,19
2013,29,Manitoba,66,0.1,9,0,0,1,45
2013,30,Manitoba,60,0.19,8,0,1,7,35
2013,31,Manitoba,61,0.03,7,0,0,10,31
2013,32,Manitoba,59,0.04,7,0,0,16,22
2013,33,Manitoba,64,0.02,8,1,0,16,24
2013,34,Manitoba,71,0.17,10,0,0,16,49
2013,35,Manitoba,76,0.01,7,0,0,17,14
2013,36,Manitoba,64,0,10,1,0,17,11
2013,37,Manitoba,63,0.01,8,0,0,19,9
2013,38,Manitoba,54,0,11,0,0,19,6
2013,39,Manitoba,60,0.1,12,0,0,19,13
2013,40,Manitoba,50,0.03,11,0,0,19,8
2013,41,Manitoba,52,0,10,0,1,19,4
2012,17,Manitoba,46,0.01,12,0,0,0,0
2012,18,Manitoba,51,0.05,11,0,0,0,0
2012,19,Manitoba,56,0.06,13,0,0,0,5
2012,20,Manitoba,58,0.16,12,0,0,0,6
2012,21,Manitoba,53,0.02,11,0,0,0,5
2012,22,Manitoba,53,0.13,9,0,0,0,5
2012,23,Manitoba,67,0.08,8,0,0,0,8
2012,24,Manitoba,62,0.17,11,0,0,0,10
2012,25,Manitoba,60,0.04,8,0,0,0,11
2012,26,Manitoba,68,0,10,0,0,0,11
2012,27,Manitoba,73,0.03,7,0,0,0,15
2012,28,Manitoba,73,0,7,0,0,0,17
2012,29,Manitoba,69,0.05,8,1,0,2,21
2012,30,Manitoba,71,0,8,1,0,20,36
2012,31,Manitoba,71,0.2,9,4,0,48,100
2012,32,Manitoba,67,0,9,7,0,62,47
2012,33,Manitoba,62,0.04,8,7,0,98,31
2012,34,Manitoba,69,0.01,7,6,0,108,84
2012,35,Manitoba,70,0.01,11,7,0,111,75
2012,36,Manitoba,63,0.01,11,1,0,116,22
2012,37,Manitoba,59,0.01,11,3,0,116,23
2012,38,Manitoba,47,0.01,12,2,0,116,13
2012,39,Manitoba,50,0,8,0,0,116,5
2012,40,Manitoba,46,0.02,15,0,0,116,7
2012,41,Manitoba,37,0.02,10,0,0,116,5
2015,17,Quebec,53,0,8,0,0,0,8
2015,18,Quebec,65,0.06,8,0,0,0,8
2015,19,Quebec,58,0.09,10,0,0,0,8
2015,20,Quebec,59,0.05,11,0,0,0,8
2015,21,Quebec,69,0.11,11,0,0,0,8
2015,22,Quebec,56,0.07,9,0,0,0,8
2015,23,Quebec,65,0.16,9,0,0,0,8
2015,24,Quebec,64,0.16,7,0,0,0,16
2015,25,Quebec,67,0.18,8,0,0,0,8
2015,26,Quebec,64,0.07,9,0,0,120,19
2015,27,Quebec,71,0.01,8,0,0,127,24
2015,28,Quebec,70,0.05,9,0,1,132,24
2015,29,Quebec,70,0.3,8,0,1,131,16
2015,30,Quebec,75,0.07,9,1,2,129,16
2015,31,Quebec,67,0.02,9,1,3,126,8
2015,32,Quebec,69,0.31,7,0,0,133,8
2015,33,Quebec,76,0.11,9,1,1,125,16
2015,34,Quebec,68,0.01,8,2,1,123,11
2015,35,Quebec,70,0,8,1,3,131,31
2015,36,Quebec,72,0.15,8,2,4,128,15
2015,37,Quebec,69,0.21,9,6,0,123,7
2015,38,Quebec,58,0,7,5,0,108,7
2015,39,Quebec,55,0.17,11,2,2,107,11
2015,40,Quebec,49,0.03,7,5,0,0,7
2015,41,Quebec,51,0.11,11,8,0,0,15
2014,17,Quebec,46,0.05,9,0,0,0,0
2014,18,Quebec,49,0.18,12,0,0,0,0
2014,19,Quebec,53,0.09,10,0,0,0,0
2014,20,Quebec,62,0.17,13,0,0,0,0
2014,21,Quebec,59,0.01,9,0,0,0,13
2014,22,Quebec,59,0.08,9,0,0,0,13
2014,23,Quebec,66,0.13,8,0,0,0,40
2014,24,Quebec,66,0.28,11,0,0,0,18
2014,25,Quebec,65,0.14,8,0,0,0,27
2014,26,Quebec,69,0.14,6,0,0,0,33
2014,27,Quebec,75,0.02,9,0,0,0,23
2014,28,Quebec,70,0.08,12,0,0,0,40
2014,29,Quebec,69,0.05,9,0,0,1,27
2014,30,Quebec,72,0.06,10,0,0,4,28
2014,31,Quebec,66,0.18,8,0,0,9,54
2014,32,Quebec,70,0.04,6,0,0,10,24
2014,33,Quebec,67,0.2,10,1,2,19,34
2014,34,Quebec,66,0,7,1,0,19,9
2014,35,Quebec,70,0,8,1,1,39,17
2014,36,Quebec,72,0.11,10,1,0,70,8
2014,37,Quebec,60,0.12,9,0,3,99,12
2014,38,Quebec,52,0.02,9,1,2,112,13
2014,39,Quebec,61,0.02,9,0,0,119,15
2014,40,Quebec,58,0.06,11,0,1,119,16
2014,41,Quebec,51,0.1,13,1,0,119,16
2013,17,Quebec,46,0.03,11,1,0,0,9
2013,18,Quebec,60,0.01,7,0,0,0,9
2013,19,Quebec,65,0.08,8,0,0,0,9
2013,20,Quebec,51,0.01,11,0,0,0,18
2013,21,Quebec,64,0.19,10,0,0,0,17
2013,22,Quebec,64,0.18,9,0,0,0,9
2013,23,Quebec,59,0.11,10,0,0,0,21
2013,24,Quebec,64,0.11,9,0,0,0,18
2013,25,Quebec,62,0.09,8,0,0,0,9
2013,26,Quebec,69,0.14,9,0,0,0,37
2013,27,Quebec,72,0.02,9,0,0,0,9
2013,28,Quebec,73,0.06,8,0,0,0,45
2013,29,Quebec,79,0.28,9,0,0,2,49
2013,30,Quebec,66,0.06,7,0,0,3,73
2013,31,Quebec,70,0.12,9,1,3,5,40
2013,32,Quebec,68,0.04,9,3,2,11,74
2013,33,Quebec,66,0.08,9,8,4,23,56
2013,34,Quebec,69,0.02,10,3,5,36,64
2013,35,Quebec,70,0.06,7,4,9,36,29
2013,36,Quebec,63,0.06,10,2,6,40,32
2013,37,Quebec,62,0.18,8,3,4,47,20
2013,38,Quebec,58,0.12,9,1,2,59,8
2013,39,Quebec,54,0.03,6,1,0,60,16
2013,40,Quebec,61,0,6,1,0,60,24
2013,41,Quebec,55,0.11,10,0,0,60,20
2012,17,Quebec,40,0.17,13,0,0,0,0
2012,18,Quebec,50,0.03,7,0,0,0,10
2012,19,Quebec,55,0.07,8,0,0,0,10
2012,20,Quebec,61,0.02,7,0,0,0,10
2012,21,Quebec,69,0.1,7,0,0,0,11
2012,22,Quebec,62,0.16,8,0,0,0,10
2012,23,Quebec,61,0.02,8,0,0,0,10
2012,24,Quebec,68,0.08,7,0,0,0,11
2012,25,Quebec,76,0.01,9,0,0,0,11
2012,26,Quebec,69,0.13,9,0,0,0,26
2012,27,Quebec,73,0.12,6,0,0,0,40
2012,28,Quebec,72,0,8,0,2,0,24
2012,29,Quebec,71,0.21,6,1,0,0,11
2012,30,Quebec,71,0.1,7,1,0,0,11
2012,31,Quebec,76,0.01,7,0,1,5,78
2012,32,Quebec,72,0.17,10,2,5,8,31
2012,33,Quebec,70,0.02,7,6,2,19,94
2012,34,Quebec,70,0,6,10,5,19,100
2012,35,Quebec,71,0.01,11,9,8,19,76
2012,36,Quebec,71,0.11,6,14,1,19,70
2012,37,Quebec,63,0.07,8,23,6,19,43
2012,38,Quebec,58,0.12,10,16,0,19,34
2012,39,Quebec,54,0.01,9,27,0,19,38
2012,40,Quebec,57,0.16,8,11,0,19,14
2012,41,Quebec,45,0.06,10,8,0,19,19
2015,17,Ontario,53,0,9,0,0,0,2
2015,18,Ontario,61,0.04,5,0,0,0,2
2015,19,Ontario,58,0.07,7,0,0,0,4
2015,20,Ontario,58,0,8,0,0,0,5
2015,21,Ontario,70,0.11,8,0,0,0,8
2015,22,Ontario,57,0.14,7,0,0,180,8
2015,23,Ontario,65,0.18,6,0,0,356,5
2015,24,Ontario,65,0.08,5,0,1,852,5
2015,25,Ontario,67,0.33,7,0,0,886,13
2015,26,Ontario,63,0.02,7,0,0,954,15
2015,27,Ontario,68,0.04,5,0,0,1152,13
2015,28,Ontario,67,0.03,6,1,0,1216,21
2015,29,Ontario,72,0.01,7,1,4,1219,16
2015,30,Ontario,76,0.03,6,1,1,1222,22
2015,31,Ontario,68,0.06,6,0,8,1176,24
2015,32,Ontario,69,0.21,6,0,0,1168,15
2015,33,Ontario,73,0.09,5,1,0,1168,24
2015,34,Ontario,64,0.01,5,5,1,987,12
2015,35,Ontario,75,0,5,2,1,881,18
2015,36,Ontario,70,0.11,5,5,0,802,9
2015,37,Ontario,65,0.07,6,1,2,712,6
2015,38,Ontario,60,0,5,5,4,526,4
2015,39,Ontario,55,0.04,9,2,2,396,6
2015,40,Ontario,53,0.14,6,3,0,65,5
2015,41,Ontario,52,0.04,8,3,4,0,2
2014,17,Ontario,46,0.05,8,0,0,0,3
2014,18,Ontario,47,0.14,9,0,0,0,2
2014,19,Ontario,53,0,9,0,0,0,2
2014,20,Ontario,56,0.13,6,0,0,0,3
2014,21,Ontario,57,0.09,5,0,0,0,4
2014,22,Ontario,65,0.02,6,0,0,0,7
2014,23,Ontario,63,0.04,6,0,0,0,10
2014,24,Ontario,65,0.19,6,0,0,0,16
2014,25,Ontario,66,0.16,5,0,0,0,13
2014,26,Ontario,69,0.06,4,0,0,0,7
2014,27,Ontario,72,0.09,7,0,0,0,20
2014,28,Ontario,68,0.12,6,0,0,0,17
2014,29,Ontario,66,0.21,5,1,0,0,13
2014,30,Ontario,68,0.03,5,0,0,2,14
2014,31,Ontario,67,0.35,5,0,0,5,35
2014,32,Ontario,68,0.21,4,0,0,9,22
2014,33,Ontario,65,0.12,7,2,0,11,30
2014,34,Ontario,67,0.02,4,0,2,13,11
2014,35,Ontario,67,0,6,2,3,30,18
2014,36,Ontario,71,0.39,5,5,0,43,13
2014,37,Ontario,60,0.15,6,1,0,52,10
2014,38,Ontario,53,0.02,4,0,1,56,7
2014,39,Ontario,60,0.08,4,0,0,56,3
2014,40,Ontario,61,0.06,4,0,0,56,6
2014,41,Ontario,50,0.06,6,0,0,56,4
2013,17,Ontario,43,0.05,6,0,0,0,2
2013,18,Ontario,57,0.05,6,0,0,0,3
2013,19,Ontario,59,0.04,5,0,0,0,4
2013,20,Ontario,51,0.02,8,0,0,0,3
2013,21,Ontario,60,0.17,8,0,0,0,7
2013,22,Ontario,64,0.16,6,1,0,0,9
2013,23,Ontario,58,0.05,7,1,0,0,9
2013,24,Ontario,64,0.29,6,0,0,0,12
2013,25,Ontario,64,0.11,5,0,0,0,12
2013,26,Ontario,73,0.06,4,0,1,2,12
2013,27,Ontario,71,0.05,5,1,0,2,20
2013,28,Ontario,72,0.13,6,2,0,4,15
2013,29,Ontario,80,0.05,5,1,2,12,20
2013,30,Ontario,65,0.12,6,5,0,22,56
2013,31,Ontario,66,0.26,5,4,8,41,43
2013,32,Ontario,67,0.04,6,5,6,65,32
2013,33,Ontario,63,0,5,5,2,89,24
2013,34,Ontario,70,0,5,2,0,131,30
2013,35,Ontario,72,0.2,3,2,8,155,22
2013,36,Ontario,63,0.12,6,7,2,179,12
2013,37,Ontario,64,0.04,6,3,2,190,15
2013,38,Ontario,57,0.17,4,5,2,194,9
2013,39,Ontario,55,0,4,0,1,196,5
2013,40,Ontario,61,0.04,4,5,0,198,9
2013,41,Ontario,56,0.04,4,1,0,198,4
2012,17,Ontario,40,0.06,11,0,0,0,4
2012,18,Ontario,50,0.12,6,0,0,0,3
2012,19,Ontario,56,0.07,6,0,0,0,3
2012,20,Ontario,58,0.02,4,0,0,0,3
2012,21,Ontario,69,0.01,6,0,0,0,5
2012,22,Ontario,64,0.09,8,0,0,0,3
2012,23,Ontario,63,0.03,6,1,0,0,6
2012,24,Ontario,67,0.08,6,0,0,0,4
2012,25,Ontario,76,0.17,6,0,0,2,7
2012,26,Ontario,70,0.04,7,0,0,6,10
2012,27,Ontario,75,0.04,5,3,1,10,39
2012,28,Ontario,73,0.02,5,5,3,19,24
2012,29,Ontario,75,0.06,6,9,1,30,19
2012,30,Ontario,72,0.38,6,14,2,89,17
2012,31,Ontario,73,0.16,4,23,1,162,77
2012,32,Ontario,70,0.14,6,44,1,249,46
2012,33,Ontario,68,0.05,4,44,8,312,64
2012,34,Ontario,67,0,4,38,4,375,83
2012,35,Ontario,70,0.15,6,26,0,409,100
2012,36,Ontario,69,0.56,4,25,0,434,79
2012,37,Ontario,61,0.03,5,17,2,454,37
2012,38,Ontario,57,0.16,5,3,4,462,23
2012,39,Ontario,53,0,6,2,6,462,24
2012,40,Ontario,57,0.03,5,3,0,464,18
2012,41,Ontario,42,0.04,5,1,0,464,10
2015,17,Saskatchewan,50,0,10,0,0,0,6
2015,18,Saskatchewan,46,0,11,0,0,0,12
2015,19,Saskatchewan,46,0,9,0,0,0,6
2015,20,Saskatchewan,53,0,8,0,0,0,6
2015,21,Saskatchewan,56,0,8,0,0,2,9
2015,22,Saskatchewan,60,0,10,0,0,0,9
2015,23,Saskatchewan,64,0,10,0,0,3,9
2015,24,Saskatchewan,57,0,8,0,0,3,12
2015,25,Saskatchewan,65,0,7,0,0,10,31
2015,26,Saskatchewan,70,0,6,0,0,13,15
2015,27,Saskatchewan,66,0,9,0,0,16,13
2015,28,Saskatchewan,67,0,8,0,0,40,15
2015,29,Saskatchewan,68,0,10,0,0,47,16
2015,30,Saskatchewan,63,0.02,9,0,0,69,43
2015,31,Saskatchewan,63,0,8,0,0,67,16
2015,32,Saskatchewan,70,0,8,0,0,80,28
2015,33,Saskatchewan,58,0,8,0,0,94,38
2015,34,Saskatchewan,62,0,8,0,0,42,21
2015,35,Saskatchewan,61,0,10,0,1,41,14
2015,36,Saskatchewan,53,0,8,0,0,0,9
2015,37,Saskatchewan,52,0,8,0,0,0,5
2015,38,Saskatchewan,54,0,10,0,0,0,5
2015,39,Saskatchewan,48,0,8,0,0,0,5
2015,40,Saskatchewan,48,0,9,0,0,0,8
2015,41,Saskatchewan,44,0,11,0,0,0,5
2014,17,Saskatchewan,40,0,12,0,0,0,6
2014,18,Saskatchewan,41,0,10,0,0,0,6
2014,19,Saskatchewan,41,0,9,0,0,0,6
2014,20,Saskatchewan,45,0,7,0,0,0,6
2014,21,Saskatchewan,59,0,10,0,0,0,13
2014,22,Saskatchewan,57,0,11,0,0,0,20
2014,23,Saskatchewan,55,0,8,0,0,0,17
2014,24,Saskatchewan,53,0,10,0,0,0,13
2014,25,Saskatchewan,57,0,10,0,0,0,7
2014,26,Saskatchewan,63,0,8,0,0,0,21
2014,27,Saskatchewan,66,0,11,0,0,0,26
2014,28,Saskatchewan,65,0,10,0,0,0,69
2014,29,Saskatchewan,64,0,9,0,0,0,65
2014,30,Saskatchewan,63,0,9,0,0,1,60
2014,31,Saskatchewan,67,0,6,0,0,1,36
2014,32,Saskatchewan,69,0,6,0,2,2,47
2014,33,Saskatchewan,67,0,7,0,0,9,67
2014,34,Saskatchewan,64,0,8,0,0,19,45
2014,35,Saskatchewan,58,0,9,0,0,20,34
2014,36,Saskatchewan,56,0,8,0,0,20,13
2014,37,Saskatchewan,46,0,9,0,0,20,19
2014,38,Saskatchewan,55,0,8,0,0,20,6
2014,39,Saskatchewan,61,0,9,0,0,20,16
2014,40,Saskatchewan,44,0,12,0,0,20,12
2014,41,Saskatchewan,45,0,9,0,0,20,6
2013,17,Saskatchewan,34,0,10,0,0,0,10
2013,18,Saskatchewan,40,0,12,0,0,0,14
2013,19,Saskatchewan,50,0,12,0,0,0,14
2013,20,Saskatchewan,59,0,9,0,0,0,7
2013,21,Saskatchewan,57,0,13,0,0,0,7
2013,22,Saskatchewan,60,0,9,0,0,0,14
2013,23,Saskatchewan,57,0,9,0,0,0,21
2013,24,Saskatchewan,57,0,10,0,0,0,20
2013,25,Saskatchewan,61,0,10,0,0,0,14
2013,26,Saskatchewan,64,0,7,0,0,0,41
2013,27,Saskatchewan,69,0,7,0,0,0,61
2013,28,Saskatchewan,65,0,8,0,0,1,65
2013,29,Saskatchewan,62,0,9,0,3,1,81
2013,30,Saskatchewan,60,0,9,0,1,3,75
2013,31,Saskatchewan,59,0,8,0,2,3,33
2013,32,Saskatchewan,60,0,6,0,1,18,44
2013,33,Saskatchewan,69,0,8,0,0,29,75
2013,34,Saskatchewan,66,0,8,1,1,29,60
2013,35,Saskatchewan,69,0,8,3,0,36,24
2013,36,Saskatchewan,67,0,7,1,0,40,21
2013,37,Saskatchewan,62,0,9,0,0,40,26
2013,38,Saskatchewan,57,0,10,1,2,40,32
2013,39,Saskatchewan,51,0,9,0,1,40,13
2013,40,Saskatchewan,45,0,11,0,0,40,29
2013,41,Saskatchewan,46,0,10,0,0,40,10
2012,17,Saskatchewan,44,0,13,0,0,0,24
2012,18,Saskatchewan,46,0,12,0,0,0,16
2012,19,Saskatchewan,51,0,13,0,0,0,16
2012,20,Saskatchewan,54,0,12,0,0,0,9
2012,21,Saskatchewan,48,0,11,0,0,0,17
2012,22,Saskatchewan,53,0,9,0,0,0,16
2012,23,Saskatchewan,61,0,13,0,0,0,8
2012,24,Saskatchewan,56,0,11,0,0,0,16
2012,25,Saskatchewan,58,0,7,0,0,0,25
2012,26,Saskatchewan,64,0,12,0,0,0,22
2012,27,Saskatchewan,65,0,9,0,0,0,23
2012,28,Saskatchewan,71,0,7,0,1,0,67
2012,29,Saskatchewan,67,0,10,0,0,0,34
2012,30,Saskatchewan,67,0,8,0,0,0,28
2012,31,Saskatchewan,64,0,8,0,0,0,59
2012,32,Saskatchewan,68,0,8,0,0,3,58
2012,33,Saskatchewan,59,0,8,2,0,4,34
2012,34,Saskatchewan,65,0,9,1,0,6,100
2012,35,Saskatchewan,64,0,9,0,0,6,49
2012,36,Saskatchewan,55,0,11,3,0,6,41
2012,37,Saskatchewan,58,0,13,0,0,6,16
2012,38,Saskatchewan,50,0,8,3,0,6,19
2012,39,Saskatchewan,55,0,6,0,0,6,15
2012,40,Saskatchewan,42,0,10,0,0,6,11
2012,41,Saskatchewan,36,0,8,0,0,6,7
First I produced this plot
But I did that in the most brute force way imaginable
#split out each year
cases2015 <- subset(mosquitoes, mosquitoes$Years==2015)
cases2014 <- subset(mosquitoes, mosquitoes$Years==2014)
cases2013 <- subset(mosquitoes, mosquitoes$Years==2013)
cases2012 <- subset(mosquitoes, mosquitoes$Years==2012)
#get the sums by week
aggregate2015 <- aggregate(cases2015$Number.of.cases, by=list(Weeks=cases2015$Weeks), FUN=sum)
aggregate2014 <- aggregate(cases2014$Number.of.cases, by=list(Weeks=cases2014$Weeks), FUN=sum)
aggregate2013 <- aggregate(cases2013$Number.of.cases, by=list(Weeks=cases2013$Weeks), FUN=sum)
aggregate2012 <- aggregate(cases2012$Number.of.cases, by=list(Weeks=cases2012$Weeks), FUN=sum)
#put the sums back together into a dataframe
aggregateSums <- aggregate2012
aggregateSums <- cbind(aggregateSums, aggregate2013[,2])
aggregateSums <- cbind(aggregateSums, aggregate2014[,2])
aggregateSums <- cbind(aggregateSums, aggregate2015[,2])
#give the columns useful names
colnames(aggregateSums) <- c("Weeks","Cases.2012","Cases.2013","Cases.2014","Cases.2015")
#base R plot
#plot the first set of points
plot(x=aggregateSums$Weeks,y=aggregateSums$Cases.2012,pch=16,col="Red",main="West Nile Cases",xlab="Week",ylab="Number of Cases")
#add additional years
points(x=aggregateSums$Weeks,y=aggregateSums$Cases.2013,pch=15,col="Blue")
points(x=aggregateSums$Weeks,y=aggregateSums$Cases.2014,pch=14,col="Orange")
points(x=aggregateSums$Weeks,y=aggregateSums$Cases.2015,pch=13,col="Brown")
#add the connecting lines
lines(x=aggregateSums$Weeks,y=aggregateSums$Cases.2012,col="Red")
lines(x=aggregateSums$Weeks,y=aggregateSums$Cases.2013,col="Blue")
lines(x=aggregateSums$Weeks,y=aggregateSums$Cases.2014,col="Orange")
lines(x=aggregateSums$Weeks,y=aggregateSums$Cases.2015,col="Brown")
#click to place legend
legend(locator(1),c("2012","2013","2014","2015"),pch=c(16,15,14,13), col=c("Red","Blue","Orange","Brown"))
So surely there has to be a more efficient way to get there.
My next step is to produce the same plot but for just one province at a time. I don't want to have to go through the above 6 times...
I'm opening to accomplishing this via ggplot. If possible, I'd like to do it without resorting to additional packages (like plyr) as I'm trying to learn the base functionality for manipulating data.
Just to close the loop after Biranjan's answer...
mosq2 <- mosquitoes %>%
select(Years,Weeks,Province,Number.of.cases) %>%
group_by(Years,Weeks,Province) %>%
summarise(sum_case=sum(Number.of.cases))
ggplot(data=mosq2, aes(x=as.factor(Weeks),y=sum_case,color=as.factor(Years))) +
geom_point(aes(shape=as.factor(Years))) +
geom_line(aes(group=as.factor(Years))) +
labs(title="West Nile Cases", x="weeks", y="Number of cases") +
theme(legend.title=element_blank()) +
facet_wrap(~Province,ncol=3) +
scale_x_discrete(breaks=c(17,30,41))
Turned out quite nicely
ggplot(data=data1, aes(x=as.factor(Weeks),y=sum_case,color=as.factor(Years)))+
geom_point(aes(shape=as.factor(Years)))+
geom_line(aes(group=as.factor(Years)))+
labs(title="West Nile cases",x="weeks",y="Number of cases")+
theme(legend.title=element_blank())
Update:
I had too few points in my simulation so it rendered fine so that was the problem. I could't find a way to plot just using ggplot. The same code works if "dplyr" is used first and variable name edited accordingly. I know it is not what you are looking for, sorry to disappoint you.
library(dplyr)
data1 <- data %>%
select(Years,Weeks,Number.of.cases) %>%
group_by(Years,Weeks) %>%
summarise(sum_case=sum(Number.of.cases))

questionnaires filled on the same day

I am working with a data set with multiple questionnaires which were supposed to be filled in on different timepoints i.e.
173 9/13/2013 10/29/2013 9/26/2014
174 10/21/2013 11/25/2013 11/3/2014
175 7/1/2014 7/3/2015 4/27/2016
176 1/15/2014 2/24/2014 6/10/2015
177 3/15/2014 4/1/2015
178 7/18/2014 9/18/2014 8/17/2015
179 6/30/2013 8/15/2013 7/15/2014
180 4/22/2013 6/24/2013 5/11/2014
181 12/7/2014 12/26/2015
182 4/2/2015 5/17/2015 4/20/2016
183 1/12/2015 2/26/2015 1/28/2016
184 7/18/2014 8/26/2014 8/14/2015
185 8/27/2013 10/19/2013 9/21/2014
186 10/29/2013 11/30/2013 11/6/2014
187 9/17/2014 11/18/2014 10/20/2015
188 5/10/2014 6/27/2014 6/1/2015
189 10/4/2013 10/5/2014
190 1/22/2013 4/11/2013
191 10/21/2014 10/21/2014
I would like to know how to see how many participants filled in all questionnaires on the same day, how many participants filled in at least 2 questionnaires on the same day. how many at least 3 on the same day etc.
Any help would be highly appreciated.
Reproducible data:
Label = c(
"1/25/2015", "1/25/2016", "1/26/2014", "1/26/2015", "1/27/2014",
"1/27/2015", "1/28/2014", "1/28/2015", "1/29/2015", "1/3/2014",
"1/3/2015", "1/3/2016", "1/30/2015", "1/31/2014", "1/4/2014",
"1/4/2015", "1/4/2016", "1/5/2014", "1/5/2015", "1/6/2014",
"1/6/2015", "1/7/2014", "1/7/2015", "1/8/2014", "1/8/2015",
"1/9/2014", "1/9/2015", "1/9/2016", "10/1/2012", "10/1/2013",
"10/1/2014", "10/1/2015", "10/10/2013", "10/10/2014", "10/11/2013",
"10/11/2014", "10/11/2015", "10/12/2013", "10/12/2014", "10/12/2015",
"10/13/2013", "10/13/2014", "10/13/2015", "10/14/2013", "10/14/2014",
"10/14/2015", "10/15/2014", "10/15/2015", "10/16/2013", "10/16/2014",
"10/16/2015", "10/17/2013", "10/17/2014", "10/17/2015", "10/18/2013",
"10/18/2014", "10/18/2015", "10/19/2013", "10/19/2014", "10/19/2015",
"10/2/2013", "10/2/2014", "10/20/2013", "10/20/2014", "10/20/2015",
"10/21/2013", "10/21/2014", "10/22/2013", "10/22/2014", "10/22/2015",
"10/23/2012", "10/23/2013", "10/23/2014", "10/23/2015", "10/24/2013",
"10/24/2014", "10/24/2015", "10/25/2013", "10/25/2014", "10/26/2013",
"10/26/2014", "10/26/2015", "10/27/2013", "10/27/2014", "10/27/2015",
"10/28/2013", "10/28/2014", "10/29/2013", "10/29/2014", "10/3/2014",
"10/3/2015", "10/30/2014", "10/31/2012", "10/31/2013", "10/31/2014",
"10/31/2015", "10/4/2013", "10/4/2014", "10/4/2015", "10/5/2014",
"10/5/2015", "10/6/2013", "10/6/2014", "10/6/2015", "10/7/2013",
"10/7/2014", "10/8/2012", "10/8/2014", "10/8/2015", "10/9/2013",
"10/9/2014", "10/9/2015", "11/1/2013", "11/1/2014", "11/1/2015",
class = "factor")
Label = c(
"4/6/2015", "4/7/2015", "4/9/2012", "5/12/2015", "5/13/2014",
"5/14/2015", "5/15/2014", "5/15/2015", "5/17/2014", "5/19/2014",
"5/20/2014", "5/25/2014", "5/27/2014", "5/29/2014", "5/30/2014",
"5/30/2015", "5/31/2015", "5/4/2014", "5/9/2015", "6/1/2015",
"6/10/2014", "6/11/2014", "6/11/2015", "6/12/2015", "6/16/2014",
"6/16/2015", "6/18/2014", "6/21/2014", "6/24/2015", "6/25/2014",
"6/25/2015", "6/26/2015", "6/27/2015", "6/29/2015", "6/5/2014",
"6/6/2015", "6/8/2014", "7/1/2014", "7/13/2014", "7/14/2015",
"7/16/2014", "7/2/2014", "7/21/2014", "7/25/2014", "7/27/2014",
"7/27/2015", "7/28/2014", "7/29/2014", "7/30/2014", "7/31/2014",
"7/31/2015", "7/4/2014", "7/4/2015", "8/1/2014", "8/11/2014",
"8/11/2015", "8/25/2014", "8/27/2015", "8/5/2014", "8/8/2014",
"8/9/2015", "9/1/2014", "9/10/2015", "9/15/2015", "9/22/2013",
"9/3/2012", "9/30/2014", "9/8/2014", "9/8/2015"), class = "factor")
Label = c(" ",
"1/16/2016", "1/26/2015", "10/11/2015", "10/14/2015", "10/16/2015",
"10/6/2014", "10/7/2013", "11/11/2015", "11/15/2015", "11/17/2013",
"11/18/2013", "11/2/2015", "11/20/2013", "11/29/2013", "2/17/2014",
"2/17/2015", "2/21/2015", "2/23/2014", "2/25/2014", "2/25/2015",
"3/11/2016", "3/2/2014", "3/22/2015", "3/4/2014", "3/4/2016",
"4/11/2014", "4/12/2013", "4/18/2016", "4/21/2015", "4/23/2015",
"4/29/2015", "4/3/2015", "4/5/2016", "5/23/2015", "5/26/2015",
"5/27/2015", "5/28/2015", "5/29/2014", "5/29/2015", "5/8/2015",
"6/16/2015", "6/22/2015", "6/28/2015", "7/24/2015", "7/27/2015",
"7/4/2014", "7/8/2015", "9/14/2015", "9/15/2015", "9/16/2014",
"9/17/2014", "9/22/2014", "9/23/2014", "9/24/2014", "9/24/2015",
"9/26/2014", "9/28/2015", "9/30/2015", "9/9/2015"), class = "factor")), .Names = c("1A_RespDate",
"1B_RespDate", "1C_1_RespDate", "1C_2_RespDate",
"1C_RespDate", "2A_1_RespDate", "2A_RespDate", "2B_RespDate",
"2C_RespDate"), row.names = c(NA, -4831L), class = "data.frame")
I'll call you dataframe df:
sapply(apply(df,1,unique),length)
will give you the number of unique dates for each individual as a vector. The highest value is 7 and the min 1 (all questionnaires answered on the same day).
which(sapply(apply(df,1,unique),length)<7)
Will give you the index of the individuals who filled at least 2 questionnaires on the same day.
length(which(sapply(apply(df,1,unique),length)<7))
Will tell you how many individuals filled at least 2 questionnaires on the same day.
Edit:
This is inelegant (there must be a cleaner way) but it seems to work
which(sapply(sapply(sapply(apply(df,1,table),function(x) x==Z),which),function(x) any(x>0)))
Z is to be set to the number of questionnaires filled on the same day.
Explaination:
apply(df,1,table)
gives a list with for each individual the unique dates and how many times they appear.
sapply(apply(df,1,table),function(x) x==Z)
will give you the same list with True/False on whether a date appears exactly Z times.
sapply(sapply(apply(df,1,table),function(x) x==Z),which)
will give either "interger(0)" or a positive integer which is the index of the date for the individual (it's not something we are interested in).
sapply(sapply(sapply(apply(df,1,table),function(x) x==Z),which),function(x) any(x>0))
will give a vector of True/False corresponding to the index of the individual
then next step with "which" is to get the index for the True.
We therefore get the individuals for which a date appears exactly Z times.

How do I convert milliseconds to dd:hh:mm using moment.js?

I'm trying to get the days, hours and minutes of 9000000 milliseconds, but moment.js is returning 0 days. I'm using Format plugin for the Moment Duration object. https://github.com/jsmreese/moment-duration-format
moment.duration(9000000, "milliseconds").format("dd:hh:mm");
returns "02:30"
How did I get 9000000?
var ms = moment.duration({
days: 1,
hours: 2,
minutes: 30,
})
console.log(ms._milliseconds);
// 9000000
Sounds like humanizeduration is what you are looking for:
humanizeDuration(97320000) // '1 day, 3 hours, 2 minutes'
Here is the github link:
https://github.com/EvanHahn/HumanizeDuration.js
1000 x 60 x 60 x 24 = 86'400'000 milliseconds.
Of course 9 mil is 0 days.
9'000'000 / (1000 x 60 x 60) = 2.5h = 2 hours 30 min
I hope I know how to use calculator
Check this place

Divide data frame in three subframes based on values

I have a data frame like below. I would like to divide this into three submatrices bases on the average row.values. Basically I need to first calculate the average row.value and then divide the data frame based on the ID and the average row.value. Any ways to do this directly in one command?
dput(head(matrix_cpm_spike_norm_mature,6))
structure(c(3.60338983255681, 2.60614455986074, 7.38026015357423,
0.485930325361538, 10.0728954273074, 11.2022336797709, 2.3081852592818,
1.92925594571705, 4.42105830784866, 1.97655358070322, 11.4414272084792,
10.5245987429194, 3.79928941804137, 1.43988964742944, 2.43182718729225,
1.85054706660746, 10.6157635199152, 9.93246596282924, 1.76499400158053,
0.867000595584972, 4.17937486393757, 1.62313934970233, 10.9760540633484,
11.4384402909535, 3.12565733850373, 1.02306009758869, 6.15822532096255,
1.45585403170949, 11.1021517437179, 10.9107307780349, 1.84965418527454,
1.57953239648466, 3.92960542264649, 0.968010692772174, 11.0236055961936,
10.5913003928287, 4.16812971357324, 2.58284099400479, 5.77874217548982,
2.71612416959378, 11.6945755132658, 12.2125038706251, 2.28044423317132,
2.28044423317132, 4.20181017967427, 1.93489752637374, 12.0201280590592,
11.4757936894966, 3.29573463740003, 3.1840621322311, 5.6701373082146,
1.96630490538038, 10.7374381660113, 13.2995125381539, 2.91175170429137,
2.1569190504634, 5.20328658455966, 2.1569190504634, 10.2217040585341,
9.83559070490264, 3.93309437957696, 2.51818736936141, 7.40221631369804,
0.335828139281744, 10.56613413102, 10.6619866025163, 2.27796568023663,
1.66056419670379, 5.29496497910422, 0.699488415484219, 10.310084339381,
9.75424847574386, 3.64268206121968, 2.37908914926602, 8.24035280411754,
1.86474527822428, 8.96352397286089, 10.2227828989983, 2.33835251965787,
2.06871044842623, 4.5954355631791, 1.01279126170234, 12.3601973455172,
11.4656151558222, 2.44441563566059, 2.40421119777587, 6.02973594460351,
1.52508878726709, 10.4309455175175, 9.04837239279591, 2.19268157249804,
1.24045869709133, 3.09706859211401, 1.13115919913948, 10.7933496311904,
9.9695518430138, 2.92171136565383, 0.408245399247249, 2.04506253637125,
1.21781706285458, 11.799265629373, 10.0589147547578, 2.01873582708745,
1.46069392909814, 3.53893363843314, 1.196243674246, 11.337876323565,
11.2124904638672, 2.52480547770426, 1.46864391283124, 4.93028527141748,
0.732496016009805, 10.1639221283662, 9.76884595465318, 1.44174154410475,
1.64024858387856, 5.63721432238654, 1.72991175679785, 11.1211504054224,
10.6024617121637, 3.22964454373919, 1.02206068958127, 6.97852650980799,
1.30645573502801, 12.8991256257565, 13.0000814142899, 2.826661275181,
1.2548377150201, 4.36474368941959, 2.12408033247534, 12.4705231651432,
11.9618071621679, 2.69847153335308, 2.27631143253466, 4.20951126623905,
-0.0664422382974684, 8.98849795367716, 9.41468863016541, 2.43225940352207,
2.09595624532069, 4.63715429950104, 1.25491429244567, 10.9872971587847,
10.4925408800726, 3.45374293794281, 3.13429367766325, 7.39158804595222,
1.54024232057877, 10.8542522087055, 10.6155963202093, 2.69223465587794,
0.809912072570472, 4.64506635251942, 1.6137442296552, 11.418571423468,
12.2726701702859, 2.77267844825925, 1.78512859921307, 9.14121538410264,
-4.43688074939158, 9.73718972990295, 10.2957742347039, 1.49118640848005,
1.5813150312641, 4.0667005226673, 0.59204571815906, 10.0375004920899,
9.54533950930523, 2.16345862609174, 3.05436004610877, 8.07790690254631,
1.79368307261622, 12.3560914214054, 13.0051227140041, 2.58124788178095,
1.88617681546547, 2.73360657353678, 1.77592657048602, 10.9528780864007,
10.4348473225329, 3.00497812997592, 1.7631510316129, 5.90700548290542,
0.877605937609275, 9.87041660889873, 9.7614993662457, 2.05885671172016,
1.76410360006183, 6.3570619407495, 1.76410360006183, 11.685705297838,
11.3602560106241, 2.82800833596723, 2.03247959283134, 6.95870308054991,
1.27136597104121, 10.8372565515699, 9.81173432822107, 2.98238612294936,
1.83072752194121, 5.68689117080157, 1.16721273421709, 12.1340028664354,
11.3680995516162, 3.39653560487816, 2.69687166650826, 6.48412064518055,
1.29454179328719, 10.0028066556529, 10.512440462176, 1.4596941835747,
0.727974609981551, 3.88239656448561, 0.800353161112983, 10.6363990565411,
10.1243371999547, 3.3438733844774, 0.697114163823588, 8.47892011973568,
0.697114163823588, 10.6855866660417, 10.6118837404499, 2.13674190430653,
1.1527414454512, 4.18464270261989, 1.27853746250223, 10.7320904309318,
9.89189546554829, 3.823237482187, 0.874031743370891, 1.95508453030069,
1.87846554046761, 9.88713195004868, 9.86183499157279, 2.53517059781643,
0.728446508864785, 3.50839578521561, 1.9965292874828, 10.9487017925789,
10.9382935538901, 2.62649833974985, 1.92098108152647, 3.36108007152541,
0.488100205530239, 10.4894419819966, 10.2226664777547, 1.33967056255352,
0.0917001628908314, 3.81741552255672, 2.03856822618478, 12.2392733147047,
11.4938478602584, 3.25046552747648, 1.48275456354609, 4.81128734606174,
1.96154924623549, 12.1358994156854, 11.4729501435715, 2.22982247734829,
1.68173260626794, 5.2904025048379, 1.68173260626794, 11.5137150099059,
11.010850144881, 3.91979174751635, 2.17257846100131, 3.53963311590271,
1.6107186758943, 11.4951784786694, 10.1260992493434, 2.35254220581856,
1.26444532526964, 4.6646553023283, 1.17529994013973, 10.9857870198993,
10.5814974795421, 3.75054671795939, 3.03029499770489, 9.00153489042535,
3.27015511904915, 11.5616951026666, 10.6545660172833, 2.18325656314713,
1.50344897782345, 3.97329240591904, 1.50344897782345, 11.3467527232018,
10.707768513033, 2.7932432629389, 1.70473241476131, 5.05340701550577,
0.547092257741687, 11.7624630621943, 9.91053139355945, 2.80574376605516,
1.63874222935751, 3.92143952780207, 1.834040048536, 11.4458009229089,
10.9155922874574, 2.81245930319929, 1.47154823046698, 4.17913709197299,
0.836392585913434, 12.5796307717185, 11.6787505410903, 1.7257545501894,
0.949499417203444, 5.34316953950321, 1.63528985628147, 12.2668256759477,
11.696888238442, 2.43827755616949, 1.81088892324984, 5.13790770293989,
1.62918292819459, 10.2635519811317, 10.3028832599596, 2.07789375455372,
1.08266801489579, 3.36924723755809, 0.361290751749976, 9.978530962904,
9.62993954501451, 3.46479867592192, 2.12156177109216, 8.78601850725548,
1.28447344131806, 11.5362902737332, 11.569001443705, 1.88432186226604,
0.738852102905189, 3.93512805821853, 2.28505136817535, 12.2366325701317,
11.5419247012961, 2.72781971767877, 0.981047395388437, 2.42660975369917,
1.39151946304238, 9.47444971798845, 10.1389766220257, 1.9620421090479,
1.31100308652463, 3.9625997590393, 1.31100308652463, 11.0538785110822,
10.5756552873608, 2.24522309004463, 1.98668802513104, 4.91591774317019,
1.41382278476808, 10.9307830177181, 9.75061291019652, 2.89312477978526,
1.41138577836645, 5.3114727278592, 1.18511963455519, 12.8070122897496,
11.9900344090709, 3.27111851368705, 0.108008163568448, 3.27111851368705,
1.04663775582004, 10.5015959502624, 10.9439135408891, 1.96611160593682,
1.75218878170152, 1.96611160593682, -0.675585494894112, 11.1733364353258,
10.5537870233171, 2.76365773789404, 2.24622484994411, 4.82500775257594,
0.859189165848259, 11.3729184708685, 10.4792280890542, 2.2746810191026,
1.26862765095299, 3.1182053649793, 1.35619562745757, 11.541422194603,
11.0876007441211, 2.93955281780494, 2.41792430951958, 7.43488775671718,
1.59455665002896, 10.8522501620988, 10.7066940648954, 2.56175708272954,
1.63789352792862, 5.62616733093608, 1.77321879142546, 11.5341769241796,
11.3614738869516, 3.52512139810446, 2.04455677275519, 9.07892025450834,
1.80422229584867, 12.2658423589039, 12.7955854152128, 2.41662572851679,
0.739356302189606, 6.58079778253096, 2.2056509250739, 11.4453206203817,
11.2577755286648, 4.43488006430324, 2.47577649074622, 7.97026648668841,
2.47577649074622, 10.5347452823169, 10.9336199647057, 0.253851439446271,
1.33349021364545, 1.90668843852765, 1.38668924767966, 11.529988569981,
9.68152140791945, 2.69321031115829, 1.69780604669796, 9.9826122018356,
-0.0693106968443571, 11.2474341893491, 10.2586104182553, 2.30251678501683,
1.67135129150304, 5.1970813744178, 1.88064524158918, 11.4072019068968,
10.9762764532365, 2.73053313404777, 2.92486117981077, 9.751901699517,
2.31573050368619, 11.0745724079847, 11.5397308498818, 1.85114298699175,
1.74446232598571, 4.67405729933025, 1.36771765795199, 11.1449736215866,
10.6467965079355, 3.12408501589049, 2.00482310148083, 4.90897925267742,
0.753058050023766, 10.3589506698824, 10.3513173985724, 2.72147165969401,
1.46186080167698, 3.9354821726169, 1.23699756995357, 11.1881701541595,
10.931341142772, 2.99260441125988, 2.23826963603629, 3.87458928218141,
0.65507014009726, 9.74342426391136, 10.8918928191143, 2.78046014914365,
0.505490227190171, 4.43327034269568, 1.9329054288394, 11.6078716913683,
10.9011106771378, 2.92822672503857, 1.76286882344236, 6.82012516033837,
1.49833750295022, 9.57121023826284, 8.39324323219324, 2.81052780333747,
1.40588646539131, 3.18875457138522, 1.18481091851206, 11.462231229116,
10.5770583597432, 3.30911666206261, 1.33469191841737, 4.96029091396055,
2.17413398672633, 10.5583178628059, 10.0329059364534, 1.88186145438922,
0.989773711428552, 5.0108103947152, 0.769365496730378, 11.7867744116038,
11.2871021676365, 2.88127273360004, 1.87947399714945, 2.88127273360004,
1.92593791384916, 9.46125879965226, 9.29462968277333, 2.36391085778722,
1.74079268896779, 3.93604270788606, 1.34641095936402, 10.2068762533305,
10.0118036485061, 2.57361661875718, 2.03528106162882, 4.33420919282757,
1.1624557079541, 11.0754175076428, 11.0622083554009, 1.93155319127651,
1.52032894976282, 4.46324520367219, 1.52032894976282, 11.3476141375944,
10.4138620742203, 3.85081675794147, 2.74371623100224, 1.11770920823725,
1.98482146279348, 12.566434472332, 11.9424300470872, 2.53925962537711,
1.48382075377967, 4.87249055114745, 1.07284442662947, 11.7111768375923,
11.5795125663534, 2.15535566170556, 1.6234973786264, 7.72868868952004,
0.562556076894618, 9.85480133708155, 9.92934562704339, 2.54728578873058,
1.8265104903494, 3.76801234500258, 1.2821466213423, 11.2568527058396,
10.8343412926481, 3.05211979795212, 0.152264838074353, 2.54493306112919,
1.77792835724608, 11.5338955198532, 9.97573017264385, 2.42059823105591,
0.60936602148068, 3.12812928480832, 1.14355342605761, 11.5524961118039,
10.5285363661307), .Dim = c(6L, 92L), .Dimnames = list(c("hsa-miR-671-5p",
"hsa-miR-3909", "hsa-miR-1247-5p", "hsa-miR-628-3p", "hsa-miR-23b-3p",
"hsa-miR-127-3p"), c("100G", "100R", "106G", "106R", "122G",
"122R", "124G", "124R", "126G", "126R", "134G", "134R", "141G",
"141R", "167G", "167R", "185G", "185R", "192G", "192R", "235G",
"235R", "239G", "239R", "243G", "243R", "246G", "246R", "261G",
"261R", "267G", "267R", "26G", "26R", "270G", "270R", "279G",
"279R", "299G", "299R", "301G", "301R", "305G", "305R", "342G",
"342R", "350G", "350R", "356G", "356R", "35G", "35R", "361G",
"361R", "366G", "366R", "367G", "367R", "377G", "377R", "379G",
"379R", "388G", "388R", "400G", "400R", "402G", "402R", "46G",
"46R", "48G", "48R", "55G", "55R", "57G", "57R", "60G", "60R",
"68G", "68R", "70G", "70R", "73G", "73R", "77G", "77R", "82G",
"82R", "93G", "93R", "94G", "94R")))
I guess we need cut to split up the rows of the matrix after getting the rowMeans
v1 <- rowMeans(m1)
v2 <- cut(v1, breaks=3)
v2
#[1] (5.52,8.48] (11.4,14.4] (5.52,8.48] (11.4,14.4] (5.52,8.48] (5.52,8.48]
#Levels: (5.52,8.48] (8.48,11.4] (11.4,14.4]
We split the sequence of rows of the matrix ('m1') by 'v2', subset the rows of 'm1' based on the row index to create a list of 3 matrices.
lst <- lapply(split(1:nrow(m1), v2), function(i) m1[i,])
So here is a different approach to grouping by row-means. This uses kmeans clustering on the row-means to divide based on the closeness of the values. I think this is more in line with your description "low", "med", "high".
Your data set as provided is a matrix with 6 rows and 96 columns (the ID's are in the row.names.
df <- data.frame(ID=rownames(df),df)
library(data.table)
setDT(df)[, rm:=rowMeans(.SD), .SDcols=2:ncol(df), by=ID]
df[,grp:=kmeans(rm, centers=3)$cluster]
df[,list(ID,rm,grp)]
# ID rm grp
# 1: hsa-let-7a-3p 5.819544 1
# 2: hsa-let-7a-5p 14.396980 3
# 3: hsa-let-7b-3p 5.526127 1
# 4: hsa-let-7b-5p 11.548722 3
# 5: hsa-let-7c-5p 7.881395 2
# 6: hsa-let-7d-3p 6.368912 1
result <- split(df,df$grp) # split into 3 data.tables based on group
You can see that 5.5, 5.8, and 6.3 are in the first group, 7.9 is in the second group, and 11.5 and 14.4 are in the 3rd group.

Resources