Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter ignores error when more than one condition is tested #1529

Closed
VitorAguiar opened this issue Nov 12, 2015 · 3 comments
Closed

filter ignores error when more than one condition is tested #1529

VitorAguiar opened this issue Nov 12, 2015 · 3 comments

Comments

@VitorAguiar
Copy link

@VitorAguiar VitorAguiar commented Nov 12, 2015

Is that a desired behaviour?

# correctly throws an error
> filter(iris, Sepal.Length = 3.5)
Error: incompatible expression in filter

# Ignores error in "Sepal.Length = 3.5" and returns data.frame satisfying the 2nd condition.
# I found this to be misleading:
> filter(iris, Sepal.Length = 3.5 & Sepal.Width > 2) %>% tbl_df()
Source: local data frame [149 x 5]

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          (dbl)       (dbl)        (dbl)       (dbl)  (fctr)
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
..          ...         ...          ...         ...     ...
@krlmlr
Copy link
Member

@krlmlr krlmlr commented Nov 12, 2015

Could be that this evaluates as

filter(iris, Sepal.Length = (3.5 & Sepal.Width > 2)) %>% tbl_df()

The difference is that the expression in the parentheses is a logical, while 3.5 (in your first example) isn't. The following works, too:

filter(iris, Sepal.Length = T) %>% tbl_df()

Maybe filter() should warn/stop if it encounters a named argument?

@metanoid
Copy link

@metanoid metanoid commented Dec 16, 2015

I think I have the same issue. I had a data frame like so:

df = data_frame(a = 1:7, b = rep("d",7))

and mistakenly filtered it like this:

df %>% filter(a = 3 & b == "d")
Source: local data frame [7 x 2]

      a     b
  (int) (chr)
1     1     d
2     2     d
3     3     d
4     4     d
5     5     d
6     6     d
7     7     d

I should have used df %>% filter(a == 3 & b == "d"). This gives my expected output.

I think krlmlr is right about it evaluating funny. I tried df %>% filter((a = 3) & (b == "d")) and received

Error: invalid (do_set) left-hand side to assignment

It would be helpful for me if filter always gave this error.

@hadley hadley added the feature label Mar 1, 2016
@hadley hadley added this to the 0.5 milestone Mar 1, 2016
@hadley
Copy link
Member

@hadley hadley commented Mar 1, 2016

Can check for this post NSE/SE update.

@hadley hadley closed this in 36d12a3 Mar 8, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants