How to aggregate dataframe by minimum daily value - r

I am trying to extract values in a large data frame (with per minute data) where I want a subset of the data frame when x variable is at its daily minimum and maximum.
For an example data set see below. The example I have given has daily values to reduce the complexity - so in this case how would I get the monthly data frame values when temperature is at its minimum and maximum.
I do not have a great deal of experience in aggregate but I have been able to extract the monthly min and max temp values:
a <- as.data.frame(aggregate(df$Temp, df[4], function(x) {
c(max = max(x), min = min(x)) }))
But I am not sure how to do this without losing the information from the original data frame - it would almost be a subset based on a minimum and maximum argument? But I am not sure how to write that.
Any help would be appreciated - and apologies for the large dput example.
Thanks
df <- structure(list(Group.1 = c(1628, 1629, 1630, 1631, 1632, 1633,
1634, 1635, 1636, 1637, 1638, 1639, 1640, 1641, 1642, 1643, 1644,
1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652, 1653, 1654, 1655,
1656, 1657, 1658, 1659, 1660, 1661, 1662, 1663, 1664, 1665, 1666,
1667, 1668, 1669, 1670, 1671, 1672, 1673, 1674, 1675, 1676),
datetime = structure(c(1466078376.13352, 1466164800, 1466251194.49235,
1466337600, 1466423992.99026, 1466510400, 1466596853.49096,
1466683185.13551, 1466769600, 1466856000.06254, 1466942345.50765,
1467028800, 1467115179.92356, 1467201600, 1467288000, 1467374400,
1467460801.81376, 1467547200, 1467633604.67316, 1467720000,
1467806423.20361, 1467892800, 1467979255.99444, 1468065552.68428,
1468152000, 1468238400, 1468324827.121, 1468411200, 1468497619.36762,
1468584000, 1468670456.74548, 1468756798.41446, 1468843200,
1468928779.09091, 1469016500.50633, 1469102400, 1469188805.17385,
1469275200, 1469361564.70097, 1469448000, 1469534423.82046,
1469620800, 1469707247.98331, 1469793556.6064, 1469880000,
1469966391.40473, 1470041370, 1470178341.25, 1470217984.93671
), class = c("POSIXct", "POSIXt")), year = c(2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016), month = c(6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 8, 8, 8), day = c(16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3), hour = c(11.4937413073713,
11.5, 11.4979137691238, 11.5, 11.4979137691238, 11.5, 11.5146036161335,
11.4961779013204, 11.5, 11.5003474635163, 11.4853963838665,
11.5, 11.4940931202224, 11.5, 11.5, 11.5, 11.5003474635163,
11.5, 11.5006954102921, 11.5, 11.5066018068103, 11.5, 11.5159944367177,
11.4867872044506, 11.5, 11.5, 11.5076495132128, 11.5, 11.505211952745,
11.5, 11.5159944367177, 11.4993045897079, 11.5, 11.2755681818182,
11.6385372714487, 11.5, 11.5020862308762, 11.5, 11.4895688456189,
11.5, 11.5066109951287, 11.5, 11.5132127955494, 11.4881780250348,
11.5, 11.4979137691238, 8.3314447592068, 22.3854166666667,
9.38818565400844), min = c(29.4777468706537, 29.5, 29.5333796940195,
29.5, 29.5083449235049, 29.5, 29.5152990264256, 29.4815844336345,
29.5, 29.4801945795691, 29.4680111265647, 29.5, 29.5198054204309,
29.5, 29.5, 29.5, 29.5093815149409, 29.5, 29.5361613351878,
29.5, 29.4906184850591, 29.5, 29.4735744089013, 29.5041724617524,
29.5, 29.5, 29.4930458970793, 29.5, 29.5100764419736, 29.5,
29.4860917941586, 29.5152990264256, 29.5, 29.2840909090909,
29.5295358649789, 29.5, 29.4610570236439, 29.5, 29.5375521557719,
29.5, 29.500347947112, 29.5, 29.5069541029207, 29.4860917941586,
29.5, 29.4819193324061, 29.1133144475921, 28.7291666666667,
29.2911392405063), sec = c(30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), DOY = c(167.499723767578,
168.5, 169.499936254057, 170.5, 171.499918868799, 172.5,
173.500619108329, 174.499827957301, 175.5, 176.500000723882,
177.499369301499, 178.5, 179.499767633773, 180.5, 181.5,
182.5, 183.500020992587, 184.5, 185.500054087467, 186.5,
187.500268560343, 188.5, 189.500648083758, 190.499452364395,
191.5, 192.5, 193.500313900479, 194.5, 195.500224162227,
196.5, 197.500656776387, 198.499981648895, 199.5, 200.490498737374,
201.505792897328, 202.5, 203.500059882553, 204.5, 205.499591446453,
206.5, 207.50027569976, 208.5, 209.500555362386, 210.499497759233,
211.5, 212.499900517694, 213.367708333333, 214.953023726852,
215.41186269339), Day = c(1628.49972376758, 1629.5, 1630.49993625406,
1631.5, 1632.4999188688, 1633.5, 1634.50061910833, 1635.4998279573,
1636.5, 1637.50000072388, 1638.4993693015, 1639.5, 1640.49976763377,
1641.5, 1642.5, 1643.5, 1644.50002099259, 1645.5, 1646.50005408747,
1647.5, 1648.50026856034, 1649.5, 1650.50064808376, 1651.49945236439,
1652.5, 1653.5, 1654.50031390048, 1655.5, 1656.50022416223,
1657.5, 1658.50065677639, 1659.4999816489, 1660.5, 1661.49049873737,
1662.50579289733, 1663.5, 1664.50005988255, 1665.5, 1666.49959144645,
1667.5, 1668.50027569976, 1669.5, 1670.50055536239, 1671.49949775923,
1672.5, 1673.49990051769, 1674.36770833333, 1675.95302372685,
1676.41186269339), DayH = c(1628.47890588781, 1629.47916666667,
1630.47907974038, 1631.47916666667, 1632.47907974038, 1633.47916666667,
1634.47977515067, 1635.47900741256, 1636.47916666667, 1637.47918114431,
1638.47855818266, 1639.47916666667, 1640.47892054668, 1641.47916666667,
1642.47916666667, 1643.47916666667, 1644.47918114431, 1645.47916666667,
1646.4791956421, 1647.47916666667, 1648.47944174195, 1649.47916666667,
1650.47983310153, 1651.47861613352, 1652.47916666667, 1653.47916666667,
1654.47948539638, 1655.47916666667, 1656.47938383136, 1657.47916666667,
1658.47983310153, 1659.47913769124, 1660.47916666667, 1661.46981534091,
1662.48493905298, 1663.47916666667, 1664.47925359295, 1665.47916666667,
1666.47873203523, 1667.47916666667, 1668.4794421248, 1669.47916666667,
1670.47971719981, 1671.47867408438, 1672.47916666667, 1673.47907974038,
1674.34714353163, 1675.93272569444, 1676.39117440225), DayD = c(1628,
1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638,
1639, 1640, 1641, 1642, 1643, 1644, 1645, 1646, 1647, 1648,
1649, 1650, 1651, 1652, 1653, 1654, 1655, 1656, 1657, 1658,
1659, 1660, 1661, 1662, 1663, 1664, 1665, 1666, 1667, 1668,
1669, 1670, 1671, 1672, 1673, 1674, 1675, 1676), Sal = c(29.0488087045063,
29.0242089236389, 28.9704142552782, 28.9337236612778, 28.9124731267455,
28.8621913531181, 28.7694603506606, 28.6800432876789, 28.6368648858889,
28.5239238692008, 28.6274684358136, 28.6899766423333, 28.7094982390063,
28.6427974009653, 28.6540963996528, 28.5762126331528, 28.5617631359555,
28.6364940399097, 28.6458543031711, 28.603713066875, 28.65796586713,
28.6926140346389, 28.6470827340195, 28.5985220503964, 28.6620416165972,
28.6305870582222, 28.7001961730876, 28.6916165265, 28.6656868092356,
28.8356597378472, 29.266969874235, 28.418354432114, 28.3670879136597,
28.7002130192898, 28.1320816093038, 27.4618531637569, 27.5341382380668,
27.4453546479236, 28.2270356746662, 28.3642271282222, 27.9785534427697,
28.1165695540903, 28.6652365165229, 28.4222878245758, 28.4388172580139,
28.0149544998192, 28.4748151350047, 28.0933474488542, 27.8804163691308
), Temp = c(-0.902727819368567, -0.824054421545139, -0.720653055488178,
-0.64214159655, -0.557226600257997, -0.438045884220833, -0.395580348047288,
-0.295740618513551, -0.262320095793056, -0.160162734756081,
-0.20336842434701, -0.197846770197222, -0.133355481749131,
-0.0565102243486111, -0.0215979124673611, 0.125483112529167,
0.238572840179291, 0.272823149654167, 0.311757436682198,
0.392257187272917, 0.396628769779013, 0.475891600833333,
0.607289245171071, 0.644559951482615, 0.665206005440278,
0.727629137738889, 0.696052211995828, 0.752315860946528,
0.832433359182071, 0.471555079075, -0.187290608750348, 1.12906111324131,
1.24940833146181, 0.780415736372869, 2.0088144469993, 2.93373915290972,
3.19907420429903, 3.16430728506875, 1.87296632160014, 1.74067924683403,
2.50500692939318, 2.26088904221181, 1.20761122894784, 1.64661297725591,
1.68971783634167, 2.8011089384096, 1.70051260145987, 2.9201268373125,
3.38838510550127), Den = c(1023.3705136356, 1023.34875807153,
1023.30254908484, 1023.27139922778, 1023.25147035396, 1023.20777893194,
1023.13225399791, 1023.05719108895, 1023.02118660833, 1022.92671791522,
1023.01148932893, 1023.06187513264, 1023.07546443363, 1023.01910884861,
1023.02718869861, 1022.95948791944, 1022.94312314663, 1023.00176208264,
1023.00818908971, 1022.97111798194, 1023.01464410354, 1023.03946157917,
1022.99742747636, 1022.95676902782, 1023.00684491736, 1022.97892510208,
1023.03639020376, 1023.02648772361, 1023.00193750904, 1023.15208127222,
1023.5269599694, 1022.78984692211, 1022.74222825486, 1023.03256159162,
1022.51158633193, 1021.90175907847, 1021.95317423018, 1021.88198156806,
1022.59607175591, 1022.71358182847, 1022.35521831037, 1022.47925275556,
1022.98340138456, 1022.76567206815, 1022.77633019236, 1022.36132367177,
1022.80426542115, 1022.42016046875, 1022.21591253502), Chl = c(0.426618683276773,
0.415201663443056, 0.385096423775383, 0.363738871729861,
0.409404512937413, 0.434480798338889, 0.491604291206537,
0.464091430292564, 0.519904672929861, 0.549126851486449,
0.552805036465925, 0.557850158557639, 0.466429236588603,
0.510509837531944, 0.488934344839583, 0.3889341805625, 0.423089474131341,
0.499932285390278, 0.459906828269124, 0.365046922974306,
0.275903841463516, 0.249863925111806, 0.234845544844924,
0.225590052227399, 0.230629094999306, 0.214579363215278,
0.166625286886648, 0.171862695222222, 0.188135247756776,
0.2714899376625, 0.249608510288595, 0.234688186765647, 0.263209856711111,
0.223578790403409, 0.196086601016174, 0.300936248076389,
0.198270502905424, 0.254613127958333, 0.211631943366481,
0.194650132228472, 0.221967901366736, 0.287470503101389,
0.346767555667594, 0.272033042223922, 0.261851759145139,
0.30292882953338, 0.354326095381492, 0.139364686395833, 0.25231837455865
), O2 = c(8.40147872631572, 8.38937171346181, 8.41514077121905,
8.42152294330694, 8.47538879331154, 8.49880474437986, 8.47472190401391,
8.57655982897498, 8.61944234794514, 8.61577329576095, 8.69213896446314,
8.68921609862778, 8.69141974274149, 8.70994810763125, 8.68851865927569,
8.67396664622847, 8.76928090276998, 8.80177919844583, 8.80529066242629,
8.7617663811118, 8.70471848277554, 8.73156013737708, 8.76644351992003,
8.7618030795612, 8.79754675150208, 8.79138352644236, 8.75052502651043,
8.71269655839444, 8.68668315413829, 8.60423627197361, 8.6258442449847,
8.54381038276356, 8.66501514202014, 8.70410267486577, 8.4057585319121,
8.04968271129722, 7.92461467095619, 7.9134090372875, 8.41306227992907,
8.53320737933681, 8.33820448514335, 8.38417834865208, 8.62405400836439,
8.42477547407102, NaN, 8.40141680898052, 8.48576496565722,
8.28582430035417, 7.96892147137215), ID = c(1.21133857408067,
1.16902903935, 1.11473135400904, 1.07275230797986, 1.03223155043115,
0.995775405476389, 0.962935072082058, 0.91925791728214, 0.844455992659028,
0.791515976553162, 0.751516800569541, 0.702131858139583,
0.707145974122307, 0.599273624570139, 0.502570098100694,
0.347712868368056, 0.335673459499653, 0.152877532414716,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PAR = c(23.0673615360132,
32.1793662073708, 6.5780126555751, 26.73609441565, 48.6703905053255,
31.0519168669979, 32.4282362099165, 62.9277536311577, 42.349564172909,
36.1037009880507, 36.4754793940695, 41.2935785894486, 56.3411955151001,
32.0717618120021, 68.055826105709, 83.7724642720521, 92.4702250095455,
63.3510742622049, 123.118356252679, 127.241395072853, 134.184346299723,
144.712733899262, 152.801023269344, 145.640278660995, 149.976623338179,
159.125758044126, 185.67869649515, 154.650267769035, 118.350412925882,
97.6856430502583, 120.50500187914, 114.336144557309, 113.572030429865,
164.301402493774, 129.582420528291, 116.032838587456, 129.230413097896,
74.3598830959583, 156.888302579191, 146.709144185685, 170.418761938188,
109.206260157938, 107.575876383898, 116.456387998532, 101.787857506992,
138.683838377278, 59.2571219263966, 301.257992689583, 132.693163343932
), CO2 = c(313.287072418802, 316.458148259666, 311.875193224513,
310.725188107302, 307.113608999861, 305.345432183449, 306.866938424095,
296.338500576548, 301.685137467594, 301.496499046973, 292.31953961727,
288.615012611822, 290.213867048504, 285.695949347705, 287.207517193185,
288.916130984771, 284.025547924356, 280.331734920028, 276.455368840947,
283.035881080528, 288.114431753027, 281.591903017038, 277.471364486351,
275.133554284401, 272.650852387413, 276.396523718707, 279.08769762117,
282.910516518289, 287.561712111621, 298.595955828581, 290.299065880292,
280.221814783983, 271.902607398331, 267.655275534974, 187.578839181691,
295.344229054242, 294.793103994916, 299.124436790682, 275.716171689137,
271.748266282198, 274.022120842021, 276.591247188178, 268.405941518524,
267.118504699234, 265.624798023783, 275.240563041295, 266.857200690445,
79.6423144553947, 283.663994869992), IR = c(94.888329799096,
168.205345239028, 43.6012952622392, 155.376871086389, 249.315080590056,
188.485805169097, 140.05749463426, 327.30331523419, 317.749437070833,
335.839690080959, 288.73672165751, 315.9134794, 342.093960744267,
147.913321086785, 265.198319784722, 299.562780926042, 349.640961553857,
170.956328942361, 328.451052432198, 347.751372498958, 334.37511897533,
334.446311627778, 329.363355990612, 326.623001649513, 322.837199274444,
322.997446772569, 325.385474717316, 280.920902051319, 190.673680428353,
126.069364682462, 163.4228148, 153.172728333727, 166.565107412222,
251.82414954041, 241.950038763854, 186.326751968743, 252.239057356328,
114.703921665806, 231.317110343331, 217.123805467431, 263.428218097425,
150.468188479832, 152.846154624771, 164.441859235949, 149.983140035384,
190.64123323394, 86.6026193321907, 449.640322291667, 216.572470091966
), Wind = c(1.98109184856853, 4.53879830464599, 8.70032103138219,
6.59899662613967, 2.86950158330462, 1.91094036908824, 2.49644956157983,
4.51135762214745, 2.42873628334266, 1.82804668840925, 2.79143010703083,
2.16237533031289, 2.15848082990616, 2.82829126926611, 3.04592042806289,
4.43295455205038, 3.64900015248738, 5.93927428289323, 4.568747849922,
4.15613471303208, 2.91637168187178, 2.52179220100977, 1.44870759127444,
1.39712269197199, 2.07052311200698, 3.19886056692388, 4.59176673645728,
4.23987138922819, 6.5241126458074, 7.15729601227273, 5.78232411651958,
4.8622789499446, 4.40757803569404, 6.01049680807786, 4.37623822535244,
5.64634072332915, 6.9869768911053, 6.16325833892184, 4.01858391688531,
5.13520196430042, 4.98398472755042, 5.54085930760014, 6.1472408154439,
2.8608028792402, 3.73845798467388, 4.77812324101398, 2.78512103174882,
3.31994465129167, 2.24169682706943), AirTemp = c(4.40810744394993,
4.83240431381944, 2.53433683929068, 3.31246568041667, 3.30829270725313,
5.59009755125, 5.59340448650904, 7.34151981681723, 7.95149346486111,
6.35076665670605, 9.06164061349096, 10.7704471338194, 10.8565160628214,
7.49149095395833, 8.37798674340278, 6.50827215368056, 5.0225399762335,
7.40747958826389, 9.64073649255911, 9.69248163631944, 12.3917031494788,
14.0608254371528, 15.2186511161335, 17.322914922114, 17.6249810152778,
17.0803095690972, 15.2016485305981, 13.1528769309028, 12.8485860753996,
9.93430037097222, 8.49772086689847, 9.2253200445758, 7.55898443965278,
7.24284858572443, 6.94021240464135, 5.91370547875, 4.38009589235049,
5.42976892055556, 7.28829746376912, 6.63289298236111, 6.79095412268615,
9.45812934520833, 9.2179658081363, 10.4900065421419, 9.56056064826389,
10.5006926291377, 9.92910989490085, 11.8657420520833, 11.0606573877637
), Press = c(1010.94713220445, 1000.91298554167, 987.981428212796,
1003.31839191667, 1010.80608631433, 1010.70622420833, 1014.20480271905,
1018.25909922168, 1017.2977571875, 1017.2116500139, 1014.43729111961,
1016.86667607639, 1017.77638776233, 1017.73254004861, 1017.58831039583,
1023.96083316667, 1023.37822454482, 1018.61740789583, 1017.69460385953,
1019.06393609722, 1019.70283468381, 1021.57185751389, 1022.54906664812,
1019.93420589013, 1015.60680218056, 1013.92687847222, 1018.10282961752,
1017.75759179861, 1011.06972191105, 1000.6753998125, 1006.33157139777,
1010.47605901252, 1004.2360214375, 1003.47128640625, 1005.9544021519,
1002.94591790972, 998.129144311544, 994.845405819444, 996.572536383866,
1007.62132047917, 1015.58149314544, 1015.31391086806, 1014.80282973574,
1014.86646130737, 1011.12184847917, 1007.64412461752, 1008.94791512748,
1016.38731552083, 1014.23599833755), Ts = c(0.0939091805522304,
0.0933571324389885, 0.0926315816934272, 0.0920807064972713,
0.0914849244286843, 0.0906487390808381, 0.0903508008513881,
0.0896503398391888, 0.0894158788365697, 0.0886992027701387,
0.089002314704399, 0.0889635640417059, 0.0885111330762104,
0.0879720536464657, 0.0877271381692247, 0.0866953766257083,
0.0859020961040837, 0.0856618525704464, 0.0853887527876392,
0.0848241204226848, 0.0847934498305695, 0.0842374892345861,
0.0833158824031102, 0.0830544806008765, 0.0829096777186984,
0.0824718703347108, 0.0826933403725865, 0.0822988121621235,
0.0817369399215142, 0.0842685237873781, 0.0888896196011912,
0.079656634577284, 0.0788127760214962, 0.0821017792323535,
0.0734881871639127, 0.0670067820465301, 0.065144627947714,
0.0653888659545473, 0.0744405166847922, 0.0753679790243025,
0.0700098285152706, 0.0717215346483428, 0.0791058326941815,
0.0760274126969679, 0.0757251777215326, 0.0679345115120072,
0.0756495928144236, 0.0670994071000443, 0.0638174503450037
), C = c(8.94065543989075, 8.91870846791228, 8.89129215782166,
8.87041424509199, 8.84686764433126, 8.81516998854457, 8.8084814071784,
8.78498979939825, 8.77801254243485, 8.75556135353215, 8.76169863848835,
8.75623839427317, 8.73659381953759, 8.71875839369915, 8.7081454153551,
8.67125827547341, 8.64042586696951, 8.62643884773293, 8.61505698749588,
8.59531077426103, 8.59085475620887, 8.56693513415321, 8.53362826949292,
8.5263455666875, 8.51701535738585, 8.50192437544613, 8.50643587795999,
8.49224025025525, 8.47229959883608, 8.56396191736222, 8.71907216880554,
8.40708575426135, 8.37898781319225, 8.48450883536676, 8.19834292777621,
8.02742642860397, 7.94335742141408, 7.96038019864108, 8.22636890956846,
8.25191633282365, 8.08596024446269, 8.14084822127661, 8.37247267374788,
8.27175018847621, 8.25980001007453, 8.01532360653014, 8.25581090260999,
7.97597778883369, 7.87850954070042), deltaO = c(-0.539176713575035,
-0.529336754450473, -0.476151386602607, -0.448891301785048,
-0.371478851019715, -0.316365244164706, -0.333759503164495,
-0.20842997042327, -0.158570194489716, -0.139788057771207,
-0.0695596740252039, -0.0670222956453951, -0.0451740767961,
-0.00881028606789499, -0.0196267560794031, 0.00270837075506498,
0.12885503580047, 0.1753403507129, 0.190233674930407, 0.166455606850779,
0.11386372656667, 0.164625003223876, 0.23281525042711, 0.235457512873693,
0.280531394116234, 0.289459150996232, 0.244089148550445,
0.22045630813919, 0.214383555302208, 0.0402743546113934,
-0.0932279238208418, 0.136724628502207, 0.286027328827889,
0.219593839499005, 0.207415604135885, 0.0222562826932507,
-0.0187427504578891, -0.0469711613535759, 0.18669337036061,
0.281291046513154, 0.25224424068066, 0.243330127375477, 0.251581334616514,
0.153326651891512, NaN, 0.285793738567012, 0.229954063047229,
0.30984651152048, 0.0904119306717343), k = c(1.3843960648632,
6.67024147531018, 21.4898676969447, 12.6511158639218, 2.86455163396347,
1.18964832218751, 2.48536575543307, 6.11457188358869, 2.22117060425211,
1.39709711243724, 3.15681260593466, 1.69087035092879, 1.96031515063071,
3.05646023068449, 3.88091903012736, 5.89522588325253, 4.61191456284459,
9.9876497231258, 5.87859816406069, 5.25567516554151, 2.66410131584561,
1.91750845984603, 0.912508517423828, 0.788437993630362, 1.51297375451523,
4.1010391646833, 6.69386108760172, 6.60202002300452, 15.3120378300758,
17.0865393002387, 10.0683520284002, 7.04605349774694, 7.06555793241213,
11.2155892631143, 6.95932652757971, 10.5460039186381, 14.4457642190408,
11.1713965641408, 5.13491665227681, 9.17952853060783, 7.55395359942885,
9.93963153178716, 10.7556273136677, 2.74976182917367, 4.24852415082047,
7.44862825836769, 2.51232305022297, 3.16831051334729, 1.58255842462718
), NCP = c(-6555.51823916664, -30259.5423200574, -93284.1485540249,
-52785.5627897263, -9203.47328702618, -3474.14113092716,
-6643.58632076862, -11084.7222504768, -2779.86321684949,
-1481.582285219, -1974.24446012626, -1018.46398725869, -689.919166266737,
1680.8563208385, -442.391803921574, -180.816469873293, 5330.66701930139,
16192.8444708129, 10544.1353158852, 8370.10647897766, 2991.63246971473,
2741.5747232174, 1716.98452107368, 1723.27625157814, 3806.41762190115,
10674.6146528716, 15783.1240720338, 14337.3594334794, 30669.7204136871,
3257.08033198347, -8574.53718630027, 10409.6993939132, 17974.5471722579,
22284.8034527993, 11849.079774671, 7706.03700970845, 2652.66574996729,
-10421.7431381603, 8448.71926418016, 24212.1390873852, 18754.1407791307,
21358.338661014, 22616.1137584938, 3695.2416322836, NaN,
18437.8290676373, 4903.94862723591, 8770.18358118303, 1206.37283774814
)), .Names = c("Group.1", "datetime", "year", "month", "day",
"hour", "min", "sec", "DOY", "Day", "DayH", "DayD", "Sal", "Temp",
"Den", "Chl", "O2", "ID", "PAR", "CO2", "IR", "Wind", "AirTemp",
"Press", "Ts", "C", "deltaO", "k", "NCP"), row.names = 1324:1372, class
= "data.frame")

If the output you are looking for is that subset of df rows having maximum or minimum Temp among all rows with the same month value then:
subset(df, ave(Temp, month, FUN = function(x) x %in% range(x)) == 1)

Related

Filter dates to show only the current month and 12 months ahead in R

I have a df with some dates and I would like to filter dates to show only the current month and 12 months ahead.
This is my df:
I would like to keep, for each date in the Date column, in the DataReferencia column, the dates of the current month and 12 months ahead and then subtract the values from the Value column. For the above dates, on the day 2003-01-17, it would be the dates in the DataReferencia column 2003-01-01 and 2003-12-01. This df runs from 2003-01 to 2020-12.
I tried this code, but returns an empty df:
library(dplyr)
library(lubridate)
test %>%
filter(year(DataReferencia) == Data.Ano & month(DataReferencia) == Data.Mes + 11,
month(DataReferencia) == Data.Mes)
My dput:
structure(list(Instituicao = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1), Data = structure(c(12069, 12069, 12069,
12069, 12069, 12069, 12069, 12069, 12069, 12069, 12069, 12069,
12070, 12070, 12070, 12070, 12070), class = "Date"), DataReferencia = structure(c(12053,
12084, 12112, 12143, 12173, 12204, 12234, 12265, 12296, 12326,
12357, 12387, 12053, 12084, 12112, 12143, 12173), class = "Date"),
Valor = c(26, 24, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
26, 24, 22, 22, 22), DataReuniao = structure(c(12073, 12073,
12073, 12073, 12073, 12073, 12073, 12073, 12073, 12073, 12073,
12073, 12073, 12073, 12073, 12073, 12073), class = "Date"),
Reuniao = c(80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80), MetaSelic = c(25.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5)), row.names = c(NA, 17L), class = "data.frame")
If I understand your question correctly, you want to filter() for dates where the year & month are the same in Data and DataReferencia, or the date in DataReferencia is 11 months ahead of Data. I'm not sure what Data.Ano and Data.Mes are in your failed code, or if these are translated names of the columns names?
This code will do the job:
test %>%
filter(
format(DataReferencia, format = '%Y-%m') == format(Data, format = '%Y-%m')
| format(DataReferencia, format = '%Y-%m') == format(Data + months(11), format = '%Y-%m')
)
# Instituicao Data DataReferencia Valor DataReuniao Reuniao MetaSelic
# 1 1 2003-01-17 2003-01-01 26 2003-01-21 80 25.5
# 2 1 2003-01-17 2003-12-01 22 2003-01-21 80 25.5
# 3 1 2003-01-18 2003-01-01 26 2003-01-21 80 25.5
We use format() to retrieve the date of the data columns in year-month format; we specify this using format = %Y-%m, utilising symbols and abbreviations explained here; basically %Y means the (4-digit) year, and %m is the (2-digit) month. Because this is still in R-recognised date format, it allows the addition of 11 months in the second condition in filter().

