1. 程式人生 > 其它 >如何解決報錯資訊:Error in UseMethod(“filter_”) 在使用dplyr包的filter() 時候

如何解決報錯資訊:Error in UseMethod(“filter_”) 在使用dplyr包的filter() 時候

報錯資訊如下:

Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "character"
Calls: %>% ... freduce -> <Anonymous> -> filter -> filter.default -> filter_
In addition: Warning message:
`filter_()` is deprecated as of dplyr 0.7.0.
Please use `filter()` instead.
See vignette('programming') for more help
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated. 
Execution halted

最開始我以為這個method是我用錯了成filter_,

後面經過查詢指令碼和網上資料才知道,是因為輸入的檔案不是tibble、data.frame格式。

情況1: 忘記讀入檔案了,我是因為這個原因報錯

file <- args[1] 

情況2:當然還有可能是輸入的是matrix,這時候需要把matrix轉換成data.frame格式

例子如下

> a <- matrix(1:9, ncol =3)
> colnames(a) <- c("test1","test2","test3")
> a
     test1 test2 test3
[1,]     1     4     7
[2,]     2     5     8
[3,]     3     6     9
> filter(a, test1 == 2)

報錯資訊

轉換成為tibble/data.frame這個報錯就沒有了

> filter(as_tibble(a), test1 == 2)
# A tibble: 1 x 3
  test1 test2 test3
  <int> <int> <int>
1     2     5     8

參考ref:(感謝參考帖子)

https://stackoverflow.com/questions/57286838/how-to-fix-dplyr-filter-error-in-usemethodfilter