基于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