【问题标题】:count if by time interval in r如果按时间间隔计算 r
【发布时间】:2019-09-09 20:32:09
【问题描述】:

我正在尝试建立包含条件“T_mean 在 18-23 和 rh>=70% 之间”的天数的窗口,自指定时间向后(14 年 9 月 24 日,蓝色)间隔 5 天)

desirebly 列是 5day_int,我是手动计算的。 我的尝试包括dplyr::lag,但到目前为止没有成功...

任何帮助都会非常有帮助!

数据:

tibble::tribble(
        ~date, ~t_mean, ~rh,
  "15-sep-14",      20,  65,
  "16-sep-14",      19,  68,
  "17-sep-14",      21,  68,
  "18-sep-14",      20,  76,
  "19-sep-14",      18,  68,
  "20-sep-14",      19,  78,
  "21-sep-14",      17,  82,
  "22-sep-14",      20,  54,
  "23-sep-14",      21,  57,
  "24-sep-14",      20,  75
  )

【问题讨论】:

  • 我不太明白“自特定时间倒退以来的 5 天间隔”部分。

标签: r dplyr lubridate


【解决方案1】:

使用zoodplyr 一种可能的解决方案:

library(dplyr)
library(zoo)

df %>%
  mutate(condition = ifelse(rh >= 70 & between(t_mean, 18, 23), 1, 0),
         fiveday_int = rollapplyr(condition, width = 5, FUN = sum, partial = TRUE))

# A tibble: 10 x 5
   date      t_mean    rh condition fiveday_int
   <chr>      <dbl> <dbl>     <dbl>       <dbl>
 1 15-sep-14     20    65         0           0
 2 16-sep-14     19    68         0           0
 3 17-sep-14     21    68         0           0
 4 18-sep-14     20    76         1           1
 5 19-sep-14     18    68         0           1
 6 20-sep-14     19    78         1           2
 7 21-sep-14     17    82         0           2
 8 22-sep-14     20    54         0           2
 9 23-sep-14     21    57         0           1
10 24-sep-14     20    75         1           2

数据:

df <- tibble::tribble(
  ~date, ~t_mean, ~rh,
  "15-sep-14",      20,  65,
  "16-sep-14",      19,  68,
  "17-sep-14",      21,  68,
  "18-sep-14",      20,  76,
  "19-sep-14",      18,  68,
  "20-sep-14",      19,  78,
  "21-sep-14",      17,  82,
  "22-sep-14",      20,  54,
  "23-sep-14",      21,  57,
  "24-sep-14",      20,  75
)

【讨论】:

    猜你喜欢
    • 2016-08-01
    • 2015-11-06
    • 1970-01-01
    • 2023-01-22
    • 2014-02-18
    • 2015-06-15
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多