Take the following two dataframes:
onedf<-data.frame(ID=c("a","b","c"),
Count=c(7,80,23))
anotherdf<-data.frame(Car_ID=c("b","c","d"),
Color=c("red","green","blue"))
In an effort to get a filtered version of onedf where IDs are only preserved if they don't appear in anotherdf, I mistakenly refer to anotherdr$ID in the code below:
filtereddf<-onedf%>%
filter(!(ID %in% anotherdf$ID))
Why is there no warning or error message produced, given the fact that no column named ID exists in anotherdf?
The result of running the above is simply filtereddf actually being an unfiltered version of onedf. I think, however, that the code should brick, and alert me that anotherdf$ID does not exist. If this happened, I would know to check my column names within anotherdf and would end up being able to correctly edit my code to
filtereddf<-onedf%>%
filter(!(ID %in% anotherdf$Car_ID))
ultimately resulting in the filtereddf being a single row,
In practice, this filter is one of many steps in a project, and so the fact that no filtering and no error message is delivered makes this challenging to diagnose as the culprit in the ultimate erroneous output.
Is there a way to achieve this filter in such a way that this misnamed column reference would spark an error, or perhaps a way to force filter() to validate the column name?
