如何在 r 中的特定 POSIXct 时间间隔内找到列数据帧的最大值?

how to find the maximum value of a column dataframe in a specific POSIXct time interval in r?

在我的数据框中 CORtrial 我有两列,rDate POSixct 格式来自 2015-07-27 17:45:00直到 2017-08-31 16:55:00REFN630 格式为 numericREFN630的值以5分钟5的时间间隔记录在rDate中。

这就是我的数据框的结构:

dput(head(CORtrial,10))
structure(list(rDate = structure(c(1438019100, 1438019400, 1438019700, 
1438020000, 1438020300, 1438020600, 1438020900, 1438021200, 1438021500, 
1438021800), class = c("POSIXct", "POSIXt"), tzone = ""), REFN630 = c(0.0111940298507463, 
0.0671936758893281, 0.0143198090692124, 0.0087719298245614, 0.00936768149882904, 
0.00985221674876847, 0.00775193798449612, 0.00815217391304348, 
0.00859598853868195, 0.00911854103343465)), row.names = c(NA, 
-10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000024f693c1ef0>)

我想知道特定日期和时间间隔之间的 max 值,例如,我想知道 REFN630 的最大值 rDate 2015-10-23 11:25:002015-10-23 11:40:00。我该怎么做?

2) 全程取最大值后:

df %>%
  filter(ymd_hms(rDate) %within% 
           interval(ymd_hms("2015-07-27 22:25:00"), ymd_hms("2015-07-27 22:50:00"))) %>%
  slice_max(order_by = REFN630)

                 rDate    REFN630
1: 2015-07-27 22:25:00 0.01431981

数据框中添加了更多列,结构如下:

 dput(head(COR_trial,10))
structure(list(rDate = structure(c(1438015500, 1438015800, 1438016100, 
1438016400, 1438016700, 1438017000, 1438017300, 1438017600, 1438017900, 
1438018200), class = c("POSIXct", "POSIXt"), tzone = ""), REFN532 = c(0.0127971, 
0.1348315, 0.0215983, 0.0143443, 0.0150862, 0.0158014, 0.0167866, 
0.0152284, 0.0162162, 0.0172911), REFN570 = c(0.0172414, 0.1515748, 
0.0171306, 0.0149573, 0.0157303, 0.0142518, 0.0150376, 0.016, 
0.0142045, 0.0151976), REFN630 = c(0.011194, 0.0671937, 0.0143198, 
0.0087719, 0.0093677, 0.0098522, 0.0077519, 0.0081522, 0.008596, 
0.0091185), REFN800 = c(0.0169082, 0.1030928, 0.0560472, 0.0569801, 
0.0574018, 0.0573248, 0.0531561, 0.0520833, 0.0510949, 0.0498084
)), row.names = c(NA, 10L), class = "data.frame")

现在,当我使用代码获取 REFN630 的最大值时,这就是我得到的:

COR_trial %>%
   filter(ymd_hms(rDate) %within% 
            interval(ymd_hms("2015-07-27 16:00:00"), ymd_hms("2015-07-27 18:00:00"))) %>%
   slice_max(order_by = REFN630)
                rDate   REFN532   REFN570   REFN630   REFN800
1 2015-07-27 17:50:00 0.1348315 0.1515748 0.0671937 0.1030928

所需的输出将是:

 rDate   REFN630 
1 2015-07-27 17:50:00 0.0671937

我该怎么做? 提前致谢。

我认为您可以使用以下解决方案。我需要更改间隔日期,因为您在问题中提到的日期不存在于数据集中,并且会在过滤后导致数据集为空。在这里,我们首先将 lubridate 包中的 interval 函数中的首选日期转换为 Date class。然后我们过滤我们的数据集,只过滤那些 rDate 落在这个区间内的数据。

library(dplyr)
library(lubridate)

df %>%
  filter(ymd_hms(rDate) %within% 
           interval(ymd_hms("2015-07-27 22:25:00"), ymd_hms("2015-07-27 22:50:00"))) %>%
  summarise(Maximum = max(REFN630, na.rm = TRUE))

     Maximum
1 0.01431981

为了只有包含最大值的列的名称:

library(tidyr)

df %>%
  filter(ymd_hms(rDate) %within% 
           interval(ymd_hms("2015-07-27 21:15:00"), ymd_hms("2015-07-27 21:30:00"))) %>%
  select(rDate, REFN630) %>%
  slice_max(order_by = REFN630)

                rDate   REFN630
1 2015-07-27 21:20:00 0.0671937

另一种选择是

library(dplyr)
df %>%
   mutate(rDate = as.POSIXct(rDate) %>%
   filter(between(rDate, as.POSIXct("2015-07-27 22:25:00"), as.POSIXct("2015-07-27 22:50:00")) %>%
   summarise(Max = max(REFN630))