简体   繁体   English

使用具有多个条件的 dplyr 滤波器

[英]Using dplyr filter with multiple conditions

I have the following data set.我有以下数据集。

There are three columns: Pentad, A, and B.共有三列:Pentad、A 和 B。

library(zoo)
library(rlang)
library(tidyverse) 

dat<-structure(list(Pentad = 50:73, A = c(152.796, 
109.678, 91.5594,115.155, 135.9, 202.441, 71.6951, 
88.3894, 261.962,135.853, 89.3425, 110.674, 100.558, 
173.507, 87.2157, 86.6425, 75.1852, 57.403, 62.5705, 
49.6846, 52.0257, 92.819, 105.419, 97.7598), 
B = c(145.402, 110.109, 83.1076, 95.3952, 148.571, 
119.178, 56.5031, 76.2635, 260.443, 109.705, 62.3749, 
100.322, 88.4134, 135.721, 63.1486, 69.7161, 62.3886, 
46.4513, 52.4546, 42.7725, 45.7643, 79.5419, 79.9434, 
87.6405)), class = "data.frame", row.names = c(NA, 
-24L))

I would like to implement the following condition in R.我想在 R 中实现以下条件。

[1] V1 should be between 0 and 90 at the time step (excluding 0 and 90)

[2] In the succeeding FOUR time steps (including the 
timestep in [1]), V1 between 0 and 90 in AT LEAST THREE timesteps

What I have so far:到目前为止我所拥有的:

 test2 <- function(dat, column_name){ 
   dat %>%
   rownames_to_column() %>%
   filter((.data[[column_name]] > 0 & .data[[column_name]] < 90) & 
         rollsum(.data[[column_name]] > 0 & .data[[column_name]] < 90, 4, fill = NA, align = 
                   "left") >= 3) %>%
   slice(1) -> result
  return(result)
}

out <- colnames(dat2) %>% 
  set_names %>% 
  map_dfr(~ test2(dat2, .x), .id = 'Col_ID')

PROBLEM:问题:

I want to get the timestep/pentad value where the above three conditions are true for both the 2nd (column A) and 3rd columns (column B).我想获得上述三个条件对于第二列(A 列)和第三列(B 列)都成立的时间步长/pentad 值。

That is, the timestep when both columns satisfy the conditions at the same time.即两列同时满足条件的时间步长。

The expected output is Pentad 64.预期的 output 是 Pentad 64。

Any idea how can I implement this in R?知道如何在 R 中实现这个吗?

I'll appreciate any help.我会很感激任何帮助。

Here's an attempt very close to OP's attempt.这是一个非常接近 OP 的尝试。

library(dplyr)
library(zoo)

test2 <- function(dat) {
   dat %>%
      filter_at(vars(A:B), all_vars(. > 0 & . < 90 & 
                     rollsum(. > 0 & . < 90, 4, fill = NA) >= 3)) %>%
       slice(1L)
}

test2(dat)

#  Pentad       A       B
#1     64 87.2157 63.1486

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

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