简体   繁体   English

R连续数天计算

[英]R count consecutive days with condition

I have the next question. 我有下一个问题。 If I have the next dataframe 如果我有下一个数据帧

data<- data.frame(
        Time= c("2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05"),
        TEN= c(10,20,11,12,16)
    )

and I want to count the consecutive days that TEN < 15 as a new column using R . 我想把TEN <15的连续天数计算为使用R的新列。

I try with 我试着用

waves_min <- function(df, prop, min_value, min_days) {
    sum(with(rle(df$Temp > min_temp), values & lengths >= min_days))
}

but it returns a total number of that count, not a value for each row. 但它返回该计数的总数,而不是每行的值。

Any ideas? 有任何想法吗?

Thanks 谢谢

dplyr + data.table::rleid : dplyr + data.table::rleid

library(dplyr)
library(data.table)

data %>%
  group_by(ID = data.table::rleid(TEN < 15)) %>%
  mutate(Consec_Days = if_else(TEN < 15, row_number(), 0L))

Output: 输出:

# A tibble: 7 x 4
# Groups:   ID [5]
  Time         TEN    ID Consec_Days
  <fct>      <dbl> <int>       <int>
1 2018-01-01    10     1           1
2 2018-01-02    20     2           0
3 2018-01-03    11     3           1
4 2018-01-04    12     3           2
5 2018-01-05    16     4           0
6 2018-01-06    17     4           0
7 2018-01-07    14     5           1

data.table : data.table

setDT(data)
data[, Consec_Days := ifelse(TEN < 15, 1:.N, 0L), by = rleid(TEN < 15)]

Output: 输出:

         Time TEN Consec_Days
1: 2018-01-01  10           1
2: 2018-01-02  20           0
3: 2018-01-03  11           1
4: 2018-01-04  12           2
5: 2018-01-05  16           0
6: 2018-01-06  17           0
7: 2018-01-07  14           1

Base R + data.table::rleid : Base R + data.table::rleid

data$Consec_Days <- with(data, ave(TEN, data.table::rleid(TEN < 15), 
                                   FUN = function(x) ifelse(x < 15, seq_along(x), 0L)))

Output: 输出:

        Time TEN Consec_Days
1 2018-01-01  10           1
2 2018-01-02  20           0
3 2018-01-03  11           1
4 2018-01-04  12           2
5 2018-01-05  16           0
6 2018-01-06  17           0
7 2018-01-07  14           1

Data: 数据:

data <- data.frame(
  Time= c("2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05", "2018-01-06", "2018-01-07"),
  TEN= c(10,20,11,12,16,17,14)
)

I've added more rows to OP's sample data to illustrate that these solutions work for all cases. 我在OP的示例数据中添加了更多行,以说明这些解决方案适用于所有情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM