dplyr 中的奇怪过滤器行为

Strange filter behavior in dplyr

考虑以下 dataset

为什么我做的时候会得到不同的结果:

library(dplyr)
df %>% 
  filter(!(w >= 1 | lag(w >= 1, default = F))) %>% 
  filter(lag(t, default = T) != t) %>%
  summarise(median = median(r), mad = mad(r))

这导致:

  median      mad
1    664 142.3296

df %>% 
  filter(!(w >= 1 | lag(w >= 1, default = F)), 
         lag(t, default = T) != t) %>%
  summarise(median = median(r), mad = mad(r))

给出:

  median      mad
1    671 152.7078

查看上面的评论,以及:http://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html

filter() allows you to select a subset of the rows of a data frame.

...

filter() works similarly to subset() except that you can give it any number of filtering conditions which are joined together with & (not && which is easy to do accidentally!). You can use other boolean operators explicitly: filter(flights, month == 1 | month == 2)