filter doesn't run - no applicable method for 'filter_' applied to an object of class "logical"

When I try to run the filter() from dplyr so as to select rows of my dataset according to the values in the dependent variables, it automatically runs the filter_() asking for a method:

Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "logical"

How can I get to run the filter and select only those rows that follow my condition.

From the error message it seems you are trying to apply the filter() function to an object other than a data frame, you might want to double check the class of the object you are working with.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Sorry, that's the dataframe here below. I need to select only those rows where the variable ind is 1 and exclude when it's 0.

> data.frame(
+          hab = c(901, 5064, 439, 692, 1163, 320),
+         vote = c(0, 52.64, 0, 0, 0, 81.18),
+          ind = c(0, 1, 0, 0, 0, 1)
+ )
   hab  vote ind
1  901  0.00   0
2 5064 52.64   1
3  439  0.00   0
4  692  0.00   0
5 1163  0.00   0
6  320 81.18   1

And this is the code and library I've been running and the error outcoming. I think it's due to a library mistake, running a filter()function different from the one I need.

> library (dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> filter(mydf$ind==1)
Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "logical"

Try

filter(df, ind == 1)

The first argument of filter is the data frame on which to operate. After that, you write the conditions to use.

It's still turning on an error message:

> filter(mydf, ind == 1)
Error in filter(mydf, ind == 1) : object 'ind' not found
1 Like

Have you run library(dplyr) before using the filter() function? If so, are you sure mydf contains a column named ind?

1 Like

This topic was automatically closed 7 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.