Strange filter behavior in dplyr

Consider the following dataset

Why am I getting different results:

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

      

Result:

  median      mad
1    664 142.3296

      

and

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

      

What gives:

  median      mad
1    671 152.7078

      

+3


source to share


1 answer


See comments above and also: http://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html

filter()

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



...

filter()

works the same way subset()

, except that you can give it any number of filter conditions that are combined with &

(not &&

that easy to do by accident!). You can use other logical operators explicitly:filter(flights, month == 1 | month == 2)

+3


source







All Articles