subtract the values in one dataframe by the mean created by summarise in dplyr

I have two dataframes, one is the original data:
df <- structure(list(day = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2), Subject = c(1, 1, 1, 1, 1, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 12, 12, 12, 12,
12, 12, 13, 13, 13, 13, 13, 13, 17, 17, 17, 17, 17, 17), TimePoint = c(1,
5, 9, 13, 17, 21, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
1, 5, 9, 15, 17, 21, 1, 5, 9, 13, 17, 21, 1, 5, 9, 13, 17, 21,
1, 5, 9, 13, 17, 21), C4b = c(489.1, 3757.5, 4013.7, 963.4, 668.8,
2135.4, 1793.5, 2485.9, 1337.2, 1200.1, 1854.5, 1163.8, 1050.4,
1514.7, 1134, 1283.9, 1883.2, 1160, 441.4, 318.4, 261.8, 392,
380.9, 312, 1731.1, 1482.9, 1635.6, 1144.3, 1455.3, 1427.1, 1202.6,
1212.7, 1294.8, 1406.6, 2153.3, 1135, 1104.5, 1129.3, 737.4,
1010.9, 934.3, 704.2), `Coagulation Factor XI` = c(2149.4, 2055.8,
2256.3, 2179, 1859.5, 2158.4, 1877.4, 1800.7, 1838.7, 1899.9,
1867, 1827.9, 1818.7, 1847, 1807.7, 1737, 2394.9, 1726.2, 1756.3,
1746.3, 1744.5, 1573.8, 1765.5, 1954.1, 1522.4, 1387.7, 1374.4,
1357.3, 1433.3, 1386.5, 1575.7, 1531.8, 1560.9, 1810.4, 1657.2,
1509.4, 2110.4, 1974.5, 1856.1, 1939.6, 1832.3, 1932.5), CTACK = c(798.9,
857.5, 702.9, 707.6, 903.7, 749.9, 689.2, 734.1, 716.8, 645.2,
641.2, 663.1, 733.3, 667.3, 774.3, 782.2, 1766.7, 679.2, 1950.6,
2084.9, 2021.5, 1930.8, 1952.3, 1917.2, 723.9, 709.2, 670.7,
716.4, 944.9, 655.7, 706.8, 734.2, 680.5, 720.4, 778.3, 790.2,
962.5, 726.4, 761.9, 849.2, 701.1, 664.3), Endostatin = c(30563,
30006.5, 25972.2, 28048.9, 24503.9, 30371.2, 49733.9, 54933.8,
54293.3, 60007.4, 60403.9, 58870.4, 56801, 59752.6, 54336.3,
47746, 21270.4, 67312.9, 61931.5, 66395.3, 65680.8, 64419.4,
63415, 67230.7, 49444.2, 55122.2, 52333, 62328.7, 47513.9, 61530,
52549.8, 53633, 53983.7, 49023.8, 47305.6, 50569.8, 58754.1,
65727.9, 78382, 68290.8, 69386.8, 59982.4), `TIMP-1` = c(279.4,
262.4, 295.9, 273.3, 294.9, 278.3, 279.4, 238.7, 224.2, 239.3,
235.7, 251.7, 228.2, 237.3, 258.4, 262.4, 389.4, 237.8, 303.1,
296.4, 295.4, 297, 317, 351.6, 310.4, 244.2, 250.8, 261.1, 283.1,
251.4, 242.2, 223.3, 234.6, 291.2, 262.3, 249.7, 293.9, 259.4,
238.9, 257.9, 248.2, 329.3), tPA = c(377.9, 326.9, 346.3, 318.3,
314, 348.2, 291.2, 427.8, 433.1, 428.8, 370.9, 390, 354, 354.4,
368.8, 364.7, 595.2, 400.1, 354, 356.8, 368.7, 363.5, 394.3,
412.3, 319, 406.9, 415.4, 292.3, 320.9, 348.2, 532, 457.4, 417.3,
404, 401.5, 533.5, 213.6, 346.7, 310.7, 292.6, 280.2, 287.1),
`EG-VEGF` = c(106.8, 102.2, 107, 108.1, 138.8, 107.5, 88.1,
85.7, 82.7, 91.7, 98.8, 91.1, 82.3, 90.3, 89.2, 94.9, 128.3,
94.6, 529.1, 679.3, 599.4, 644.5, 674.1, 584.5, 98.2, 101.1,
97.3, 95.3, 86.8, 92.5, 101.9, 94.9, 109, 94.1, 103.8, 98.2,
92, 98.8, 91.6, 92.9, 98.8, 105.8), `TIMP-2` = c(466.5, 420.7,
479.8, 423, 462.7, 393.1, 400.5, 380.3, 355.9, 393.7, 423.6,
401.6, 364.2, 372.8, 391.3, 480.6, 1171.4, 357.8, 533.7,
522.6, 523.1, 511.5, 511, 644.8, 405.3, 398.2, 393, 369.7,
427.8, 380, 396.3, 394.5, 407, 490.9, 435.6, 402.7, 424.3,
436, 391.6, 405.6, 414.2, 536.2), `TGF-b1` = c(1451.2, 1194.6,
1521.3, 1705.8, 1945.4, 1900.2, 1839.4, 1701.9, 1752.9, 1714.6,
1597.6, 1660.5, 1786.9, 1694, 1519.6, 1906.1, 1654.2, 1636,
1566.8, 1730.9, 1496.2, 1850.9, 1715.1, 1648.8, 1834.9, 1686,
1769.4, 1750.8, 1833, 1862.6, 1511, 1505.9, 1524.9, 1514,
1391.2, 1369.1, 2064.6, 1790.9, 1799.1, 1548.2, 1839, 1667
), `VEGF sR3` = c(3094.3, 3235, 3116.3, 3027.1, 2536, 3061.8,
3215.7, 3189.6, 3139.1, 3618.8, 3585.6, 3556, 3326.7, 3167.5,
3403.3, 2583.1, 1621.6, 3415.2, 2293.9, 2508.4, 2662.6, 2486.2,
2538.8, 2452.5, 3012.5, 2920.1, 2914.1, 2870, 2455.2, 2809.3,
3534.8, 3501.9, 3691.4, 3517.6, 3414, 3405.6, 1943.3, 2334.2,
2137.4, 2185.7, 2273.2, 2304.9), C5 = c(5566.9, 5466.7, 5591.6,
5552.7, 5348.3, 5388.4, 5499.1, 5834.1, 5556.7, 5737.5, 5632.2,
5886.4, 5543.2, 6134.6, 5597.3, 5557.9, 5446.3, 5863.7, 5699.8,
5263.7, 5773.3, 5313.7, 6014.2, 5453.3, 6284.5, 5658.5, 5491.2,
5855.1, 5477.2, 5815.2, 5938.1, 5660.6, 6190.9, 5626.5, 6248.6,
5673, 6071.4, 6262.5, 5649.1, 6052, 6000.4, 5649), `Apo E` = c(3351.8,
936.5, 928.1, 3096.5, 2267.6, 2217.1, 2143.2, 2547.1, 2368.6,
2531.5, 1922.4, 2134.3, 2283.7, 2131, 2260.3, 2249.8, 2001.2,
2271.8, 3635.3, 2338.7, 2301.2, 2705, 2604.3, 2738.5, 5091.4,
3638.9, 2710.4, 3605, 3683, 3016.1, 3698.2, 3050.5, 2162.1,
2086.9, 2500.5, 2449.1, 4416, 2628.9, 2902.4, 3100.8, 2433.7,
3083.3), BDNF = c(325, 324, 337.9, 436.6, 557.3, 379.5, 556.2,
458.3, 400.3, 447.4, 448.1, 450.3, 425.1, 456.4, 482.1, 585,
663.2, 424.9, 343.8, 337.2, 328.1, 417.9, 343.2, 386.1, 590.6,
359.9, 340.3, 360.5, 394.1, 374.6, 367.9, 294.1, 319.4, 320,
366.2, 372.1, 502.1, 403.1, 432.9, 397.3, 389, 364.5), `bFGF-R` = c(12689.3,
13327.1, 12159.8, 11959.7, 11430.1, 11628.7, 11430.7, 12808.9,
11439, 12836.8, 13735.2, 12351.3, 11754, 12071.3, 11841.7,
10368.6, 12122.4, 12371.7, 11184.5, 11499.1, 11687.9, 10997.4,
11006.7, 10709.3, 11615, 12553.3, 11459.9, 12403, 10952,
12060.3, 13330.8, 12688.8, 13717.2, 11868.7, 11919.1, 11584.5,
10987.7, 12370.3, 11619.4, 11737.2, 12695.7, 12403.2), C8 = c(1868.7,
1678.4, 1916.9, 1966.6, 1724.9, 2028, 1840.3, 1797.4, 1871.4,
1967.9, 1926.4, 1860.2, 1860.4, 1901.4, 1951.1, 1675.4, 1026,
2024.3, 1950.6, 2174.5, 2266.5, 2251.5, 2176.4, 2067.2, 1968,
2187.1, 2360.5, 2057.5, 1971.1, 2202.2, 2099.5, 2337.2, 2320.8,
2232.9, 2358.2, 2374.8, 2071.3, 2773.3, 2695.9, 2641.2, 2738.7,
2607), `Cathepsin G` = c(1434.9, 1347, 1410.5, 1421.1, 2318.2,
1675, 1025.9, 1217.4, 1021.9, 1088.4, 1034.2, 1099.8, 1080.1,
1051.2, 967.8, 3874.4, 1875.6, 1058.9, 1093.7, 1083.2, 922.1,
1204.4, 1126.1, 1157.9, 1642.2, 1528.4, 1627.3, 1811.6, 1498.4,
1759.8, 926.3, 1022.4, 1106.7, 1058.7, 999.1, 995.3, 1020.5,
1085.4, 1088.9, 961.9, 1135.7, 1080.7), `CXCL16, soluble` = c(6878.8,
7479.3, 7088.7, 6739.4, 6143.9, 7477.5, 6742, 6822.4, 6164.9,
7320.9, 7459.1, 7150.5, 6407.1, 6504, 6901.8, 5929.6, 3924.1,
7827.3, 7358.7, 7758.3, 8305.5, 7586.9, 7829.1, 8268.3, 6860.7,
7470.7, 6857, 7003.7, 6364.7, 7738.7, 5441.6, 5609.3, 5732.5,
5128.2, 5642.7, 5402.4, 5170.2, 6272, 5963, 5996.4, 6461,
6721.3), `FGF-10` = c(255.2, 210, 262.6, 303.6, 219.4, 301.6,
241.2, 283.5, 248.6, 260.1, 265.7, 299.5, 268.9, 265.3, 201.3,
190.3, 296.6, 239.5, 242.7, 287.3, 216.2, 288.8, 283.8, 246.7,
255.1, 268.6, 236.3, 258.4, 206.8, 259.4, 264.1, 233.2, 277.1,
205.7, 200, 193.1, 251, 228, 229.9, 213.6, 247.2, 235.2),
`FGF-8B` = c(712.8, 648.4, 704.5, 763.9, 837.4, 795.9, 836.1,
841.7, 872.4, 860.1, 921.1, 845.8, 843.6, 875.2, 850, 847.4,
934.7, 938.6, 715.6, 823.9, 733.3, 801.1, 800.3, 819.9, 791.1,
930.8, 938.3, 869.6, 794.8, 939.9, 925.9, 971.2, 1015.9,
928.8, 851.9, 827, 833.6, 837.7, 858, 828.2, 935.7, 900.5
), GIIE = c(270, 254.6, 268.7, 279, 224.4, 293.2, 239.8,
238, 222.9, 260.2, 282.3, 262.3, 239, 241, 238.7, 200.6,
207.3, 252, 270.8, 281.1, 354.2, 289.2, 293, 295.2, 274.5,
291.4, 270.6, 275.8, 232, 272.7, 267, 245.3, 278.3, 260.6,
264, 250.6, 232.9, 274.4, 256.2, 254.1, 271.2, 282), GV = c(433.7,
441.1, 438.9, 468, 425.6, 459.2, 317.6, 332.2, 326, 306.4,
307.4, 310.6, 347.9, 317, 273.1, 325.9, 798.4, 299.1, 327.8,
307.9, 258.6, 308.7, 306.6, 298.6, 319.2, 326.2, 299.8, 329,
436.5, 297.7, 320.9, 306.6, 314.1, 312.1, 298.1, 300.1, 417,
306.6, 314.7, 321.4, 304.8, 305), `IL-12` = c(725.7, 667,
734.8, 772.8, 1045.1, 829.4, 659.4, 695.3, 653.8, 672.8,
701.1, 658.1, 683.5, 670.8, 678.8, 1002.5, 991.4, 703.8,
667.9, 714.1, 630.6, 720, 689.8, 781.4, 671.2, 715.2, 748.9,
693.2, 723.5, 724.7, 868.6, 891.2, 917.9, 858.8, 868.9, 828.6,
744.7, 711.9, 715.1, 683.2, 740.2, 724.3)), row.names = c(NA,
-42L), class = c("tbl_df", "tbl", "data.frame"))
My original data has an enormous amount of variables (1,130) of which I used the library(dplyr) summarise function to create mean values by TimePoint for each of the columns.
df_mean <- df %>%
group_by(TimePoint) %>%
summarise_at(vars(C4b:GV), mean, na.rm = T)
Now I want to create a new dataframe that is simply the difference between the original values in df and the mean of each time point for all the variables so essentially df - df_mean = new dataframe except for the variables "day", "Subject", and "TimePoint".
Thank you!
scale() function from R Base, with scale = FALSE for the centering option
mutate_at() to apply to multiple variables at once
vars() to select list of variables
! to select complement of variables - you don't want it applied to day and Subject
library(tidyverse)
df_centered <- df %>%
group_by(TimePoint) %>%
mutate_at(vars(!c("day", "Subject")), ~scale(., scale = FALSE), na.rm = TRUE)
I tried something like this, hope it helps.
df1 <- df[,-c(1,2,25)]
df_mm <- full_join(df1, df_mean, by = "TimePoint")
df_ss <- NULL
names <- colnames(df)[-length(df)]
for(i in 2:length(df1)){
df_ss[[i]] <- (df_mm[,i] - df_mm[,length(df1)+i-1])
}
df_ss[1] <- NULL
df_ss <- do.call(cbind, df_ss)
df_ss <- mutate(df_ss,
day = df$day,
Subject = df$Subject,
TimePoint = df$TimePoint)
df_ss <- df_ss[,c(22:24,1:21)]
colnames(df_ss) <- names
View(df_ss)

