R:计算最后一个时间框架的列的最小值



我有这个数据框架

df <- tibble(time = c(as.POSIXct("2012-01-01 00:00:00"), as.POSIXct("2012-01-02 00:00:00"), as.POSIXct("2012-01-04 00:00:00")),
value = c(0, 0.1, 0.2))
time                value
<dttm>              <dbl>
1 2012-01-01 00:00:00   0  
2 2012-01-02 00:00:00   0.1
3 2012-01-04 00:00:00   0.2

计算过去48小时内最小valuew的好方法是什么对于每一行。我的方法有效,但我相信还有更聪明的方法:

df <- tibble(time = c(as.POSIXct("2012-01-01 00:00:00"), as.POSIXct("2012-01-02 00:00:00"), as.POSIXct("2012-01-04 00:00:00")),
value = c(0, 0.1, 0.2)) %>%
mutate(min_v_48h = purrr::pmap_dbl(
.l = list(time),
.f = function(upper_bound) {
lower_bound <- upper_bound - (48 * 60 * 60)
valid <- .data[["value"]][.data[["time"]] >= lower_bound & .data[["time"]] < upper_bound]
ifelse(length(valid) > 0, min(valid), Inf)
})) 
time                value min_v_48h
<dttm>              <dbl>     <dbl>
1 2012-01-01 00:00:00   0       Inf  
2 2012-01-02 00:00:00   0.1       0  
3 2012-01-04 00:00:00   0.2       0.1

runner包在这方面非常出色,特别是在时间方面。

library(dplyr)
df <- tibble(time = c(as.POSIXct("2012-01-01 00:00:00"), as.POSIXct("2012-01-02 00:00:00"), as.POSIXct("2012-01-04 00:00:00")),
value = c(0, 0.1, 0.2))

library(runner)

df %>%
mutate(
min_v_48h = runner(
x= value,
idx = time,
k="2 days",
f = min,
lag = 1
)
)
#> Warning in f(.this_window, ...): no non-missing arguments to min; returning Inf
#> # A tibble: 3 × 3
#>   time                value min_v_48h
#>   <dttm>              <dbl>     <dbl>
#> 1 2012-01-01 00:00:00   0       Inf  
#> 2 2012-01-02 00:00:00   0.1       0  
#> 3 2012-01-04 00:00:00   0.2       0.1

由reprex包(v2.0.1)创建于2022-07-13

使用map2(也使用purrr)和lubridate如何:

library(tidyverse)
df |>
mutate(min_v_48h = map2_dbl(timex, timex-days(2), ~ min(value[timex >= .y & timex < .x])))

timex <= .x,如果您想在间隔中包含当前时间。

输出:

# A tibble: 3 × 3
timex               value min_v_48h
<dttm>              <dbl>     <dbl>
1 2012-01-01 00:00:00   0       Inf  
2 2012-01-02 00:00:00   0.1       0  
3 2012-01-04 00:00:00   0.2       0.1
Warning message:
Problem while computing `min_v_48h = map2_dbl(...)`.
ℹ no non-missing arguments to min; returning Inf 

。您可能需要添加验证步骤。