0

Sorry for that question against a convention. The single = in filter() is the most common error made by me and my students. One of my students asked me, what the single equal sign is needed for in filter() other than comparing. We both understand, that in other contexts the = is used to define a variable or an object. Her argument is, that in filter(), the error handler always say probably you forgot to write two == So why could filter() not accept both? The absolute correct == and in context of filter() unambiguous =?

I‘m asking for the argument. So this question is meant as a question and not a suggestion! :-)

jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • https://stackoverflow.com/questions/28176650/what-is-the-difference-between-and – Jon Spring Oct 18 '22 at 00:49
  • R exclusively uses `==` as the logical operator. It goes back a long time and I think would be inconsistent with the design philosophy/tradition of R or the tidyverse to make a change as substantial as that. – Jon Spring Oct 18 '22 at 00:55
  • `?filter` tells us that the second argument has to be "expressions that return a logical value" _i.e._ TRUE or FALSE. Only comparison operators do this, not assignment operators. A human might think that "=" is unambiguous in the context of `filter`, but the R language isn't designed that way. – neilfws Oct 18 '22 at 00:57
  • Ok, it‘s tradition and philosophy and logical. My students are more familiar with the double interpretation (assignment and logical) of the single equal sign from SPSS and Excel. I will tell them, that R is cleaner and a bit stricter by tradition. – Benjamin Fretwurst Oct 18 '22 at 01:16
  • Also, even if `=` were interpreted as an equality check by `filter()`, it could not be equivalent to `==` in this context. That's because `=` is actually passing a named argument to the function. If the left hand side is not a name but an expression (e.g. trying to do `max(x) = x`), that's a syntax error. – Mikko Marttila Oct 18 '22 at 08:33

1 Answers1

1

My thought is that dplyr::filter is requiring using “==“ to test a logical statement in order to properly filter, and that the code doesn’t work with an assignment operator (“=“ or “<-“ which are equivalent in R). Having multiple definitions for “=“ would cause too much confusion in R in my opinion.

mpianko
  • 38
  • 7
  • Good point! On the other hand, we use the + for sums and the pipe in ggplot. ;-) – Benjamin Fretwurst Oct 18 '22 at 01:50
  • 1
    That is a great example! In reading about this awhile back, I came across this question and post on the rstudio blog, check out Hadley’s reply on the ‘+’ and lack of the Magrittr pipe in ggplot : thought it was interesting and hope you do as well – mpianko Oct 18 '22 at 03:09