Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于dplyr中的两个条件筛选行_R_Filter_Dplyr - Fatal编程技术网

基于dplyr中的两个条件筛选行

基于dplyr中的两个条件筛选行,r,filter,dplyr,R,Filter,Dplyr,样本数据: y <- c(sort(sample(0:100, 365,replace = T)),sort(sample(0:100, 365,replace = T))) df <- data.frame(loc.id = rep(1:2,each = 365), day = rep(1:365,times = 2), y = y,ref.day = 250) 我想包括一个附加语句,即如果切片后day为ref.day,则选择day等于ref.day的行。 在这种情况下,它看起来

样本数据:

y <- c(sort(sample(0:100, 365,replace = T)),sort(sample(0:100, 365,replace = T)))
df <- data.frame(loc.id = rep(1:2,each = 365), day = rep(1:365,times = 2), y = y,ref.day = 250)
我想包括一个附加语句,即如果切片后
day
ref.day
,则选择day等于ref.day的行。 在这种情况下,它看起来像:

  # A tibble: 8 x 4
        loc.id   day    y ref.day
        <int> <int> <int>   <dbl>
    1      1    78    21     250
    2      1   154    41     250
    3      1   225    61     250
    4      1   288    81     250 # this row will not be selected. Instead row where day == 250 will be here instead 
    5      2    79    21     250
    6      2   147    41     250
    7      2   224    61     250
    8      2   300    81     250 # this row will not be selected. Instead row where day == 250 will be here instead
#一个tible:8 x 4
loc.id日期y参考日期
1      1    78    21     250
2      1   154    41     250
3      1   225    61     250
4 1 288 81 250#将不选择此行。相反,day==250的行将位于此处
5      2    79    21     250
6      2   147    41     250
7      2   224    61     250
8 2 300 81 250#将不选择此行。相反,day==250的行将位于此处

请使用
set.seed
以获得再现性我没有遵循为每个“loc.id”筛选最后一行的逻辑,因为在
切片中,您已经在使用表达式
which.max(y>80)
假设,如果没有与日期==250匹配的行。原因是
df%>%group\u by(loc.id)%%>%filter(any(y>20))%%>%slice(y==ref.day)#一个tible:0 x 4
。在我使用的示例中,“y”的最大值为100
  # A tibble: 8 x 4
        loc.id   day    y ref.day
        <int> <int> <int>   <dbl>
    1      1    78    21     250
    2      1   154    41     250
    3      1   225    61     250
    4      1   288    81     250 # this row will not be selected. Instead row where day == 250 will be here instead 
    5      2    79    21     250
    6      2   147    41     250
    7      2   224    61     250
    8      2   300    81     250 # this row will not be selected. Instead row where day == 250 will be here instead