library(sf)
library(mapview)
library(dplyr)
library(lubridate)
library(ggplot2)
library(lubridate)
## Load the loggers data
load("~/shiny/data_loggers/data_loggers2/data_loggers.rda")

## Select the important columns

d %>% select(Name='NAME',date_time='dt',Temp='TEMP',depth='DistBelowCanopy') -> d

## Load the loggers coordinates

loggers<-read.csv("~/shiny/data_loggers/data_loggers2/loggers.csv")

There is no tree identifier for the loggers!

We can make a rough and ready one by clustering the loggers on euclidean distance and cutting the resulting tree.

dmat<-dist(loggers[,2:3])
tl<-hclust(dmat)
plot(tl)

loggers$tree<-cutree(tl,h=0)
loggers %>% group_by(tree) %>% summarise(n=n()) %>% aqm::dt()
st_as_sf(loggers,coords=c("Lon","Lat"),crs=4326) %>% mapview()

One point has the wrong coordinates!

Check the data logger names correspond

unique(d$Name)
##  [1] 10A                   10b                   11A                  
##  [4] 12A                   12B                   13B                  
##  [7] 14A                   14B                   17A                  
## [10] 17B_0_                18A                   18B                  
## [13] 19A                   19B                   19C                  
## [16] 4A                    4B                    4C                   
## [19] 5A                    5B                    6A                   
## [22] 6B                    7A                    7B                   
## [25] 9A                    9B                    NMIDA                
## [28] NMIDB                 NMIDC                 NW2HillsA            
## [31] NW2HillsB             NW2HillsC             NW3Log_1_            
## [34] NW3LogB               NW3Log                NW3Log_B             
## [37] 11B                   EastMid2_             MidEast1_            
## [40] NE1_                  NE2_2017_             MidEasterly_A_2017_  
## [43] MidEasterly_B_2017_   MidEasterly_B_2017_0_ DL1_Mid_West_HIGH    
## [46] DL1_Mid_West_LOW      DL1_Mid_West_MID      DL11_Middle_HIGH     
## [49] DL11_Middle_LOW       DL11_Middle_MID       DL12_North_Mid_HIGH  
## [52] DL12_North_Mid_LOW    DL12_North_Mid_MID    DL14_South_West_LOW  
## [55] DL3_North_East_HIGH   DL3_North_East_LOW    DL4_Far_East_HIGH    
## [58] DL4_Far_East_MID      DL5_F_North_East_LOW  DL5_F_North_East_MID 
## [61] DL6_South_East_High   DL6_South_East_Low    DL7_South_east_HIGH  
## [64] DL7_South_east_Mid    DL8_Near_Camp_HIGH    DL8_Near_Camp_Low    
## [67] DL8_Near_Camp_MID    
## 67 Levels: 10A 10b 11A 11B 12A 12B 13B 14A 14B 17A 17B_0_ 18A 18B ... NW3LogB
unique(loggers$Name)
##  [1] DL1_HIGH     DL1_MID      DL1_LOW      DL3_HIGH     DL3_MID     
##  [6] DL3_LOW      DL4_HIGH     DL4_MID      DL4_LOW      DL5_HIGH    
## [11] DL5_MID      DL5_LOW      DL6_HIGH     DL6_LOW      DL7_HIGH    
## [16] DL7_LOW*     DL7_LOW      DL8_HIGH     DL8_MID      DL8_LOW     
## [21] DL11_HIGH    DL11_MID     DL11_LOW     DL12_HIGH    DL12_MID    
## [26] DL12_LOW     DL14_High    DL14_MID     DL14_LOW     11A         
## [31] 11B          12A          12B          NW3Log       NW3LogB     
## [36] NW2HillsA    NW2HillsB    NW2HillsC    10A          10B         
## [41] NW1FarHillsA NW1FarHillsB NW1FarHillsC 4A           4B          
## [46] 4C           5A           5B           6A           6B          
## [51] NMIDA        NMIDB        NMIDC        17A          17B         
## [56] 18A          18B          19A          19B          19C         
## [61] 14A          14B          13B          9A           9B          
## [66] 7A           7B          
## 67 Levels: 10A 10B 11A 11B 12A 12B 13B 14A 14B 17A 17B 18A 18B 19A ... NW3LogB
unique(loggers$Name) %in% unique(d$Name)
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
## [34]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE
## [45]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
## [56]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [67]  TRUE

Unfortunately the names do not match, so this will only work for the loggers with the same names in both data sets.

d<-merge(d,loggers)

Now to get the result you asked for group by tree and data stamp then take the maxima and minima.

d %>% group_by(tree,date_time) %>% summarise(n=n(),min=min(Temp), max=max(Temp), dif=max-min) %>% filter(n> 1) ->df

aqm::dt(df)