0

I would like to know how I can find the minimum and maximum day of year (DoY) based on water temperature (Wtemp) for each site (siteID).

Example Dataset:

df1 <- data.frame(matrix(ncol = 4, nrow = 20))
x <- c("siteID", "Date", "DoY", "Wtemp")
colnames(df1) <- x
df1$siteID <- c(101,101,101,101,101,
                102,102,102,102,102,
                103,103,103,103,103,
                104,104,104,104,104)
df1$Date <- rep(seq(from = as.Date("2020-01-01"), to = as.Date("2020-01-05"), by = 1),4)  
df1$DoY <- rep(seq(from = 1, to = 5, by = 1),4) 
df1$Wtemp <- c(10,2,6,12,15,
               20,15,5,10,16,
               2,4,6,8,10,
               12,14,16,18,20)

The output should look like this:

  siteID DoY_MaxWtemp DoY_MinWtemp
1    101            5            2
2    102            1            3
3    103            5            1
4    104            5            1
tassones
  • 891
  • 5
  • 18

1 Answers1

1

We can group by 'siteID', get the index of 'max' and 'min' value of 'Wtemp' with which.max and which.min respectively, use that to extract the corresponding values of 'DoY' in summarise

library(dplyr)
df1 %>%
   group_by(siteID) %>% 
   summarise(Doy_MaxWtemp = DoY[which.max(Wtemp)],
            Doy_MinWtemp = DoY[which.min(Wtemp)], .groups = 'drop')

-output

# A tibble: 4 x 3
#  siteID Doy_MaxWtemp Doy_MinWtemp
#*  <dbl>        <dbl>        <dbl>
#1    101            5            2
#2    102            1            3
#3    103            5            1
#4    104            5            1
akrun
  • 874,273
  • 37
  • 540
  • 662