How to fix dplyr filter() Error in UseMethod("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.
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
Created on 2019-07-31 by the reprex package (v0.3.0)
Since you are new, I would recommend you to read chapter 1-16 of https://r4ds.had.co.nz/.
Related videos on Youtube
Ozo
Updated on June 04, 2022Comments
-
Ozo over 1 year
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.
Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "c('matrix', 'double', 'numeric')"
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)
I would like to get an output of only the row of 2010 data and perferably be able to select only one header.
-
larsoevlisen over 4 yearsIt is good practice to include a subset of your data or a representative sample of your data.
-
-
Ozo over 4 yearsThanks a lot for the suggestion but it still outputs the same error ""> mydata %>% filter(Year == 2010) Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "c('matrix', 'double', 'numeric')" is there any way how to get rid of it?
-
brettljausn over 4 yearsI'm guessing
cbind(AVG_SWE_YEAR,AVG_NO[,2],AVG_FI[,2],AVG_EE[,2],AVG_LV[,2],AVG_LT[,2])
returns a matrix and not a dataframe. maybe tryMatrix_5c_AVG_Year <- data.frame(AVG_SWE_YEAR,AVG_NO[,2],AVG_FI[,2],AVG_EE[,2],AVG_LV[,2],AVG_LT[,2])
instead -
Ozo over 4 yearsIt gives the same error. But I will try by exporting the matrix to excel and then importing it into a new r project and I hope that it will fix the issue.
-
larsoevlisen over 4 yearsIt is very hard to help you without understanding more about your data and setup. I have changed my answer to reflect, to the best of my ability and with the input from @brettljausn, what I believe is your problem and a solution. Also - what was your solution?
-
Ozo over 4 yearsThanks, you helped me a lot!