Home > front end >  Calculating differences in daily averages between rows with similar strings, and across multiple duo
Calculating differences in daily averages between rows with similar strings, and across multiple duo

Time:10-07

I have a dataframe with daily averages of temperature values from June-August across different sites. The numbers across the top of my dataframe are the day of the month. Here is what my dataframe:

df = structure(list(month = c("Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug", 
"Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Aug"
), LoggerID_SiteCode = c("20SM_S", "20SM_U", "59SM_S", "59SM_U", 
"82SM_S", "82SM_U", "84SM_S", "84SM_U", "B3SM_S", "B3SM_U", "B4SM_S", 
"B4SM_U", "X21SM_S", "X21SM_U", "X22SM_AIR", "X22SM_S", "X22SM_U", 
"20SM_S", "20SM_U", "59SM_S", "59SM_U", "82SM_S", "82SM_U", "84SM_S", 
"84SM_U", "B3SM_S", "B3SM_U", "B4SM_S", "B4SM_U", "X21SM_S", 
"X21SM_U", "X22SM_AIR", "X22SM_S", "X22SM_U", "20SM_S", "20SM_U", 
"59SM_S", "59SM_U", "82SM_S", "82SM_U", "84SM_S", "84SM_U", "B3SM_S", 
"B3SM_U", "B4SM_S", "B4SM_U", "X21SM_S", "X21SM_U", "X22SM_AIR", 
"X22SM_S", "X22SM_U"), `1` = c(14.014140625, 14.419828125, 11.7819375, 
11.737921875, 11.826, 11.838078125, 13.09875, 13.042890625, 13.365453125, 
13.34446875, 13.39696875, 13.4661875, 13.218859375, 13.3031875, 
9.40565625, 13.36103125, 13.4303125, 20.5320833333333, 20.75221875, 
18.2314166666667, 18.1579791666667, 18.3542604166667, 18.3483229166667, 
18.9057395833333, 18.859125, 20.8006145833333, 20.7569375, 22.1831979166667, 
22.36015625, 21.299125, 21.3090729166667, 19.61834375, 21.63771875, 
21.8392916666667, 23.4016875, 23.7738541666667, 19.7501979166667, 
19.5498229166667, 20.5085, 20.5045104166667, 21.8800729166667, 
21.8591354166667, 23.8568229166667, 23.8276770833333, 24.49278125, 
24.78775, 24.1600104166667, 24.1821041666667, 20.1108020833333, 
24.6896770833333, 24.8989583333333), `2` = c(15.0379895833333, 
15.5674479166667, 12.1091458333333, 11.8856875, 12.1120520833333, 
12.14103125, 13.56009375, 13.5015416666667, 14.6758229166667, 
14.6559479166667, 14.6583541666667, 14.8266354166667, 14.37284375, 
14.5551458333333, 9.86863541666667, 14.47728125, 14.6123333333333, 
19.8243125, 19.9572604166667, 18.130625, 18.0434583333333, 18.1188125, 
18.09403125, 18.5594166666667, 18.536625, 20.2586979166667, 20.21109375, 
21.4797083333333, 21.5663541666667, 20.704375, 20.6984166666667, 
18.9615104166667, 21.1216458333333, 21.2320104166667, 23.0491666666667, 
23.36909375, 20.0028958333333, 19.82434375, 20.5851145833333, 
20.5712083333333, 21.8934166666667, 21.8636041666667, 23.4151979166667, 
23.3880729166667, 23.73575, 23.9203541666667, 23.5861458333333, 
23.5961458333333, 20.8876145833333, 24.1444166666667, 24.2785625
), `3` = c(16.7085520833333, 17.15690625, 13.4537708333333, 12.9555104166667, 
13.6354479166667, 13.6492916666667, 15.4358333333333, 15.381875, 
16.8948020833333, 16.8698958333333, 16.9444375, 17.0844270833333, 
16.7813958333333, 16.9045, 11.773875, 16.8727083333333, 16.987875, 
21.0365520833333, 21.2658541666667, 18.8788854166667, 18.7985208333333, 
18.68046875, 18.6665833333333, 19.40371875, 19.3521979166667, 
21.1712708333333, 21.1244791666667, 22.2876666666667, 22.4888854166667, 
21.5274166666667, 21.543375, 22.14909375, 21.9608125, 22.1917708333333, 
24.56521875, 24.9479583333333, 20.4829791666667, 20.0959166666667, 
21.1794791666667, 21.1575625, 22.9686458333333, 22.95171875, 
24.8283854166667, 24.7808020833333, 24.68990625, 24.9368958333333, 
24.6775833333333, 24.6926458333333, 22.5007083333333, 24.97534375, 
25.1817604166667), `4` = c(17.630875, 18.1708020833333, 14.0926875, 
13.3176458333333, 14.24859375, 14.2663958333333, 16.30465625, 
16.2518229166667, 17.8945208333333, 17.8735729166667, 17.9581041666667, 
18.13784375, 17.9125, 18.0653125, 11.4542083333333, 18.0862395833333, 
18.24521875, 21.3067604166667, 21.6376875, 18.6557395833333, 
18.4772395833333, 18.4514479166667, 18.4495, 19.5476041666667, 
19.50296875, 21.7196875, 21.68278125, 22.878875, 23.2011041666667, 
22.3003645833333, 22.3262395833333, 18.06384375, 22.67365625, 
22.9671041666667, 25.4092083333333, 25.97925, 20.20209375, 19.8153854166667, 
20.9608333333333, 20.95790625, 23.21959375, 23.1897291666667, 
26.072625, 26.0346666666667, 26.1280416666667, 26.6098125, 26.0494375, 
26.003875, 22.1682083333333, 26.44346875, 26.7681041666667), 
    `5` = c(16.04846875, 16.3166145833333, 12.8577708333333, 
    12.4570520833333, 13.5322291666667, 13.5101770833333, 15.3514375, 
    15.3046354166667, 16.2457708333333, 16.22190625, 16.7946458333333, 
    16.8492291666667, 16.5752708333333, 16.6090520833333, 10.2932604166667, 
    16.74690625, 16.824375, 20.94978125, 21.2985416666667, 17.2471354166667, 
    16.8189479166667, 17.9953125, 17.9873854166667, 19.08384375, 
    19.0293125, 21.0660625, 21.03021875, 22.3047395833333, 22.6389375, 
    21.64575, 21.6835520833333, 17.4655729166667, 22.1127291666667, 
    22.45809375, 26.1360833333333, 26.69846875, 20.2437083333333, 
    19.7219583333333, 21.79284375, 21.7978958333333, 24.2163333333333, 
    24.1863020833333, 26.5697916666667, 26.45721875, 26.9940208333333, 
    27.46003125, 26.7016145833333, 26.6310416666667, 22.5182395833333, 
    27.28078125, 27.5965520833333), `6` = c(16.5398541666667, 
    16.8853541666667, 13.4444479166667, 12.8398229166667, 13.7101458333333, 
    13.6970208333333, 15.35215625, 15.3093229166667, 16.8329895833333, 
    16.7981770833333, 16.5056041666667, 16.5899479166667, 16.7936145833333, 
    16.8461666666667, 12.7983333333333, 16.5292083333333, 16.6026041666667, 
    18.5676458333333, 18.6508541666667, 16.5402916666667, 16.3755208333333, 
    16.8681666666667, 16.8334375, 17.4154375, 17.3936041666667, 
    19.1138541666667, 19.0642604166667, 20.1411979166667, 20.1828020833333, 
    19.8347604166667, 19.8208333333333, 15.7474166666667, 20.1719375, 
    20.24740625, 26.0077916666667, 26.3147291666667, 21.99065625, 
    21.57615625, 22.4968125, 22.49384375, 24.5875729166667, 24.5694895833333, 
    27.1030416666667, 27.088625, 27.57275, 27.9571770833333, 
    27.33546875, 27.3478020833333, 23.022625, 27.91828125, 28.1843541666667
    ), `7` = c(18.19403125, 18.7980729166667, 14.0785416666667, 
    13.3501458333333, 14.0134791666667, 14.0383020833333, 16.3364270833333, 
    16.2763958333333, 18.1927395833333, 18.1867395833333, 18.5642916666667, 
    18.8020833333333, 18.4816770833333, 18.6464791666667, 14.31371875, 
    18.6230104166667, 18.8101041666667, 17.2801145833333, 17.4358229166667, 
    16.40975, 16.3371979166667, 16.12259375, 16.1254479166667, 
    16.11421875, 16.07740625, 17.8414895833333, 17.8037708333333, 
    19.246125, 19.391, 18.15296875, 18.1470208333333, 17.3362916666667, 
    18.63803125, 18.7778541666667, 27.18190625, 27.6144375, 22.06675, 
    21.5910625, 23.2363333333333, 23.2434583333333, 25.4481875, 
    25.4200833333333, 27.8525, 27.8330625, 28.2850208333333, 
    28.6942291666667, 28.4174583333333, 28.5022291666667, 24.8013125, 
    28.7242604166667, 29.0191041666667), `8` = c(16.9541458333333, 
    17.1764270833333, 13.9181770833333, 13.21303125, 14.3434791666667, 
    14.3155104166667, 15.7044895833333, 15.6627083333333, 17.59459375, 
    17.5757395833333, 18.5883229166667, 18.6844791666667, 18.0747916666667, 
    18.1164479166667, 13.5276354166667, 18.2958125, 18.4058645833333, 
    18.0746666666667, 18.24925, 16.9883125, 16.90890625, 16.59878125, 
    16.5928125, 16.87928125, 16.8355520833333, 18.5963645833333, 
    18.5596354166667, 20.4459375, 20.5553020833333, 19.2072604166667, 
    19.2082291666667, 17.38446875, 19.8295208333333, 19.9855729166667, 
    24.7151145833333, 24.9166979166667, 21.7891145833333, 21.1542604166667, 
    22.0695104166667, 22.0465416666667, 24.1481666666667, 24.120125, 
    25.6848958333333, 25.66365625, 26.0290520833333, 26.1601145833333, 
    26.4324479166667, 26.4221041666667, 20.9032916666667, 26.5353854166667, 
    26.6615833333333), `9` = c(17.5008854166667, 17.7916145833333, 
    14.541375, 14.13859375, 15.18275, 15.1569479166667, 16.0261145833333, 
    15.9883125, 17.48459375, 17.4508333333333, 17.9459166666667, 
    18.0044583333333, 17.4711666666667, 17.49, 15.2927083333333, 
    17.6496354166667, 17.6972291666667, 19.3896041666667, 19.62646875, 
    17.7327708333333, 17.6682395833333, 17.30140625, 17.2845416666667, 
    18.0489270833333, 18.0122291666667, 19.76678125, 19.7191666666667, 
    21.2829375, 21.4435625, 20.3473541666667, 20.3453541666667, 
    18.1576875, 20.6929583333333, 20.8771979166667, 21.527875, 
    21.68925, 19.7357708333333, 19.5703229166667, 20.10384375, 
    20.0770625, 20.9925625, 20.9746979166667, 22.3181979166667, 
    22.2813125, 22.90684375, 22.9739583333333, 22.8778229166667, 
    22.8648229166667, 18.9291770833333, 23.2147916666667, 23.2849583333333
    ), `10` = c(16.3477604166667, 16.4519583333333, 14.8937395833333, 
    14.8269791666667, 14.7589583333333, 14.7249583333333, 15.6324166666667, 
    15.59653125, 16.5496354166667, 16.5049375, 16.95740625, 16.9574270833333, 
    16.43740625, 16.4185104166667, 17.9693645833333, 16.3493333333333, 
    16.3513125, 19.5711041666667, 19.8679479166667, 16.52996875, 
    16.214625, 16.8446354166667, 16.8307291666667, 17.73484375, 
    17.69015625, 20.0106770833333, 19.95703125, 21.3473125, 21.58084375, 
    20.5223958333333, 20.5462604166667, 15.6216770833333, 20.9629270833333, 
    21.2400208333333, 21.29809375, 21.4913020833333, 19.0509166666667, 
    18.8389166666667, 19.4989270833333, 19.47315625, 19.6397604166667, 
    19.6169583333333, 21.5492916666667, 21.5194479166667, 22.3291458333333, 
    22.483125, 21.842875, 21.8398854166667, 17.8315729166667, 
    22.457625, 22.5644791666667), `11` = c(16.7248333333333, 
    16.8508333333333, 15.5882395833333, 15.5205625, 15.4738854166667, 
    15.4479895833333, 15.7519791666667, 15.7171041666667, 17.0024479166667, 
    16.9488541666667, 17.4800625, 17.47709375, 17.0354479166667, 
    17.0176145833333, 18.0849375, 17.08421875, 17.1477083333333, 
    20.1630729166667, 20.52096875, 16.20653125, 15.8445208333333, 
    17.1358541666667, 17.1307916666667, 18.3276458333333, 18.2779791666667, 
    20.5848645833333, 20.5421145833333, 21.7477916666667, 22.0418541666667, 
    21.04146875, 21.07128125, 16.5431770833333, 21.4669375, 21.809875, 
    21.5136458333333, 21.7330833333333, 19.0481354166667, 18.79434375, 
    19.40503125, 19.3931458333333, 19.66003125, 19.6322708333333, 
    21.8270416666667, 21.7922291666667, 22.5500520833333, 22.7165625, 
    22.1876458333333, 22.204625, 19.0415833333333, 22.5168854166667, 
    22.6268854166667), `12` = c(18.1904895833333, 18.38196875, 
    16.0962708333333, 16.0327291666667, 16.3976979166667, 16.4085208333333, 
    16.95665625, 16.9070104166667, 18.1280520833333, 18.09528125, 
    18.5308333333333, 18.5496666666667, 18.1103333333333, 18.1113541666667, 
    17.5500208333333, 18.14615625, 18.2373333333333, 21.2182291666667, 
    21.6248645833333, 17.1691458333333, 16.8180208333333, 17.9934166666667, 
    17.99140625, 19.4933020833333, 19.44271875, 21.7102083333333, 
    21.6773229166667, 22.4307604166667, 22.7797395833333, 22.0848333333333, 
    22.11971875, 18.0297708333333, 22.437375, 22.8291979166667, 
    21.0739791666667, 21.2061875, 18.937, 18.6645104166667, 19.2778958333333, 
    19.252125, 19.7606875, 19.7438333333333, 21.6236041666667, 
    21.5877395833333, 22.3784791666667, 22.4153854166667, 21.9948645833333, 
    21.97990625, 19.3479583333333, 22.2848125, 22.3306770833333
    ), `13` = c(18.3237083333333, 18.46540625, 16.1389166666667, 
    16.0704375, 16.6572395833333, 16.6294375, 17.2048333333333, 
    17.1721458333333, 18.3969895833333, 18.3593645833333, 18.9886875, 
    18.9995625, 18.268125, 18.25521875, 17.2908229166667, 18.4416354166667, 
    18.5318541666667, 22.7179895833333, 23.0826145833333, 18.7689375, 
    18.31178125, 19.6159375, 19.6149895833333, 20.9778020833333, 
    20.943, 23.0735, 23.0343333333333, 23.7204375, 24.0246458333333, 
    23.3204583333333, 23.3504895833333, 21.7418020833333, 23.6002291666667, 
    23.947875, 22.7009479166667, 22.9681145833333, 19.6342604166667, 
    19.329875, 19.6670208333333, 19.6471770833333, 21.0864270833333, 
    21.0556458333333, 22.863, 22.8169166666667, 24.08365625, 
    24.2920520833333, 23.2148229166667, 23.23890625, 21.0874479166667, 
    23.73946875, 23.8844791666667), `14` = c(16.7583125, 16.8723645833333, 
    14.8873541666667, 14.80965625, 15.48696875, 15.4591770833333, 
    15.5465729166667, 15.5177083333333, 16.8694791666667, 16.8129270833333, 
    16.98340625, 16.9893541666667, 16.4577083333333, 16.4467604166667, 
    13.4741979166667, 16.522125, 16.5638125, 22.9061354166667, 
    23.1827395833333, 18.89346875, 18.4951458333333, 19.68659375, 
    19.678625, 21.4454791666667, 21.4087083333333, 23.3487708333333, 
    23.3116041666667, 24.0662083333333, 24.3456979166667, 24.09065625, 
    24.1188229166667, 19.16625, 24.3637291666667, 24.6231145833333, 
    22.6349166666667, 22.8239166666667, 20.3904479166667, 20.1106979166667, 
    20.1186458333333, 20.0998125, 21.3152604166667, 21.2884375, 
    23.4103020833333, 23.38321875, 24.2470729166667, 24.33978125, 
    23.92878125, 23.9267604166667, 20.0247916666667, 24.36259375, 
    24.4744479166667), `15` = c(15.49221875, 15.6303958333333, 
    13.9025416666667, 13.8236458333333, 14.6860520833333, 14.6710208333333, 
    14.54278125, 14.5078125, 15.57234375, 15.5275520833333, 15.3614270833333, 
    15.3654270833333, 15.1777083333333, 15.16675, 14.3794479166667, 
    15.12721875, 15.1740208333333, 22.0527083333333, 22.35809375, 
    18.03825, 17.6706354166667, 18.86971875, 18.85684375, 20.7991875, 
    20.75453125, 22.6806145833333, 22.6526041666667, 23.3149583333333, 
    23.59559375, 22.7678541666667, 22.7918333333333, 18.28075, 
    23.4674166666667, 23.7676041666667, 23.0534791666667, 23.3631666666667, 
    19.9671458333333, 19.7400104166667, 19.8146458333333, 19.8027291666667, 
    20.6455416666667, 20.6187708333333, 23.73375, 23.7065520833333, 
    24.5267916666667, 24.8052083333333, 23.9112916666667, 23.9374375, 
    20.0153645833333, 24.4918125, 24.6946145833333), `16` = c(16.8619479166667, 
    17.0644895833333, 14.56459375, 14.50065625, 15.2865104166667, 
    15.30725, 15.5902916666667, 15.5353541666667, 16.6910104166667, 
    16.6451770833333, 16.66328125, 16.6840729166667, 16.3378541666667, 
    16.3358645833333, 16.6586354166667, 16.30859375, 16.4029166666667, 
    22.3619583333333, 22.7873020833333, 17.9289479166667, 17.5572604166667, 
    18.93146875, 18.9195520833333, 20.6039479166667, 20.55528125, 
    22.6367708333333, 22.59659375, 23.8094895833333, 24.2103333333333, 
    22.9721354166667, 23.0281666666667, 20.0556875, 23.43596875, 
    23.8037291666667, 21.8633854166667, 22.1501458333333, 19.0540208333333, 
    18.8865208333333, 19.01853125, 19.0056354166667, 19.95990625, 
    19.9351458333333, 22.5748541666667, 22.5398645833333, 23.0920833333333, 
    23.2444895833333, 23.0893645833333, 23.08934375, 17.9661458333333, 
    23.4003125, 23.579875), `17` = c(16.48434375, 16.6093958333333, 
    14.41875, 14.3509375, 15.231125, 15.2012291666667, 15.2959166666667, 
    15.2650104166667, 16.6746354166667, 16.6140729166667, 16.7683125, 
    16.7822291666667, 16.4616041666667, 16.4417708333333, 14.4401666666667, 
    16.4329270833333, 16.4894791666667, 22.78703125, 23.2278541666667, 
    18.30578125, 17.9153020833333, 19.39521875, 19.39825, 20.9721979166667, 
    20.9294791666667, 23.0128645833333, 22.98878125, 23.9042916666667, 
    24.3003333333333, 23.3107708333333, 23.3548333333333, 20.1161875, 
    23.8259375, 24.2167708333333, 20.463125, 20.6249479166667, 
    18.7368229166667, 18.4791458333333, 19.09646875, 19.0637916666667, 
    19.43846875, 19.4116666666667, 21.0241041666667, 20.9883645833333, 
    21.2965416666667, 21.34225, 21.2856979166667, 21.2657708333333, 
    18.4415520833333, 21.5730520833333, 21.6316979166667), `18` = c(18.0537604166667, 
    18.2960104166667, 15.6555208333333, 15.5898020833333, 16.4309375, 
    16.4616145833333, 16.3649375, 16.30728125, 18.0854375, 18.0407291666667, 
    18.44175, 18.4784270833333, 18.03465625, 18.0436145833333, 
    17.4123854166667, 17.9999895833333, 18.1805, 24.1290625, 
    24.5978333333333, 18.59934375, 18.1293020833333, 20.6048333333333, 
    20.60390625, 22.4125625, 22.3648125, 23.98584375, 23.9545416666667, 
    24.5128645833333, 24.9593229166667, 23.90534375, 23.9686770833333, 
    21.82903125, 24.53903125, 24.9296041666667, 21.5444895833333, 
    21.76309375, 18.9242291666667, 18.86278125, 19.43459375, 
    19.4048854166667, 20.0478854166667, 20.0230520833333, 21.5666770833333, 
    21.5478020833333, 20.6925625, 20.7661770833333, 21.3473125, 
    21.334375, 19.8066979166667, 21.5901979166667, 21.67890625
    ), `19` = c(19.1531354166667, 19.3844791666667, 16.8701354166667, 
    16.8036354166667, 17.3193854166667, 17.3202916666667, 17.9071979166667, 
    17.8576354166667, 19.2716979166667, 19.2400208333333, 19.6762604166667, 
    19.7189479166667, 19.27546875, 19.2804166666667, 16.3044375, 
    19.36475, 19.54634375, 22.82475, 23.0289791666667, 19.7190416666667, 
    19.4147916666667, 20.54528125, 20.52246875, 21.8786875, 21.85875, 
    23.1530833333333, 23.1240833333333, 23.4976354166667, 23.6331145833333, 
    23.5135, 23.5235416666667, 19.961625, 23.8045208333333, 23.9591041666667, 
    21.3097916666667, 21.5399583333333, 18.8568333333333, 18.8032916666667, 
    19.1985729166667, 19.1767916666667, 20.0330104166667, 20.01121875, 
    21.5963125, 21.5625104166667, 21.4332916666667, 21.5119895833333, 
    21.7194270833333, 21.7233958333333, 20.2504791666667, 21.87546875, 
    21.9832395833333), `20` = c(18.5477604166667, 18.7372291666667, 
    16.1148020833333, 16.04528125, 16.44153125, 16.423625, 17.02721875, 
    16.99153125, 18.7003854166667, 18.6458645833333, 19.6269791666667, 
    19.6836145833333, 18.85996875, 18.8619479166667, 15.0577604166667, 
    19.12675, 19.3440520833333, 22.9221770833333, 23.3399479166667, 
    19.6206458333333, 19.5343020833333, 19.7157083333333, 19.6997916666667, 
    21.3017916666667, 21.2640729166667, 22.7138854166667, 22.6858125, 
    23.5619270833333, 23.9034791666667, 22.9473958333333, 23.0063854166667, 
    21.1179166666667, 23.5755729166667, 23.8353541666667, 22.9734791666667, 
    23.3252604166667, 19.5473229166667, 19.3041354166667, 19.70571875, 
    19.6938541666667, 21.3981354166667, 21.3703020833333, 23.0352708333333, 
    23.0031770833333, 22.7355416666667, 22.8641145833333, 22.7898020833333, 
    22.8148333333333, 20.5598854166667, 23.2178541666667, 23.4215416666667
    ), `21` = c(16.88640625, 17.0191875, 15.1159270833333, 15.0391979166667, 
    15.333875, 15.2990625, 15.4552916666667, 15.4284479166667, 
    17.08878125, 17.0342604166667, 18.1383958333333, 18.1671041666667, 
    17.4318333333333, 17.4219166666667, 14.1825208333333, 17.6220729166667, 
    17.7291354166667, 24.1062604166667, 24.5747708333333, 19.4121666666667, 
    18.9464479166667, 20.2019895833333, 20.1911041666667, 21.9127291666667, 
    21.8769791666667, 23.9566354166667, 23.9295520833333, 24.95296875, 
    25.2990520833333, 24.0814479166667, 24.1486041666667, 21.1984166666667, 
    24.6519166666667, 24.94628125, 23.4728645833333, 23.8495833333333, 
    19.1455729166667, 18.8876354166667, 19.5444375, 19.5256041666667, 
    21.688, 21.6591770833333, 23.8927395833333, 23.8777708333333, 
    23.6350520833333, 23.7944791666667, 23.89090625, 23.92496875, 
    20.0387604166667, 24.2398333333333, 24.5055208333333), `22` = c(17.3017291666667, 
    17.5609895833333, 15.0479479166667, 14.9791770833333, 15.13196875, 
    15.1229166666667, 16.13140625, 16.08265625, 17.4560416666667, 
    17.39646875, 18.3434895833333, 18.4109895833333, 17.5692604166667, 
    17.5801666666667, 14.1947083333333, 17.7945104166667, 18.0415520833333, 
    25.4757708333333, 25.79140625, 20.5286875, 20.1079375, 21.75925, 
    21.7453229166667, 23.44975, 23.4226875, 26.05365625, 26.027125, 
    25.7063541666667, 25.8924791666667, 25.6671458333333, 25.6681770833333, 
    22.3603958333333, 26.1249166666667, 26.30115625, 24.4271770833333, 
    24.83734375, 20.366875, 20.0285520833333, 20.5650104166667, 
    20.5591145833333, 22.693875, 22.6579895833333, 24.77575, 
    24.7636875, 24.4291666666667, 24.5782291666667, 24.7388645833333, 
    24.7642395833333, 21.218375, 25.0976875, 25.3510104166667
    ), `23` = c(19.7261770833333, 20.0995520833333, 16.5360729166667, 
    16.4546354166667, 16.78453125, 16.7903958333333, 18.3991875, 
    18.3395729166667, 19.7710833333333, 19.73825, 20.5276145833333, 
    20.6133645833333, 20.102125, 20.1179479166667, 18.03465625, 
    20.2398333333333, 20.54559375, 26.05440625, 26.4927604166667, 
    20.14140625, 19.71059375, 22.0660625, 22.0651145833333, 23.26359375, 
    23.230625, 26.0631770833333, 26.0305520833333, 26.4956875, 
    26.8785, 26.1507083333333, 26.18515625, 23.5830520833333, 
    26.6151145833333, 26.9123645833333, 22.7216979166667, 22.8795208333333, 
    20.09978125, 19.8607291666667, 20.68521875, 20.6614375, 21.9554166666667, 
    21.9375208333333, 23.1829479166667, 23.1679583333333, 23.1878333333333, 
    23.2528229166667, 23.4020520833333, 23.3880208333333, 20.7529270833333, 
    23.7869791666667, 23.8913229166667), `24` = c(19.0966875, 
    19.2006979166667, 17.3595416666667, 17.2712708333333, 17.4543541666667, 
    17.4206458333333, 18.2463125, 18.21759375, 19.9166979166667, 
    19.8671041666667, 20.4413958333333, 20.4433854166667, 20.1129791666667, 
    20.0990833333333, 18.3761458333333, 20.284625, 20.3451458333333, 
    26.3504791666667, 26.7905520833333, 20.7500416666667, 20.2065208333333, 
    22.38453125, 22.38065625, 24.0930833333333, 24.0650833333333, 
    26.6649479166667, 26.6434791666667, 26.9527395833333, 27.3617708333333, 
    26.7376041666667, 26.7915625, 24.048875, 27.2904270833333, 
    27.5969791666667, 23.01528125, 23.1945208333333, 20.3846458333333, 
    20.3409375, 20.9861770833333, 20.9613541666667, 21.2917604166667, 
    21.2689375, 23.2413229166667, 23.2123645833333, 23.4761770833333, 
    23.5384583333333, 23.0750520833333, 23.0630625, 22.4216354166667, 
    23.6208020833333, 23.7443645833333), `25` = c(16.58490625, 
    16.6653020833333, 16.3813854166667, 16.3059375, 15.5130416666667, 
    15.46628125, 16.0734166666667, 16.0475625, 16.95590625, 16.9123020833333, 
    18.5436770833333, 18.54665625, 17.37734375, 17.3585, 17.5651770833333, 
    17.9640520833333, 17.9818958333333, 26.2148645833333, 26.6187916666667, 
    21.5103229166667, 20.9878645833333, 22.7868229166667, 22.7778645833333, 
    24.7739583333333, 24.7326979166667, 26.4462395833333, 26.419875, 
    26.68203125, 27.02140625, 26.8165104166667, 26.8461875, 22.59721875, 
    27.0725729166667, 27.339, 24.0131666666667, 24.2381458333333, 
    20.7889166666667, 20.5633854166667, 21.4130104166667, 21.3991145833333, 
    22.3509270833333, 22.3269895833333, 24.3582604166667, 24.32096875, 
    24.58940625, 24.6643854166667, 24.28759375, 24.2805520833333, 
    22.55628125, 24.4724375, 24.6039166666667), `26` = c(17.7763541666667, 
    17.8963645833333, 17.0935, 17.01809375, 16.529625, 16.5037604166667, 
    17.4280833333333, 17.4012916666667, 18.0073958333333, 17.96278125, 
    18.5289791666667, 18.5339375, 17.988625, 17.9777291666667, 
    18.8091041666667, 18.1838645833333, 18.2364166666667, 26.019, 
    26.3920625, 21.7126354166667, 21.2606979166667, 22.79403125, 
    22.78909375, 25.2501875, 25.2119375, 25.9246458333333, 25.8889375, 
    26.4631354166667, 26.7698645833333, 26.0702604166667, 26.0925208333333, 
    22.7341354166667, 26.5412916666667, 26.7733020833333, 22.9823645833333, 
    23.19796875, 19.7271875, 19.4723854166667, 20.3661875, 20.3513125, 
    21.2094583333333, 21.1856145833333, 23.6893229166667, 23.6601875, 
    23.91625, 24.0007916666667, 23.8436979166667, 23.8477291666667, 
    19.8111875, 24.1051145833333, 24.2966354166667), `27` = c(19.7576875, 
    19.9385208333333, 18.09365625, 18.02328125, 17.8102395833333, 
    17.818125, 19.1309583333333, 19.09425, 19.73559375, 19.6978645833333, 
    20.19540625, 20.2728854166667, 19.8479583333333, 19.8420208333333, 
    18.3665416666667, 20.0144270833333, 20.1058020833333, 24.7965208333333, 
    25.2972395833333, 20.2148333333333, 19.7670104166667, 21.2140833333333, 
    21.2200625, 23.7454895833333, 23.7075520833333, 25.0217291666667, 
    24.9706770833333, 25.83490625, 26.3001979166667, 25.3183854166667, 
    25.3685520833333, 20.49359375, 25.8625625, 26.2006458333333, 
    22.14365625, 22.3568229166667, 19.9589791666667, 19.66053125, 
    20.15159375, 20.1307291666667, 20.7270833333333, 20.7071875, 
    23.0307604166667, 22.99875, 23.18825, 23.2514895833333, 23.3037395833333, 
    23.29871875, 19.7014895833333, 23.6857291666667, 23.83259375
    ), `28` = c(19.58625, 19.69521875, 18.3256354166667, 18.2513541666667, 
    17.7827291666667, 17.7658958333333, 18.57353125, 18.5497604166667, 
    20.09790625, 20.0473333333333, 20.98703125, 21.0437604166667, 
    20.4292916666667, 20.4143854166667, 19.1754166666667, 20.7242916666667, 
    20.8157604166667, 24.6030208333333, 25.07025, 19.78984375, 
    19.4128958333333, 20.7389166666667, 20.72503125, 23.0073125, 
    22.97140625, 24.70871875, 24.6580208333333, 25.3213645833333, 
    25.6813020833333, 25.0481145833333, 25.0925833333333, 20.0586145833333, 
    25.42190625, 25.658875, 21.05825, 21.4172708333333, 17.6356041666667, 
    17.3976666666667, 17.8465625, 17.8286458333333, 19.2195208333333, 
    19.1947291666667, 21.6785, 21.6474583333333, 22.151625, 22.3501875, 
    21.8043229166667, 21.8133854166667, 16.5611458333333, 22.3426041666667, 
    22.5661666666667), `29` = c(19.5091354166667, 19.6789583333333, 
    18.6072708333333, 18.5408125, 17.9429479166667, 17.9548020833333, 
    18.3037916666667, 18.2601354166667, 20.09096875, 20.0432916666667, 
    21.5038229166667, 21.6345208333333, 20.4987604166667, 20.4997604166667, 
    20.2220416666667, 20.97875, 21.1370520833333, 22.25209375, 
    22.4305520833333, 19.176875, 18.9549791666667, 20.0205729166667, 
    19.99975, 21.1092291666667, 21.0953229166667, 22.8455520833333, 
    22.8186041666667, 22.98696875, 23.1280416666667, 23.1301145833333, 
    23.1180520833333, 18.5767708333333, 23.20121875, 23.32128125, 
    22.4616770833333, 22.833625, 18.5596354166667, 18.3623645833333, 
    18.9592291666667, 18.95621875, 20.42265625, 20.39884375, 
    23.0845520833333, 23.0603645833333, 22.9259479166667, 23.08203125, 
    22.94875, 22.95578125, 19.6460729166667, 23.3298958333333, 
    23.5436770833333), `30` = c(19.5531979166667, 19.7489166666667, 
    17.744625, 17.6662708333333, 17.5789270833333, 17.586875, 
    18.0717291666667, 18.0350208333333, 19.8433854166667, 19.8036875, 
    21.6261354166667, 21.7718333333333, 20.40978125, 20.40778125, 
    17.08371875, 20.95615625, 21.1033333333333, 21.6935, 21.8827708333333, 
    18.96378125, 18.7110416666667, 19.8715520833333, 19.8428020833333, 
    20.5939166666667, 20.5760416666667, 22.0307708333333, 21.9958854166667, 
    22.2183333333333, 22.3021770833333, 22.2649583333333, 22.26196875, 
    19.3437291666667, 22.5151770833333, 22.59696875, 23.3865208333333, 
    23.79390625, 19.4759895833333, 19.2646458333333, 19.8758854166667, 
    19.8769166666667, 21.4979895833333, 21.4711979166667, 23.8544583333333, 
    23.84128125, 23.7210833333333, 23.9321979166667, 23.7751979166667, 
    23.7960833333333, 20.9445833333333, 24.1608125, 24.42909375
    ), `31` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, 23.3514791666667, 23.87315625, 18.734125, 
    18.4752395833333, 19.7669270833333, 19.7639791666667, 21.6865, 
    21.6526979166667, 23.1953645833333, 23.1438125, 23.7164270833333, 
    24.17815625, 23.15909375, 23.2061145833333, 20.2959895833333, 
    23.82084375, 24.07909375, 23.8836666666667, 24.261125, 21.0007395833333, 
    20.8149895833333, 20.9360208333333, 20.94403125, 22.5955208333333, 
    22.5756145833333, 24.74965625, 24.7435520833333, 24.72128125, 
    24.911375, 24.7784375, 24.7934895833333, 23.06671875, 25.3203125, 
    25.5327291666667)), row.names = c(NA, -51L), class = c("tbl_df", 
"tbl", "data.frame"))

I want to calculate the difference between these averages for sites that match, and only differ between a "U" and "S". For example, what are the daily differences in averages between "20SM_U" and "20SM_S", and what are the differences in averages between "B3SM_U" and "B3SM_S".

I was using this code a while ago

Difference_Day = df %>%
  mutate_all( ~ replace(., is.nan(.), NA)) %>%
  mutate(site = genXtract(LoggerID_SiteCode, "_", "_")) %>%
  rowwise %>%
  mutate(site = first(site)) %>%
  ungroup() %>%
  select(-LoggerID_SiteCode) %>%
  pivot_longer(c(`1`:`31`), names_to = "day", values_to = "value") %>%
  group_by(month, site, day) %>%
  mutate(diff = c(abs(diff(value)))) %>%
  select(-value) %>%
  distinct(diff) %>%
  ungroup() %>%
  arrange(as.numeric(day)) %>%
  pivot_wider(names_from = "day", values_from = "diff") %>%
  arrange(month)

but now I'm getting the error message

Error in `mutate()`:
! Problem while computing `diff = c(abs(diff(value)))`.
x `diff` must be size 17 or 1, not 16.
i The error occurred in group 1: month = "Aug", site = NA, day = "1".

Any ideas of how to fix my code/use a different code to get the results I want

CodePudding user response:

