简体   繁体   English

根据 R 中的条件时间戳范围过滤数据

[英]Filtering data based on the conditional timestamp range in R

I have a dataset of 10min averages.我有一个 10 分钟平均值的数据集。 I trying to filter out the data based on a set of conditions such as when 24-hour-Rolling Average is greater than a certain number and another column (wind direction) is between a certain range for any 12 or more hours (cumulative) over the rolling 24-hour averaging period.我试图根据一组条件过滤掉数据,例如当 24 小时滚动平均值大于某个数字并且另一列(风向)在任何 12 小时或更长时间(累积)的某个范围之间时滚动的 24 小时平均周期。 As shown below, I want 'Reportable Condition' to show 1 when we have PM_Condition and Wind_Condition satisfied for 12 or more hours in last 24 hours.如下所示,当我们在过去 24 小时内满足 PM_Condition 和 Wind_Condition 12 小时或更长时间时,我希望“可报告条件”显示 1。

在此处输入图像描述

I have used dplyr's mutate function for PM_Condition and Wind_Condition.我已将 dplyr 的变异 function 用于 PM_Condition 和 Wind_Condition。 How can I filter data for Reportable conditon here?如何在此处过滤可报告条件的数据?

df <- data.frame(a = c(rep(0:1, each = 20), rep(1, 20), rep(0, 10)),
                 b = rep(1, 30), rep(0, 20))

rollSum <- function(x1, x2){
    N <- length(x1)
    rs <- c(rep(0, 23))    # The last 23 hours are zero
    
    for (i in 24:N){       # start summation from the 24th hour
        
        if (sum(x1[(i-23):i]) >= 12 & sum(x2[(i-23):i]) >= 12){
            rs <- c(rs, 1)   # If the sum of the column x1 and x2
        }else{               # are >= 12, then recorded as 1
            rs <- c(rs, 0)   # otherwise, recorded as zero
        }
    }
    return(rs)
}

df$Reportable_Condition <- rollSum(df$a, df$b)

> df[20:30, ]
   a b Reportable_Condition
20 1 1                    0
21 1 1                    0
22 1 1                    0
23 1 1                    0
24 1 1                    1
25 1 1                    1
26 1 1                    1
27 1 1                    1
28 1 1                    1
29 1 1                    1
30 1 1                    1

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

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