Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find an inherited method for function ‘select’ for signature ‘"data.frame"’

Tags:

r

I'm trying to select columns from a data frame by the following code.

library(dplyr)
dv %>% select(LGA)
select(dv, LGA) 

Both of them will fail with the error

Unable to find an inherited method for function ‘select’ for signature ‘"data.frame"’

But the following code will be fine.

dplyr::select(dv, LGA)

Is this a function confliction in packages?

All libraries imported are as the following.

library(jsonlite)
library(geojsonio)
library(dplyr)
library(ggmap)
library(geojson)
library(leaflet)
library(mapview)
library(RColorBrewer)
library(scales)

I'm new to R, so super confused how you guys deal with problems like this?

like image 592
JsW Avatar asked Jun 02 '19 02:06

JsW


2 Answers

There's a great package that helps with package conflicts called conflicted.

If you type search() into your console, you'll see an ordered vector of packages called the "search list". When you call select, R searches through this "search path" and matches the first function called select. When you call dplyr::select you're calling it directly from the namespace dplyr, so the function works as expected.

Here's an example using conflicted. We'll load up raster and dplyr, which both have a select function.

library(dplyr)
library(raster)
library(conflicted)

d <- data.frame(a = 1:10, b = 1:10)

Now when we call select, we're prompted with the exact conflict:

> select(d, a)
Error: [conflicted] `select` found in 2 packages.
Either pick the one you want with `::` 
* raster::select
* dplyr::select
Or declare a preference with `conflict_prefer()`
* conflict_prefer("select", "raster")
* conflict_prefer("select", "dplyr")
like image 96
Rich Pauloo Avatar answered Nov 05 '22 09:11

Rich Pauloo


This function dplyr::select solved my problem.

like image 3
Javan Ochieng Okendo Avatar answered Nov 05 '22 07:11

Javan Ochieng Okendo