I have a df which has two columns, time and val. The df is arranged by time. I want to filter out all the rows from the max value, in this case it is 1.29. I have provided example below:
library(tidyverse)
library(lubridate)
# This is the entire df
df1 <- tibble::tribble(
~date, ~val,
"2021-09-16 11:02:45", 1.21,
"2021-09-16 11:02:45", 1.21,
"2021-09-16 11:02:45", 1.21,
"2021-09-16 11:02:45", 1.22,
"2021-09-16 11:02:45", 1.22,
"2021-09-16 11:02:45", 1.22,
"2021-09-16 11:02:37", 1.22,
"2021-09-16 10:59:29", 1.29,
"2021-09-16 10:59:14", 1.29,
"2021-09-16 10:59:14", 1.28,
"2021-09-16 10:59:14", 1.28,
"2021-09-16 10:58:17", 1.28,
"2021-09-16 10:58:17", 1.28,
"2021-09-16 10:58:05", 1.26,
"2021-09-16 10:58:05", 1.26,
"2021-09-16 10:58:05", 1.23,
"2021-09-16 10:57:16", 1.23
) %>%
mutate(date = ymd_hms(date))
# This is the outcome I am looking for
tibble::tribble(
~date, ~val,
"2021-09-16 10:59:29", 1.29,
"2021-09-16 10:59:14", 1.29,
"2021-09-16 10:59:14", 1.28,
"2021-09-16 10:59:14", 1.28,
"2021-09-16 10:58:17", 1.28,
"2021-09-16 10:58:17", 1.28,
"2021-09-16 10:58:05", 1.26,
"2021-09-16 10:58:05", 1.26,
"2021-09-16 10:58:05", 1.23,
"2021-09-16 10:57:16", 1.23
) %>%
mutate(date = ymd_hms(date))
How to do this efficiently, any ideas?