I have a dataset that shows values for daily averages in Aug and Sept. There are 6 rows that occur in August and 6 in September. I need to compare the daily averages between "LoggerID_Sitecodes"
that have the same "SiteCode" (i.e. "X1WR"
, "X3WR"
, "14WR"
). for example I need to know what the daily difference was between "X1WR_S_Stationary"
and "X1WR_U_Compare"
in August, and again in September. Here the data frame I'm working with
Summary_Stats_ShieldComparisons = structure(list(month = c("Aug", "Aug", "Aug", "Aug", "Aug", "Aug", "Sep", "Sep", "Sep", "Sep", "Sep", "Sep"), LoggerID_SiteCode = c("X20676906_X1WR_S_Stationary", "X20817726_14WR_S_Stationary", "X20819740_X3WR_S_Stationary", "X21092860_X3WR_U_Compare", "X21092863_X1WR_U_Compare", "X21092877_14WR_U_Compare", "X20676906_X1WR_S_Stationary", "X20817726_14WR_S_Stationary", "X20819740_X3WR_S_Stationary", "X21092860_X3WR_U_Compare", "X21092863_X1WR_U_Compare", "X21092877_14WR_U_Compare"), `1` = c(19.7973541666667, 19.8387604166667, 20.18475, NaN, NaN, NaN, 20.7902604166667, 20.8141354166667, 21.2830104166667, 21.2929479166667, 20.8281875, 20.8101458333333 ), `2` = c(19.167875, 19.485, 19.0699166666667, NaN, NaN, NaN, 19.5951979166667, 19.5447395833333, 20.1386770833333, 20.1535416666667, 19.5912395833333, 19.52684375), `3` = c(19.1635833333333, 19.4361041666667, 18.6934895833333, NaN, NaN, NaN, 19.5257083333333, 19.1441145833333, 19.5713333333333, 19.59909375, 19.5366041666667, 19.1539791666667 ), `4` = c(20.58890625, 21.04803125, 20.2363854166667, NaN, NaN, NaN, 18.35540625, 18.3098854166667, 18.50115625, 18.4912708333333, 18.3722708333333, 18.313875), `5` = c(20.4786875, 21.2495, 20.0483333333333, NaN, NaN, NaN, 17.5555208333333, 17.3981666666667, 17.3645416666667, 17.34075, 17.57234375, 17.4060729166667), `6` = c(20.6277291666667, 21.5976875, 19.65246875, NaN, NaN, NaN, 17.1620833333333, 17.2158333333333, 17.0411875, 16.9986041666667, 17.1630520833333, 17.2089166666667 ), `7` = c(21.0440729166667, 22.0925729166667, 20.3653333333333, NaN, NaN, NaN, 18.9051145833333, 18.4130833333333, 18.1541875, 18.1224479166667, 18.9368854166667, 18.4041041666667), `8` = c(21.0748229166667, 22.6307708333333, 21.0419166666667, NaN, NaN, NaN, 18.93325, 18.6943020833333, 19.1433645833333, 19.0987708333333, 18.9830104166667, 18.6754791666667), `9` = c(21.2133541666667, 21.96090625, 20.8293333333333, NaN, NaN, NaN, 18.92021875, 19.3938541666667, 18.8428645833333, 18.82209375, 18.9281354166667, 19.39384375), `10` = c(21.0549583333333, 21.5886666666667, 21.0386875, 21.0903333333333, 21.07584375, 21.6045833333333, 19.9498854166667, 20.2733333333333, 18.5902604166667, 18.8200729166667, 19.9508958333333, 20.2773125), `11` = c(22.0971145833333, 22.28015625, 21.6225625, 21.6523645833333, 22.1451458333333, 22.3070625, 19.29984375, 19.7349166666667, 19.4305729166667, 19.5128333333333, 19.3048229166667, 19.7180520833333), `12` = c(22.6818854166667, 23.1176041666667, 22.5744375, 22.5664479166667, 22.7099166666667, 23.135625, 18.51884375, 18.83503125, 18.70721875, 18.7646979166667, 18.5287604166667, 18.8320625), `13` = c(23.9690625, 23.7686458333333, 24.0393958333333, 24.0303541666667, 24.0491770833333, 23.7916979166667, 18.8686354166667, 18.9042916666667, 18.8498229166667, 18.8508333333333, 18.8785520833333, 18.9013229166667), `14` = c(24.3618229166667, 24.29590625, 24.8081979166667, 24.7779375, 24.41753125, 24.3341666666667, 17.8340520833333, 17.648875, 18.26125, 18.24240625, 17.8538645833333, 17.6330104166667), `15` = c(25.377125, 24.76175, 25.80578125, 25.7824791666667, 25.4849166666667, 24.76578125, 16.7127291666667, 16.5705833333333, 17.227625, 17.1958854166667, 16.7285833333333, 16.5765208333333), `16` = c(23.89946875, 23.3866354166667, 25.5114895833333, 25.4689583333333, 24.0171145833333, 23.384625, 18.32178125, 18.0413645833333, 17.95309375, 17.9134479166667, 18.32771875, 18.0601875), `17` = c(23.0580416666667, 22.281625, 24.908625, 24.8368958333333, 23.1826041666667, 22.2736875, 18.36525, 17.6020625, 17.84609375, 17.8045, 18.40496875, 17.5951458333333 ), `18` = c(22.2622708333333, 21.6921875, 23.8456979166667, 23.7704791666667, 22.35740625, 21.7041145833333, 20.0287604166667, 19.6535729166667, 19.1127291666667, 19.0949270833333, 20.04965625, 19.66053125), `19` = c(21.6695416666667, 21.6125520833333, 22.6521354166667, 22.6231145833333, 21.7014166666667, 21.6076041666667, 19.3356666666667, 19.4366041666667, 19.649625, 19.60590625, 19.370375, 19.4296458333333 ), `20` = c(20.8732916666667, 21.1062604166667, 22.2158020833333, 22.1788958333333, 20.897125, 21.10825, 17.3488333333333, 17.1304583333333, 18.2068125, 18.1503020833333, 17.3944270833333, 17.1155729166667), `21` = c(21.9709166666667, 21.60440625, 23.2420416666667, 23.2148854166667, 22.0552291666667, 21.6133854166667, 16.7735104166667, 15.9922083333333, 17.9162708333333, 17.8567708333333, 16.8260729166667, 15.9743125), `22` = c(22.2590520833333, 21.9096041666667, 23.1308229166667, 23.0857708333333, 22.3375, 21.9086354166667, 17.0556875, 16.09653125, 17.7455625, 17.6989895833333, 17.11028125, 16.07759375), `23` = c(22.3858125, 21.9991770833333, 23.0202604166667, 22.9752395833333, 22.4510104166667, 22.0151770833333, 18.7368229166667, 17.5346458333333, 18.8792916666667, 18.841625, 18.7895104166667, 17.5237604166667), `24` = c(23.7411979166667, 23.0992604166667, 24.2068645833333, 24.1847708333333, 23.8049479166667, 23.11025, 18.79309375, 17.82234375, 19.183, 19.14334375, 18.8576979166667, 17.8034895833333), `25` = c(25.13925, 24.0122291666667, 25.8055104166667, 25.775125, 25.2316770833333, 24.0142291666667, 18.4624479166667, 18.1642395833333, 18.9538333333333, 18.8943645833333, 18.4753333333333, 18.15734375), `26` = c(25.3103645833333, 24.1949895833333, 26.4140208333333, 26.3784270833333, 25.4208854166667, 24.20096875, 18.8378854166667, 18.5516770833333, 18.3652083333333, 18.3018645833333, 18.8388958333333, 18.5368333333333), `27` = c(24.9939166666667, 24.2193541666667, 25.419875, 25.37846875, 25.0889375, 24.23046875, 18.4467083333333, 17.9333125, 18.7626770833333, 18.7200625, 18.4794375, 17.9254270833333)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))
Essentially, I want the end result to look like this table, but for the values to be the difference between sites. Can anyone help?
As mentioned above, it is easiest to change to long format to get the differences, then pivot back. Here, I first extract the relevant site code (e.g., X1WR
), so that we can group by the site code. Then, I pivot the data to long format, so that we can then get the difference for each group (i.e., by month
, site
, and day
). Then, I pivot it back to the long format. I used absolute value for the difference, but you can remove the abs
if you don't need it.
library(tidyverse)
library(qdap)
Summary_Stats_ShieldComparisons %>%
mutate_all( ~ replace(., is.nan(.), NA)) %>%
mutate(site = genXtract(LoggerID_SiteCode, "_", "_")) %>%
rowwise %>%
mutate(site = first(site)) %>%
ungroup() %>%
select(-LoggerID_SiteCode) %>%
pivot_longer(c(`1`:`27`), 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)
Output
# A tibble: 6 × 29
month site `1` `2` `3` `4` `5` `6` `7` `8` `9`
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Aug X1WR NA NA NA NA NA NA NA NA NA
2 Aug 14WR NA NA NA NA NA NA NA NA NA
3 Aug X3WR NA NA NA NA NA NA NA NA NA
4 Sep X1WR 0.0379 0.00396 0.0109 0.0169 0.0168 0.000969 0.0318 0.0498 0.00792
5 Sep 14WR 0.00399 0.0179 0.00986 0.00399 0.00791 0.00692 0.00898 0.0188 0.0000104
6 Sep X3WR 0.00994 0.0149 0.0278 0.00989 0.0238 0.0426 0.0317 0.0446 0.0208
# … with 18 more variables: 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>