You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A friend and I have both encountered unexpected behavior from filter() and struggled to debug it until realizing that filter() was comparing a column against itself, instead of against a variable of the same name that had been defined elsewhere in the environment (for example, as a function argument).
library(dplyr)
# mtcars has 32 rows originally
nrow(mtcars)
# > [1] 32# should show that 11 cars have 4 cylindersmtcars|> filter(cyl==4) |> nrow()
# > [1] 11# should also show that 11 cars have 4 cylinderscyl<-4mtcars|> filter(cyl==cyl) |> nrow()
# > [1] 32
Possible solutions:
filter() warns when comparing a column against itself (which would return all rows), much like it warns when using a single = instead of double equal ==
filter() first looks to the environment for a defined variable of the same name as the column when a column is possibly compared against itself
The text was updated successfully, but these errors were encountered:
I think it would be tough to correctly and robustly detect these cases, so I don't think there is much we can do about this. I'm not sure cyl == cyl is really common enough to warrant an extra check for this
A friend and I have both encountered unexpected behavior from
filter()
and struggled to debug it until realizing thatfilter()
was comparing a column against itself, instead of against a variable of the same name that had been defined elsewhere in the environment (for example, as a function argument).Possible solutions:
filter()
warns when comparing a column against itself (which would return all rows), much like it warns when using a single=
instead of double equal==
filter()
first looks to the environment for a defined variable of the same name as the column when a column is possibly compared against itselfThe text was updated successfully, but these errors were encountered: