Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does R subset fail silently when I use single equals

Tags:

r

I made this rookie mistake in R

subset(mtcars, cyl = 4)

with single equals = instead of double equals ==

Why does it fail silently, i.e., return the unfiltered list, instead of breaking with an error?

like image 635
wrschneider Avatar asked Jan 30 '15 16:01

wrschneider


1 Answers

What's actually happening is that cyl = 4 is being treated as an additional named argument passed on to ... in subset.data.frame, and so there actually is no subset argument to filter with.

As for why it is programmed in such a way as to not throw an error in this case, you'd have to ask R Core for the real answer. But based on how R does it's function argument matching, it seems like it would be a hard thing to detect. Or at least, awkward.

For instance, what if a package implemented a new S3 method for subset that had an actual additional argument called cyl?

like image 133
joran Avatar answered Nov 14 '22 21:11

joran