The obvious answer here is to split Loggerid_SiteCode into separate ID and SiteCode columns. You can pivot to long format then group by month, day and logger ID, then find the difference between the two sites:

res <- df %>% 
  pivot_longer(`1`:`31`, names_to = "Day", values_to = "Values") %>%
  mutate(id = sub("_.*", "", LoggerID_SiteCode),
         SiteCode = sub(".*_", "", LoggerID_SiteCode),
         Day = as.numeric(Day)) %>%
  group_by(id, month, Day) %>%
  summarize(U_minus_S = Values[SiteCode == "U"] - Values[SiteCode == "S"])

res
#> # A tibble: 744 x 4
#> # Groups:   id, month [24]
#>    id    month   Day U_minus_S
#>    <chr> <chr> <dbl>     <dbl>
#>  1 20SM  Aug       1     0.372
#>  2 20SM  Aug       2     0.320
#>  3 20SM  Aug       3     0.383
#>  4 20SM  Aug       4     0.570
#>  5 20SM  Aug       5     0.562
#>  6 20SM  Aug       6     0.307
#>  7 20SM  Aug       7     0.433
#>  8 20SM  Aug       8     0.202
#>  9 20SM  Aug       9     0.161
#> 10 20SM  Aug      10     0.193
#> # ... with 734 more rows
#> # i Use `print(n = ...)` to see more rows

