【问题标题】:Can't filter on distinct rows using Dplyr and distinct(): no applicable method for 'distinct_' applied to an object of class "factor"无法使用 Dplyr 和 distinct() 过滤不同的行:没有适用于“distinct_”的方法应用于“因子”类的对象
【发布时间】:2019-06-25 17:15:28
【问题描述】:

我正在尝试使用 dplyr 过滤数据框中列的不同值,但我不断收到“评估错误:没有适用于 'distinct_' 的方法应用于“因子”类的对象。”

我尝试将要过滤的列的类更改为字符和因子,但两者都会导致错误。

例子:

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".

这应该会从我的数据库中删除第 4 行,只留下 a、b、c 的值

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    想出了我自己问题的答案,在我不可避免地再次忘记时将其发布在这里:

    您不能将过滤器与 distinct 一起使用,您只需要使用 distinct。因此,代码变为:

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

    然后返回a,b,c的期望值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2015-08-18
      • 2017-12-01
      • 2023-03-13
      • 1970-01-01
      • 2022-09-24
      相关资源
      最近更新 更多