简体   繁体   中英

Filtering data based on the conditional timestamp range in R

I have a dataset of 10min averages. 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. 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.

在此处输入图像描述

I have used dplyr's mutate function for PM_Condition and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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