Reputation: 47
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.
Upvotes: 2
Views: 18124
Reputation: 47
Thanks to larsoevlisen I understood that my data are as an matrix and can not be manipulated in that way so I had to transform them into an data.frame()
to filter the data I need out.
Final solution:
Matrix_5c_AVG_Year < cbind(AVG_SWE_YEAR,AVG_NO_YEAR[,2],AVG_FI_YEAR[,2],AVG_EE_YEAR[,2],AVG_LV_YEAR[,2],AVG_LT_YEAR[,2])
Matrix_5c_AVG_Year <-data.frame(Matrix_5c_AVG_Year)
colnames(Matrix_5c_AVG_Year) <- c("Year","AVG_SWE1", "AVG_NO1",
"AVG_FI1", "AVG_EE1", "AVG_LV1", "AVG_LT1")
Upvotes: 0
Reputation: 313
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/.
Upvotes: 1