-2

First off, here is the data set so you can play with it locally:

dput(test)

structure(list(Games = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
38L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 
33L, 34L, 35L, 36L, 37L, 38L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 
37L, 38L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 
28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
32L, 33L, 34L, 35L, 36L, 37L, 38L), League = c("BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", "BPL", 
"BPL", "BPL", "BPL", "BPL", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", "LGA", 
"LGA", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", 
"BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "BLI", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", "LG1", 
"LG1", "LG1", "LG1", "LG1"), line = c("Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", 
"Upper", "Upper", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Prediction", 
"Prediction", "Prediction", "Prediction", "Prediction", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower", "Lower", "Lower", "Lower", "Lower", "Lower", 
"Lower", "Lower"), value = c(1.29298996115076, 2.40499166735689, 
3.69798162850766, 4.78660126564843, 5.83266953230409, 7.25470630289318, 
8.49759553439007, 9.6095972405962, 10.902587201747, 12.2552936000241, 
13.3439132371649, 14.5109638123662, 15.8636702106434, 17.0651422119924, 
18.2666142133414, 19.4680862146904, 20.7610761758412, 22.0039654073381, 
23.246854638835, 24.3354742759757, 25.6881806742529, 27.110217444842, 
28.2222191510481, 29.5152091121989, 30.8679155104761, 32.1609054716268, 
33.4037947031237, 34.6052667044727, 35.8982566656235, 36.9443249322792, 
38.1457969336282, 39.3472689349772, 40.6402588961279, 41.7288785332687, 
42.9717677647656, 44.0837694709717, 45.3266587024686, 46.4937092776699, 
1.14244921317105, 2.07994244701033, 3.22239166018138, 4.12353606349401, 
4.95615615314181, 6.19262849787408, 7.2907822523304, 8.22827548616968, 
9.37072469934073, 10.5592560861148, 11.4604004894275, 12.4750490564289, 
13.6635804432029, 14.7191561786698, 15.7747319141366, 16.8303076496034, 
17.9727568627745, 19.0709106172308, 20.1690643716871, 21.0702087749998, 
22.2587401617738, 23.4952125065061, 24.4327057403454, 25.5751549535164, 
26.7636863402905, 27.9061355534616, 29.0042893079179, 30.0598650433847, 
31.2023142565558, 32.0349343462036, 33.0905100816704, 34.1460858171372, 
35.2885350303083, 36.1896794336209, 37.2878331880772, 38.2253264219165, 
39.3234801763728, 40.3381287433743, 0.991908465191337, 1.75489322666377, 
2.7468016918551, 3.46047086133959, 4.07964277397953, 5.13055069285498, 
6.08396897027074, 6.84695373174317, 7.8388621969345, 8.8632185722055, 
9.57688774168998, 10.4391343004915, 11.4634906757625, 12.3731701453471, 
13.2828496149318, 14.1925290845164, 15.1844375497078, 16.1378558271235, 
17.0912741045393, 17.8049432740238, 18.8292996492948, 19.8802075681702, 
20.6431923296426, 21.635100794834, 22.659457170105, 23.6513656352963, 
24.6047839127121, 25.5144633822967, 26.5063718474881, 27.125543760128, 
28.0352232297126, 28.9449026992973, 29.9368111644886, 30.6504803339731, 
31.6038986113889, 32.3668833728613, 33.3203016502771, 34.1825482090786, 
1.29298996115076, 2.88008216870556, 3.96870180584633, 5.2616917669971, 
6.39944846523036, 7.64233769672725, 8.66837198271614, 10.0210783809933, 
11.0878947100969, 12.1998964163031, 13.6219331868922, 14.9746395851693, 
16.2175288166662, 17.4190008180152, 18.711990779166, 20.0049807403168, 
21.1169824465229, 22.4099724076737, 23.6114444090227, 24.7000640461634, 
25.9930540073142, 27.0190882933031, 28.2619775248, 29.3997342230332, 
30.5117359292394, 31.8047258903901, 33.0061978917391, 34.0730142208428, 
35.2744862221918, 36.3864879283979, 37.6794778895487, 38.9724678506994, 
40.3945046212885, 41.7472110195657, 43.0402009807164, 44.2830902122133, 
45.8701824197681, 47.2228888180453, 1.14244921317105, 2.4806827570886, 
3.38182716040123, 4.52427637357228, 5.49958461525208, 6.5977383697084, 
7.39807580873082, 8.58660719550491, 9.45281209844173, 10.390305332281, 
11.6267776770133, 12.8153090637874, 13.9134628182437, 14.9690385537105, 
16.1114877668816, 17.2539369800526, 18.1914302138919, 19.3338794270629, 
20.3894551625298, 21.2905995658424, 22.4330487790135, 23.2333862180359, 
24.3315399724922, 25.306848214172, 26.2443414480113, 27.3867906611823, 
28.4423663966492, 29.308571299586, 30.3641470350528, 31.3016402688921, 
32.4440894820631, 33.5865386952342, 34.8230110399664, 36.0115424267405, 
37.1539916399116, 38.2521453943679, 39.5903789382855, 40.7789103250595, 
0.991908465191337, 2.08128334547164, 2.79495251495612, 3.78686098014746, 
4.5997207652738, 5.55313904268955, 6.12777963474551, 7.1521360100165, 
7.81772948678652, 8.58071424825894, 9.63162216713439, 10.6559785424054, 
11.6093968198211, 12.5190762894058, 13.5109847545971, 14.5028932197885, 
15.2658779812609, 16.2577864464522, 17.1674659160369, 17.8811350855214, 
18.8730435507127, 19.4476841427686, 20.4011024201844, 21.2139622053107, 
21.9769469667832, 22.9688554319745, 23.8785349015592, 24.5441283783292, 
25.4538078479138, 26.2167926093862, 27.2087010745776, 28.2006095397689, 
29.2515174586444, 30.2758738339154, 31.2677822991067, 32.2212005765225, 
33.3105754568028, 34.3349318320738, 1.2428892314969, 2.59559562977408, 
3.79706763112308, 5.14977402940026, 6.35124603074926, 7.64423599190003, 
8.71105232100364, 10.0040422821544, 11.504440002718, 12.6164417089241, 
13.9094316700749, 15.1523209015718, 16.3952101330687, 17.8172469036578, 
19.1102368648085, 20.2479935630418, 21.5409835241926, 22.7838727556895, 
23.8506890847931, 25.1436790459438, 26.4366690070946, 27.7296589682454, 
29.1516957388344, 30.3945849703313, 31.5960569716803, 32.8890469328311, 
34.131936164328, 35.2439378705341, 36.7443355910977, 37.882092289331, 
39.08356429068, 40.3765542518308, 41.7292606501079, 43.0819670483851, 
NA, NA, NA, NA, 1.09815375445633, 2.28668514123042, 3.34226087669724, 
4.53079226347133, 5.58636799893815, 6.7288172121092, 7.59502211504602, 
8.73747132821707, 10.0238183917451, 10.9613116255843, 12.1037608387554, 
13.2019145932117, 14.300068347668, 15.5365406924003, 16.6789899055714, 
17.6542981472512, 18.7967473604222, 19.8949011148785, 20.7611060178154, 
21.9035552309864, 23.0460044441575, 24.1884536573285, 25.4249260020608, 
26.5230797565171, 27.5786554919839, 28.721104705155, 29.8192584596113, 
30.7567516934506, 32.0430987569786, 33.0184069986584, 34.0739827341252, 
35.2164319472963, 36.4049633340703, 37.5934947208444, NA, NA, 
NA, NA, 0.953418277415756, 1.97777465268675, 2.8874541222714, 
3.9118104975424, 4.82148996712705, 5.81339843231838, 6.4789919090884, 
7.47090037427973, 8.54319678077214, 9.30618154224456, 10.2980900074359, 
11.2515082848517, 12.2049265622674, 13.2558344811429, 14.2477429463342, 
15.0606027314605, 16.0525111966519, 17.0059294740676, 17.6715229508376, 
18.663431416029, 19.6553398812203, 20.6472483464116, 21.6981562652871, 
22.6515745427028, 23.5612540122875, 24.5531624774788, 25.5065807548946, 
26.269565516367, 27.3418619228594, 28.1547217079858, 29.0644011775704, 
30.0563096427617, 31.0806660180327, 32.1050223933037, NA, NA, 
NA, NA, 1.29298996115076, 2.58597992230153, 3.75303049750283, 
5.04602045865359, 6.93875492269199, 8.36079169328107, 10.1442930752809, 
11.6446907958445, 12.8461627971935, 14.1391527583443, 15.7262449658991, 
17.1482817364882, 18.735373944043, 20.1574107146321, 21.6578084351956, 
23.0798452057847, 24.2813172071337, 25.448367782335, 26.6912570138319, 
27.9341462453288, 29.5212384528836, 30.9432752234727, 32.7267766054726, 
34.2271743260362, 35.6492110966253, 37.0712478672143, 38.2727198685633, 
39.4397704437646, 40.6412424451136, 41.8082930203149, 43.3953852278697, 
44.6883751890205, 45.9813651501713, 47.8740996142097, 49.2961363847987, 
50.5891263459495, 51.8821163071003, 53.3825140276639, 1.14244921317105, 
2.2848984263421, 3.29954699334351, 4.44199620651456, 5.9487877292247, 
7.18526007395696, 8.63362972467803, 9.91997678820602, 10.9755525236728, 
12.1180017368439, 13.4562352807614, 14.6927076254937, 16.0309411694113, 
17.2674135141435, 18.5537605776715, 19.7902329224038, 20.8458086578706, 
21.860457224872, 22.9586109793284, 24.0567647337847, 25.3949982777022, 
26.6314706224345, 28.0798402731556, 29.3661873366836, 30.6026596814158, 
31.8391320261481, 32.8947077616149, 33.9093563286163, 34.9649320640831, 
35.9795806310846, 37.3178141750021, 38.4602633881732, 39.6027126013442, 
41.1095041240543, 42.3459764687866, 43.4884256819577, 44.6308748951287, 
45.9172219586567, 0.991908465191337, 1.98381693038267, 2.84606348918419, 
3.83797195437552, 4.95882053575741, 6.00972845463285, 7.12296637407513, 
8.19526278056753, 9.10494225015218, 10.0968507153435, 11.1862255956238, 
12.2371335144993, 13.3265083947796, 14.377416313655, 15.4497127201474, 
16.5006206390229, 17.4103001086075, 18.272546667409, 19.2259649448248, 
20.1793832222405, 21.2687581025208, 22.3196660213963, 23.4329039408386, 
24.505200347331, 25.5561082662064, 26.6070161850819, 27.5166956546665, 
28.378942213468, 29.2886216830527, 30.1508682418542, 31.2402431221345, 
32.2321515873258, 33.2240600525172, 34.344908633899, 35.3958165527745, 
36.3877250179658, 37.3796334831572, 38.4519298896496)), .Names = c("Games", 
"League", "line", "value"), row.names = 115:570, class = "data.frame")

