Filter Reporting Error Based on Type

I'm trying to filter a column to remove values greater than zero.
The column is numeric:
[1] "numeric"
num [1:38177] 1899 1899 1910 1901 1899 ...

But when I execute filter, the system reports:
YearBuilt <- filter(Data$year_built > 0)
Error in UseMethod("filter") :
no applicable method for 'filter' applied to an object of class "logical"

Can someone explain?


YearBuilt <- filter(Data, year_built > 0)

Note that YearBuilt will be all the rows of Data where the year_built column is greater than 0.

Thank you. That worked. But why the error was reported as it was baffles me. It should have indicated that I failed to use the syntax that you used.

Actually, the error message makes sense if your brain has been twisted into R-thinking. I am not an expert, so I may be a little off on this explanation. When a function gets an object to work on, it looks at the class of the object to determine just what to do. You can see this with print() which does very different things depending on the kind of object. Printing a graphical object is different than printing a vector. So, under the hood there may be different filter functions, one for data frames, another for, say, lists. UseMethod() picks from among these possible functions, depending on the class of the object. You passed a logical vector, the output of Data$year_built > 0. The filter function does not know what to do with that and apparently does not have a default function to use on objects without a specialized function. Therefore it threw that error.
I hope that is more or less correct and clear.

Thank you. Actually that does make sense.

By the way, you can see the underlying functions, the "methods", behind a function with the methods() function. For filter I get this.

[1]* filter.tbl_lazy*   filter.ts*    

I get 356 entries for print.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.