If you want it in wide format, it's trivial to pivot again:

res %>%
  pivot_wider(names_from = Day, values_from = U_minus_S)
#> # A tibble: 24 x 33
#> # Groups:   id, month [24]
#>    id    month      `1`     `2`     `3`      `4`      `5`      `6`      `7`
#>    <chr> <chr>    <dbl>   <dbl>   <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
#>  1 20SM  Aug    0.372    0.320   0.383   0.570    0.562    0.307    0.433  
#>  2 20SM  Jul    0.220    0.133   0.229   0.331    0.349    0.0832   0.156  
#>  3 20SM  Jun    0.406    0.529   0.448   0.540    0.268    0.346    0.604  
#>  4 59SM  Aug   -0.200   -0.179  -0.387  -0.387   -0.522   -0.415   -0.476  
#>  5 59SM  Jul   -0.0734  -0.0872 -0.0804 -0.178   -0.428   -0.165   -0.0726 
#>  6 59SM  Jun   -0.0440  -0.223  -0.498  -0.775   -0.401   -0.605   -0.728  
#>  7 82SM  Aug   -0.00399 -0.0139 -0.0219 -0.00293  0.00505 -0.00297  0.00713
#>  8 82SM  Jul   -0.00594 -0.0248 -0.0139 -0.00195 -0.00793 -0.0347   0.00285
#>  9 82SM  Jun    0.0121   0.0290  0.0138  0.0178  -0.0221  -0.0131   0.0248 
#> 10 84SM  Aug   -0.0209  -0.0298 -0.0169 -0.0299  -0.0300  -0.0181  -0.0281 
#> # ... with 14 more rows, and 24 more variables: `8` <dbl>, `9` <dbl>,
#> #   `10` <dbl>, `11` <dbl>, `12` <dbl>, `13` <dbl>, `14` <dbl>, `15` <dbl>,
#> #   `16` <dbl>, `17` <dbl>, `18` <dbl>, `19` <dbl>, `20` <dbl>, `21` <dbl>,
#> #   `22` <dbl>, `23` <dbl>, `24` <dbl>, `25` <dbl>, `26` <dbl>, `27` <dbl>,
#> #   `28` <dbl>, `29` <dbl>, `30` <dbl>, `31` <dbl>
  • Related