如何解决报错信息: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

 

 

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值