I have 4 columns: Games, League, line and value. I want to filter the data frame above to only include the highest value for each League+Line combination.

For example, I just want the highest value for the "Upper" Range of League "MLS". I'd like to do this for each combination of League and Line.

If any of this was unclear, please specify what and I'll update this post.

Psidom
  • 209,562
  • 33
  • 339
  • 356
madhavt
  • 79
  • 6

2 Answers2

3

Is this something you need?

library(dplyr)
test %>% group_by(League, line) %>% filter(value == max(value, na.rm = T))

# Source: local data frame [12 x 4]
# Groups: League, line [12]
# 
# Games League       line    value
# <int>  <chr>      <chr>    <dbl>
# 1     38    BPL      Upper 46.49371
# 2     38    BPL Prediction 40.33813
# 3     38    BPL      Lower 34.18255
# 4     38    LGA      Upper 47.22289
# 5     38    LGA Prediction 40.77891
# 6     38    LGA      Lower 34.33493
# 7     34    BLI      Upper 43.08197
# 8     34    BLI Prediction 37.59349
# 9     34    BLI      Lower 32.10502
# 10    38    LG1      Upper 53.38251
# 11    38    LG1 Prediction 45.91722
# 12    38    LG1      Lower 38.45193

You can also use data.table syntax:

library(data.table)
setDT(test)[test[, .I[which.max(value)], .(League, line)]$V1]

