I have a dataset with 450 observations and variables "date", "year", "site", "number". I want to select the observations with the highest number per site and year, and then select the numbers before and after the date on which that observation was taken.
df <- data.frame(
year = c(rep(2029, 10), rep(2020, 10), rep(2021, 10)),
date = c(seq(as.Date("2029-01-01"), as.Date("2029-01-10"), by = "day"),
seq(as.Date("2020-01-01"), as.Date("2020-01-10"), by = "day"),
seq(as.Date("2021-01-01"), as.Date("2021-01-10"), by = "day")),
site = rep(c("Site A", "Site B", "Site C"), each = 10, times = 3),
number = sample(1:100, 30, replace = TRUE))
I've gotten as far as select the highest number per site and year using dplyr::group_by(site,year) %>% slice_max(n=1, number) but I'm stuck after this.
Thanks!
You can write a function to get the index of the
maxvalue: