qu1ckie
qu1ckie

Reputation: 33

Strange filter behavior in dplyr

Consider the following dataset

Why do I get different results when doing:

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

Which results in:

  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))

Which gives:

  median      mad
1    671 152.7078

Upvotes: 3

Views: 378

Answers (1)

kristang
kristang

Reputation: 557

See comments above, as well as: 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)

Upvotes: 3

Related Questions