【发布时间】:2021-11-15 11:20:21
【问题描述】:
我有一个 df,它有两列,time 和 val。 df 是按时间排列的。我想从最大值中过滤掉所有行,在本例中为1.29。我在下面提供了示例:
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))
如何有效地做到这一点,有什么想法吗?
【问题讨论】: