Can't filter on distinct rows using Dplyr and distinct(): no applicable method for 'distinct_' applied to an object of class "factor"

Viewed 5369

I am trying to filter distinct values of a column in a dataframe using dplyr, but I keep getting "Evaluation error: no applicable method for 'distinct_' applied to an object of class "factor"."

I've tried changing the class of the column I'm trying to filter on to character and factor, but both cause the error.

Example:

testFrame<-c("a","b","c","c"))
testFrame<-as.data.frame(testFrame)
testFrame %>% filter(distinct(testFrame, .keep_all=TRUE))
#Results in Error in filter_impl(.data, quo) :
#Evaluation error: no applicable method for 'distinct_' applied to an object of class "factor".

This should remove row four from my database, leaving me with just values of a,b,c

1 Answers

Figured out answer to my own question, posting it here for when I inevitably forget again:

You can't use filter with distinct, you only need to use distinct. Thus, code becomes:

testFrame<-c("a","b","c","c"))
testFrame<-as.data.frame(testFrame)
testFrame %>% distinct(testFrame, .keep_all=TRUE)

Then it returns the expected values of a,b,c

Related