Predicting effects of a cyclic covariate in a spatial GAM-GEE

I have fit a GAM-GEE in the {geepack} package as I wanted to account for within individual residual autocorrelation for some spatial animal data with known individuals. I have a cyclic covariate I want to fit using a cyclic spline and so I did this by generating the basis functions for the spline first in {mgcv}, as cyclic cubic splines are not currently available in the {splines} package, and then using this as a covariate in the geeglm() fit. I did this by first:
cyclic_basisfunc <- mgcv::gam(response ~s(cyclic_term, bs="cc"), fit=F, data=df, family = "binomial")$X %>%
as_tibble() %>%
dplyr::select(V2:ncol(.)) %>%
as.matrix()
and then:
fit1 <- geepack::geeglm(response ~ cyclic_basisfunc +
bs(beta1)+
bs(lon+lat),
family="binomial",
data=df,
id=as.factor(id),
corstr = "independence")
I can estimate the partial effects of the cyclic smooth by extracting the relevant elements of the model matrix which correspond to that term (in this case 8 basis functions for the cyclic term). However when it comes to predicting over a spatial grid, using the predict() function, I cannot figure out how to appropriately name the elements in the prediction grid. The input variable for the cyclic term was a matrix and any attempt to include the prediction values either as a numeric vector or a matrix returns faults.
I tried generating a prediction grid over my study area as such (n.b. have tried using both the name of the cyclic_basisfunc and cyclic_term as column headers):
predgrid <- data.frame(expand.grid(lat=seq(min(df$lat-0.1), max(df$lat+0.1), length.out = 20),
lon=seq(min(df$lon-0.1), max(df$lon+0.1), length.out = 20),
cyclic_basis_func=seq(min(cyclic_basis_func),
max(cyclic_basisfunc), length.out=20),
beta1=25)))
predict(fit1, predgrid)
and get the error:
Error: variable 'cyclic_basisfunc' was fitted with type "nmatrix.8" but type "numeric" was supplied
This makes sense as the original covariate was fit as an 8 column matrix, 1 for each basis function. Have tried inputting a column into the prediction gird as an 8 column matrix yet still this does not seem to work. Is there a way to generate an appropriate prediction grid for this model? (dput for sample of data below).
df <- structure(list(response = c(0.117, 0.915, 0.22, 0.284, 0.551,
0.871, 0.25, 0.261, 0.117, 0.875, 0.67, 0.533, 0.324, 0.138,
0.154, 0.286, 0.935, 0.744, 0.118, 0.224, 0.865, 0.13, 0.889,
0.115, 0, 0, 0.703, 0.917, 0.812, 0.14, 0.219, 0.114, 0.24, 0.163,
0.115, 0.228, 0, 0, 0.115, 0.106, 0.243, 0.13, 0.908, 0.117,
0.472, 0.95, 0.217, 0.133, 0.744, 0.92, 0.26, 0.138, 0.958, 0.113,
0.147, 0.132, 0.496, 0.148, 0.119, 0.186, 0.721, 0.889, 0.162,
0.157, 0.269, 0.25, 0.129, 0.357, 0.188, 0.361, 0.137, 0.128,
0.872, 0.121, 0.135, 0.466, 0.15, 0.589, 0.138, 0.134, 0.122,
0.463, 0.121, 0.369, 0.813, 0.145, 0.911, 0.17, 0.123, 0.649,
0.476, 0.119, 0.367, 0.135, 0.923, 0.875, 0.119, 0.115, 0.895,
0.923), cyclic_term = c(0.732222222222222, -2.28277777777778,
2.56777777777778, -3.43333333333333, -0.89, -5.68611111111111,
-3.47388888888889, -2.88277777777778, -1.79277777777778, 1.50333333333333,
-0.910555555555556, -4.14944444444444, -0.379027777777778, 0.113333333333333,
0.075, -3.99055555555556, -5.48388888888889, -1.84513888888889,
0.286111111111111, -1.24833333333333, -2.19652777777778, -0.532222222222222,
0.598333333333333, 5.43222222222222, -2.73222222222222, -1.11125,
3.16833333333333, -2.88055555555556, 1.90319444444444, 2.585,
-5.64333333333333, -3.79666666666667, 0.692083333333333, 5.80666666666667,
-4.42333333333333, 1.95666666666667, 2.61722222222222, -5.90055555555556,
1.63, 3.55222222222222, -4.20111111111111, -2.34, 3.39277777777778,
-4.32944444444444, 1.32222222222222, 4.74388888888889, 0.251111111111111,
0.0705555555555556, -1.84513888888889, 5.14305555555556, -3.92555555555556,
-2.34277777777778, 2.55777777777778, -3.75944444444444, 2.32277777777778,
1.82944444444444, -3.42111111111111, 3.22388888888889, -3.90055555555556,
1.94, -5.01888888888889, 4.93902777777778, -2.97388888888889,
4.11222222222222, 1.55055555555556, -2.29055555555556, 0.556666666666667,
1.40375, 3.52944444444444, 4.56555555555556, 1.30611111111111,
-2.59944444444444, 4.11166666666667, 6.005, 1.28111111111111,
-2.35333333333333, -0.125, 0.991666666666667, -4.29055555555556,
4.64055555555556, 1.19222222222222, -0.710555555555556, 0.125,
3.835, -3.79722222222222, 1.46, 0.455833333333333, -5.855, 2.59277777777778,
-1.42777777777778, 4.815, 0.508888888888889, -2.14333333333333,
-1.47444444444444, 5.01847222222222, 3.06666666666667, 5.92388888888889,
1.39944444444444, 5.00236111111111, 4.21666666666667), beta1 = c(32.95,
28.8, 32.15, 32.75, 34.7, 29.7, 28.95, 28.85, 32.25, 28.5, 33.5,
28.5, 30.75, 28.8, 32.4, 32.65, 29.7, 32.25, 29.7, 31.85, 32.15,
31.45, 31.25, 31.05, 38.7, 35.2, 31.65, 33, 32.05, 28.7, 31.85,
35.5, 31.25, 35.25, 33.25, 29.7, 35.5, 30.55, 35.45, 36, 33,
29.85, 31.15, 33.35, 34.8, 28.1, 35.35, 28.8, 32.25, 29.3, 29.7,
28.95, 28.4, 34.7, 28.5, 28.8, 28.5, 33.1, 36.35, 29, 26.95,
33.05, 32.2, 29.2, 30.35, 36, 29.7, 34.25, 34.1, 31.9, 32.05,
28.9, 33.3, 31.85, 32.55, 28.8, 29.1, 39.2, 28.95, 32.15, 28.8,
33.1, 29.5, 37.95, 32.85, 28.5, 30.3, 34.55, 28.15, 33.35, 35.35,
31.6, 35.95, 28.9, 31.1, 32.5, 35.7, 31.85, 32.95, 33.55), lon = c(-8.14386899769604,
-8.1572279376935, -8.15157242384156, -8.11266145447895, -8.15174118001044,
-8.15335952072546, -8.14600667978252, -8.15297882985764, -8.15568356665537,
-8.13472008705139, -8.09368161533273, -8.10491923518749, -8.1603014305949,
-8.15632063618518, -8.13543502792374, -8.09733172904193, -8.15868642071182,
-8.12876868592058, -8.15690393084368, -8.10847025857788, -8.15564957894737,
-8.16047965739412, -8.1538774272955, -8.13959002494812, -8.16031308174808,
-8.13153629064039, -8.10225327088153, -8.11704735322503, -8.10320579591837,
-8.09718723480212, -8.14769670963066, -8.15279598640478, -8.1536752924518,
-8.15005347845117, -8.11959004402161, -8.15327362169542, -8.15338984397156,
-8.15480377425017, -8.14843624352758, -8.1536150198704, -8.14265275265451,
-8.15419676931013, -8.14388546959556, -8.12423783110794, -8.15865186565657,
-8.12779523300791, -8.15498210353148, -8.15711005849511, -8.12876868592058,
-8.14498268712871, -8.12777905464012, -8.14658887045715, -8.14966988563538,
-8.15137416124342, -8.14757286777317, -8.15659830243711, -8.11739216850327,
-8.14816670318125, -8.15283383471808, -8.15503278645551, -8.12968355281506,
-8.11532096238244, -8.15388445232111, -8.12550097443724, -8.14214966153336,
-8.15406262640947, -8.15366204068896, -8.16073804747475, -8.14748077293754,
-8.10236112317726, -8.13306401111526, -8.15754008293152, -8.11496173845736,
-8.14744857712061, -8.13097980901942, -8.15712565747807, -8.16003438931358,
-8.16002796870351, -8.11892837135011, -8.13700008392334, -8.15721941772399,
-8.14819490909576, -8.1561399618782, -8.16012501716613, -8.11709369783742,
-8.13470092070733, -8.14629675, -8.15713679162397, -8.13686372838595,
-8.12430530737705, -8.15464372671311, -8.15669989585876, -8.16044796649062,
-8.15766701538992, -8.09362511111111, -8.13870000839233, -8.14998125100998,
-8.15243885077905, -8.12291705479452, -8.15384632745479), lat = c(33.6622974395803,
33.6600173368609, 33.6599819460086, 33.6598656189251, 33.6673908233704,
33.6593042234088, 33.6572338143965, 33.6580708565473, 33.6629485478539,
33.6547317504883, 33.6598712810572, 33.6567040290043, 33.6652851274788,
33.6596383685524, 33.6570077561196, 33.6611995549193, 33.661593106719,
33.6588793953069, 33.6614662478323, 33.6584457075095, 33.6641300278638,
33.6621415089752, 33.6598426484043, 33.6570205688477, 33.6727939605693,
33.6593126847291, 33.6591196082918, 33.6591313969, 33.6605346,
33.6553558936485, 33.6650662271625, 33.6653484273653, 33.6600826614748,
33.6642524699626, 33.6585006713867, 33.658733988916, 33.664975062454,
33.6610514512704, 33.6621421965042, 33.6687249002733, 33.6576841575676,
33.6600360803426, 33.6574947407709, 33.6584060573279, 33.6673802707071,
33.6550894507841, 33.6654066532252, 33.6595139325288, 33.6588793953069,
33.6568044356436, 33.6559013620991, 33.6568076288124, 33.6572189331055,
33.6670448613725, 33.6563744930416, 33.6598780530983, 33.6554991693199,
33.6602992307323, 33.6667773110577, 33.6591215807971, 33.6549378741848,
33.6592876394984, 33.6624833258972, 33.6556429238423, 33.6564236265265,
33.6680011078708, 33.6591718634038, 33.6684195434343, 33.6642363505652,
33.6599223753418, 33.6575357831156, 33.6607284545898, 33.6602992227631,
33.6646009116676, 33.6569601709497, 33.6597380618501, 33.6610439757783,
33.6709440919706, 33.6553216040898, 33.6567993164063, 33.6604019538554,
33.6604042053223, 33.6607238769535, 33.6704235076904, 33.6597095889516,
33.6546409606935, 33.658124, 33.6659726122235, 33.6550456763148,
33.6591343852459, 33.6648648385784, 33.6633987426758, 33.6695201510491,
33.6609040792869, 33.655895, 33.6606597900391, 33.6629373624763,
33.6612642187822, 33.6580512191781, 33.6635307311956), id = c(8,
14, 12, 12, 7, 12, 8, 12, 10, 12, 14, 12, 6, 14, 10, 12, 8, 4,
14, 14, 2, 10, 2, 12, 9, 5, 12, 12, 5, 10, 12, 14, 2, 14, 14,
14, 12, 12, 14, 8, 12, 10, 12, 14, 6, 12, 12, 14, 4, 3, 12, 10,
14, 8, 14, 14, 12, 10, 8, 14, 12, 3, 10, 12, 12, 12, 14, 6, 14,
9, 12, 10, 12, 14, 14, 14, 14, 10, 12, 12, 14, 12, 14, 14, 12,
12, 4, 8, 14, 2, 14, 14, 11, 10, 3, 12, 8, 14, 3, 12)), row.names = c("25101",
"15358", "80097", "89024", "27479", "98805", "25425", "86335",
"31333", "93483", "12171", "100849", "155418", "12853", "33858",
"100851", "22470", "149448", "7443", "12167", "144934", "33938",
"144132", "91062", "56909", "153781", "95039", "99533", "153760",
"32687", "86913", "12298", "144133", "7402", "11672", "13939",
"78548", "98801", "8135", "24867", "91818", "32609", "95688",
"11675", "155218", "94268", "90367", "7440", "149447", "145506",
"90571", "35105", "14210", "26177", "14975", "16723", "86359",
"34450", "26139", "14198", "89237", "145503", "31062", "92665",
"87694", "78666", "13917", "155219", "15350", "60377", "82820",
"33174", "87056", "7406", "15370", "15356", "9330", "33533",
"86726", "95709", "8131", "96538", "13911", "14229", "86539",
"93482", "145837", "22101", "11305", "144939", "7391", "7445",
"21817", "32804", "145314", "98223", "24175", "8132", "145504",
"87396"), class = "data.frame")
Have you tried evaluating the new values using the same basis used in your original data, then use the result as covariates? Note that you will get several warnings because your response is a proportion/probability and you have not given weights or anything like that. Assuming that the model is correctly specified, see if the code below does what you want.
It seems that you can extract the splines from your GAM using the "lpmatrix" type in predict.gam. See here:
How to extract fitted splines from a GAM (`mgcv::gam`)
I get a bit lost to be honest, so I would suggest just setting up the basis for your covariate without fitting a GAM.
Make sure the number and location of knots makes sense and set up a basis.
As I understand it, cSplinesDes() evaluates the basis functions at the desired points
spl_bs <- cSplineDes(x = df$cyclic_term,
knots = seq(min(df$cyclic_term),
max(df$cyclic_term),
length.out = 8),
ord = 4, derivs = 0)
Give reasonable names
colnames(spl_bs) <- paste0("cyclic.", 1:ncol(spl_bs))
You probably want to include these new columns in your data frame.
Note that I removed the Intercept term below but you might want to leave it in
df <- cbind(spl_bs, df)
Fit GEE with convenience code to specify model
model <- reformulate(c(-1,
paste0("cyclic.", 1:(ncol(spl_bs))),
"splines::bs(beta1)", "splines::bs(lon+lat)"),
response = "response")
fit1 <- geepack::geeglm(model,
family="binomial",
data=df,
id=as.factor(id),
corstr = "independence")
Now with the predictions. Create new data
predgrid <- data.frame(expand.grid(lat=seq(min(df$lat-0.1), max(df$lat+0.1), length.out = 20),
lon=seq(min(df$lon-0.1), max(df$lon+0.1), length.out = 20),
cyclic_term=seq(min(df$cyclic_term),
max(df$cyclic_term), length.out=20),
beta1=25))
Set the same basis you used for the original data, but now evaluate at the new points
new_spline <- cSplineDes(predgrid$cyclic_term,
knots = seq(min(df$cyclic_term),
max(df$cyclic_term),
length.out = 8),
ord = 4, derivs=0)
colnames(new_spline) <- paste0("cyclic.", 1:ncol(new_spline))
Assemble new data and predict from fit1
newdf <- cbind(new_spline, predgrid)
predict(fit1, newdf)
Note that latitude/longitude spans out of range of original data, which is dangerous