Or a more readable version:

setDT(test)[, .SD[value == max(value, na.rm = T)], .(League, line)]
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • a liitle more succinct and easier to follow, perhaps `test[, .(maxval=max(value)), by=.(League, line)]` – dww Aug 28 '16 at 00:35
  • @dww If you don't care about the `Games` column, that could be more efficient. Btw, you need to specify `na.rm = TRUE` any way since data contains missing values. – Psidom Aug 28 '16 at 00:38
  • @Psidom So, I just noticed I'm getting a weird error. The filter works well but it is leaving out one of my League variables. **Note**: my actual data set contains 6 different leagues.. I just cut it down so it would fit in the body of this post – madhavt Aug 28 '16 at 01:12
  • Not exactly sure, it could be the league has no data at all, i.e the value is NA for all records for that league. – Psidom Aug 28 '16 at 01:19
  • @Psidom surprisingly, it is the BLI League that isn't showing up. – madhavt Aug 28 '16 at 01:20
  • Did you specify `na.rm = TRUE` in the `max` function? There is missing data for that League. – Psidom Aug 28 '16 at 01:24
0

We can also do this with ave from base R

test[with(test, ave(-value, League, line, FUN = rank)<2),]
#    Games League       line    value
#152    38    BPL      Upper 46.49371
#190    38    BPL Prediction 40.33813
#228    38    BPL      Lower 34.18255
#266    38    LGA      Upper 47.22289
#304    38    LGA Prediction 40.77891
#342    38    LGA      Lower 34.33493
#376    34    BLI      Upper 43.08197
#414    34    BLI Prediction 37.59349
#452    34    BLI      Lower 32.10502
#494    38    LG1      Upper 53.38251
#532    38    LG1 Prediction 45.91722
#570    38    LG1      Lower 38.45193

Or using data.table, we convert the 'data.frame' to 'data.table' (setDT(test)), grouped by 'League', 'line', order the 'value' descendingly, and select the first row (head(.SD, 1)). It will be more efficient as showed in the benchmarks

library(data.table)
setDT(test)[order(-value), head(.SD, 1) , by = .(League, line)]
#   League       line Games    value
#1:    LG1      Upper    38 53.38251
#2:    LGA      Upper    38 47.22289
#3:    BPL      Upper    38 46.49371
#4:    LG1 Prediction    38 45.91722
#5:    BLI      Upper    34 43.08197
#6:    LGA Prediction    38 40.77891
#7:    BPL Prediction    38 40.33813
#8:    LG1      Lower    38 38.45193
#9:    BLI Prediction    34 37.59349
#10:   LGA      Lower    38 34.33493
#11:   BPL      Lower    38 34.18255
#12:   BLI      Lower    34 32.10502

The corresponding method that we can implement in dplyr is

library(dplyr)
test %>%
    group_by(League, line) %>%
    arrange(desc(value)) %>%
    slice(1L)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662