如何修复UseMethod("filter_")中的dplyr filter()错误 [英] How to fix dplyr filter() Error in UseMethod("filter_")

查看:182
本文介绍了如何修复UseMethod("filter_")中的dplyr filter()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从创建的数据矩阵中选择数据时,我收到一条错误消息,我希望有人可以帮助我并修复它.

When I try to select a data from the data matrix that I have created I receive an error, I would like that someone can help me out and fix it.

UseMethod("filter_")中的错误:'filter_'没有适用的方法应用于类"c('matrix','double','numeric')"的对象

Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "c('matrix', 'double', 'numeric')"

我尝试通过执行dplyr来调用该函数:或通过使用一些管道操作mydata%>%filter(2010)或什至已安装和加载的软件包冲突",并给dplyr一个优先级,但没有任何效果.我是r的新人.

I have tried to call out the function by doing dplyr:: or by using some pipe operations mydata %>% filter(2010) or even installed and loaded package "conflicted" and gave the dplyr an priority but nothing works. I am new with r.

Matrix_5c_AVG_Year  <- cbind(AVG_SWE_YEAR,AVG_NO[,2],AVG_FI[,2],AVG_EE[,2],AVG_LV[,2],AVG_LT[,2])
colnames(Matrix_5c_AVG_Year) <- c("Year","AVG_SWE1", "AVG_NO1", "AVG_FI1", "AVG_EE1", "AVG_LV1", "AVG_LT1") 
mydata<-Matrix_5c_AVG_Year  
mydata %>% filter(2010)

我只希望获得2010年数据的输出,并且只能选择一个标头.

I would like to get an output of only the row of 2010 data and perferably be able to select only one header.

推荐答案

正如@brettljausn所评论的那样,您需要将矩阵转换为data.frame.如果不添加要在其上比较条件值的列名,则在调用 filter 时也会出错.

As commented by @brettljausn, you need to convert your matrix to a data.frame. You will also get an error in the call to filter if you do not add the column name on which you want to compare your conditional value.

这应该可以说明您的问题和解决方案(由于使用了 filter ,因此继续tidyverse):

This should work illustrate your problem and a solution (continuing in tidyverse, since you are using filter):

library(tidyverse)


(a <- matrix(c(5,1), 2, 2))
#>      [,1] [,2]
#> [1,]    5    5
#> [2,]    1    1

colnames(a) <- c("Year", "AVG_SWE1")

a %>%
  filter(Year == 5)
#> Error in UseMethod("filter_"): no applicable method for 'filter_' applied to an object of class "c('matrix', 'double', 'numeric')"

(a2 <- as_tibble(a))
#> # A tibble: 2 x 2
#>    Year AVG_SWE1
#>   <dbl>    <dbl>
#> 1     5        5
#> 2     1        1

a2 %>%
  filter(Year == 5)
#> # A tibble: 1 x 2
#>    Year AVG_SWE1
#>   <dbl>    <dbl>
#> 1     5        5

reprex软件包(v0.3.0)于2019-07-31创建sup>

Created on 2019-07-31 by the reprex package (v0.3.0)

由于您是新手,所以建议您阅读 https://r4ds的第1-16章.had.co.nz/.

Since you are new, I would recommend you to read chapter 1-16 of https://r4ds.had.co.nz/.

这篇关于如何修复UseMethod("filter_")中的dplyr filter()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