Get value from other row in same group in r

I have the following dataset:
date_nba <- tibble::tribble(
~idGame, ~slugTeam, ~slugOpponent, ~drebTeam, ~orebTeam,
20900001, "NOP", "TOR", 37, 16,
20900001, "TOR", "NOP", 41, 16,
20900002, "LAL", "LAC", 32, 9,
20900002, "LAC", "LAL", 34, 11
)
I want to create a column called drebOpp, which is the drebTeam from the other slugTeam in the same idGame. So the desired result would be:
tibble::tribble(
~idGame, ~slugTeam, ~slugOpponent, ~drebTeam, ~orebTeam, ~drebOpp,
20900001, "NOP", "TOR", 37, 16, 41,
20900001, "TOR", "NOP", 41, 16, 37,
20900002, "LAL", "LAC", 32, 9, 34,
20900002, "LAC", "LAL", 34, 11, 32
)
I know there's probably an easy solution using group_by and mutate, but I can't seem to find it anywhere. Any help would be appreciated!
We can do a group by match
library(dplyr)
date_nba %>%
group_by(idGame) %>%
mutate(drebOpp = drebTeam[match(slugTeam, slugOpponent)])

Variable lengths differ error when using lm in R

I am trying to run a linear model on time and a monthly factor, however I am getting the error:
Error in model.frame.default(formula = ts.data ~ time2 + factor(month2), :
variable lengths differ (found for 'time2')
This is how I created the variables:
time2<-seq(along=ts.data)
month2<-rep(1:12,length=length(ts.data))
However running length(time2), length(month2) and length(ts.data) gives the same number, does anyone know how I fix the error?
Trying to run lm :
lm(ts.data~time2+factor(month2))
The data I am using:
structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 78238, 73928, 70708, 75175, 70744, 65604, 61227, 62635,
47652, 51507, 81874, 98236, 99401, 94756, 94697, 93732, 100334,
139355, 88575, 94169, 86084, 98249, 95321, 87822, 80256, 81875,
86293, 80712, 79533, 82847, 84498, 84185, 78382, 82701, 80491,
91140, 86847, 96727, 101295, 99450, 87783, 101246, 97913, 100081,
96346, 93608, 90648, 99105, 90920, 84960, 82591, 88090, 89980,
87778, 87429, 81898, 77285, 80369, 73193, 65139, 60126, 57219,
94204, 112472, 157199, 154791.5, 154294.88, 161920.63, 147408.75,
134418, 132158.5, 104572.5, 96831, 91045.88, 141182.63, 214759.25,
216647, 184598.38, 210794.38, 182403.75, 193001.63, 176807.38,
186552.63, 201375.88, 181861.25, 193234.88, 187240.25, 168242,
172475.13, 188996.25, 179663.88, 192861.63, 187461.25, 188670.5,
198826.25, 208696.5, 180490.75, 202265.88, 187966.13, 203342.13,
194850.38, 230582.63, 212517, 223432.5, 196511.63, 229582.25,
206120.63, 225629.88, 209769.63, 210797.63, 213215.75, 215144.88,
223266, 230747.63, 228573, 223828.88, 202102.88, 192863.63, 206675.13,
195647.5, 173897.25, 183788.88, 158511.38, 138559, 114163.25,
110399.13, 164751.13, 270772, 90430, 81719, 79183, 85428, 79372,
72361, 66207, 55403, 51693, 60280, 98698, 123059, 121550, 107662,
107863, 107630, 114685, 169659, 100104, 107598, 97728, 112850,
107784, 97580, 92709, 99098, 99482, 100543, 98856, 106081, 108248,
104769, 96966, 100093, 103107, 114944, 108001, 126289, 135213,
129717, 121688, 134421, 127318, 127412, 121922, 119045, 116989,
126286, 116707, 106627, 98219, 111225, 117279, 113725, 114633,
100633, 95478, 98394, 87616, 75329, 68274, 70658, 122995, 145224,
155833, 131896.5, 138340.63, 145610, 130653.13, 122562.75, 115850.5,
91749.88, 81787.13, 85457.5, 142931.63, 214970, 216836.63, 175902,
180757.88, 175233.63, 168982.13, 168727.25, 173501, 182731.38,
152260.63, 182607, 179326.5, 157693.13, 161004.75, 172990.5,
166204.38, 175172.63, 186446.13, 202645.38, 202500.25, 204148.38,
187763.5, 207269.75, 183334.88, 206552.5, 207270.13, 226123.88,
239037.88, 214656.38, 216552.75, 231406.75, 207365.63, 217873.5,
200308.88, 201696.5, 208984.75, 227723.38, 212083.25, 206262.38,
186596.25, 215496.63, 199399, 184933.25, 195925.63, 190318.63,
170375.38, 171624.13, 154537.13, 133532.25, 119179.13, 113297.88,
174946.5, 304690, 108567, 99358, 97299, 103628, 96936, 89254,
83761, 72058, 66685, 74491, 117292, 139878, 139585, 130180, 130079,
127562, 136152, 197149, 118619, 127875, 118094, 134989, 130688,
121475, 112367, 114805, 117087, 118526, 118038, 123988, 127511,
125790, 116702, 123049, 124260, 141232, 133809, 156349, 162637,
158367, 144491, 164389, 155305, 161401, 151829, 144188, 142702,
156405, 141937, 129857, 120318, 132823, 138201, 135058, 129275,
119897, 112924, 120385, 108134, 94062, 86695, 88434, 145426,
167100, 184196.63, 166628.38, 168193.13, 190280.88, 154984.75,
153784.38, 148033.75, 121304.25, 107303.25, 108003.13, 168770,
240983.75, 242817.13, 220229.38, 222805.75, 205068.75, 205204.25,
192598.25, 206565.38, 227284.88, 199258.25, 227122.88, 209076.13,
194855.5, 196357.25, 206865.88, 209580.13, 222190.88, 234610.88,
229339.13, 219321.63, 232571.75, 218584.75, 246116.38, 229563,
256776.75, 257335.25, 271507, 272014, 265850.5, 253426.63, 291759.63,
262608.88, 279417.25, 264583.25, 256634.88, 271024.88, 283927.13,
270597.38, 264222.5, 235009.13, 258379.25, 246485.5, 240163.25,
238369.88, 240961.5, 219826.75, 212077.5, 194937, 166299, 141284.88,
130153.38, 206775, 342062.88), .Dim = c(64L, 8L), .Dimnames = list(
NULL, c("Week_Number", "Campaign_Period", "Control_Traffic",
"Control_Revenue", "VOD_Test_Traffic", "VOD_Test_Revenue",
"TV_Test_Traffic", "TV_Test_revenue")), .Tsp = c(1, 2.21153846153846,
52), class = c("mts", "ts", "matrix"))
If we are creating grouping variables based on the number of rows, we need to change the 'month2' and 'time2'.
month2<-rep(1:12,length=nrow(ts.data))
time2<-seq_len(nrow(ts.data))
res <- lm(ts.data~time2 + factor(month2))
coef(res)
# Week_Number Campaign_Period Control_Traffic Control_Revenue VOD_Test_Traffic VOD_Test_Revenue TV_Test_Traffic TV_Test_revenue
#(Intercept) 0.0000000000000213162821 3.384444444 78799.578 157220.4207 87712.9656 148735.2930 106055.5914 177326.337
#time2 0.9999999999999994448885 -0.017777778 123.605 727.3023 383.8344 966.6405 465.7336 1229.196
#factor(month2)2 0.0000000000000152835379 0.017777778 -1177.438 2858.4910 -1320.1678 -8588.9322 -1128.5669 -6726.196
#factor(month2)3 0.0000000000000008207055 0.035555556 5419.457 10544.3136 6779.6644 346.1573 7382.8661 1173.044
#factor(month2)4 0.0000000000000016587917 0.053333333 8603.018 27254.5313 12531.1633 23542.1418 12777.1325 28186.515
#factor(month2)5 0.0000000000000012801265 -0.268888889 3290.677 6985.0394 7531.8356 3638.7607 7201.7339 5969.374
#factor(month2)6 0.0000000000000020182926 -0.251111111 12858.272 5428.8610 20021.4011 4320.5682 21940.0003 7326.704
#factor(month2)7 0.0000000000000016906446 -0.233333333 1297.067 6299.8347 3690.1667 327.4537 2400.8667 -451.516
#factor(month2)8 0.0000000000000016516546 -0.015555556 1838.662 6690.3563 -832.6678 -2303.4348 445.1331 3647.310
#factor(month2)9 0.0000000000000015682557 0.002222222 -5728.743 -12651.4220 -7622.1022 -22135.3253 -8178.0006 -15978.562
#factor(month2)10 0.0000000000000003302248 0.020000000 -1715.348 -5722.0704 -2630.9367 -11870.0938 -2470.1342 -9128.055
#factor(month2)11 0.0000000000000022008184 0.037777778 1179.647 -5052.7747 1691.6289 -8744.7323 2258.9322 -5674.003
#factor(month2)12 0.0000000000000033608693 0.055555556 5039.042 4606.1469 5908.5944 4559.9012 7788.3986 6991.025

Resources