Why does a subset of R fail when I use single peers

I made this rookie mistake in R

subset(mtcars, cyl = 4) 

with single values = instead of double is ==

Why does he fail, i.e. returns an unfiltered list, and not broken with an error?

+7
r
source share
1 answer

What actually happens is that cyl = 4 treated as an extra argument with the name passed in ... to subset.data.frame , and therefore there really is no subset argument to filter with.

As for why it is programmed so as not to cause errors in this case, you will need to ask R Core for a real answer. But depending on how R performs the function of matching arguments, it seems to be difficult to detect. Or at least uncomfortable.

For example, what if a package implemented a new S3 method for a subset that had an actual optional argument named cyl ?

+8
source share

All Articles