1

dplyr used to work without issue, recently it seems to be in conflict with some other package I loaded. I didn't use plyr or MASS.

 mtcars%>%select(mpg)

Error in select(., mpg) : unused argument (mpg)

Why is it giving this error?

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
santoku
  • 3,297
  • 7
  • 48
  • 76
  • 2
    Try loading them in a different order -- dplyr last. Also try running `conficts()` and then for each conflict found `find("X")` where X is the name of a function that conflicts. – G. Grothendieck Apr 09 '17 at 15:11
  • 2
    `select` in `dplyr` conflicts with `select` in `MASS`. Even if you haven't loaded `MASS`, you might have loaded one of the many packages that depend on `MASS` (see "reverse depends" on the [CRAN MASS package page](https://cran.r-project.org/web/packages/MASS/index.html)) and that therefore load `MASS` functions into the global environment. – eipi10 Apr 09 '17 at 19:38
  • @eipi10 thanks, that could be why, does it solve the issue if i always load dplyr last? – santoku Apr 10 '17 at 03:30
  • I believe so... – eipi10 Apr 10 '17 at 04:24
  • `raster::select` – Scransom Jan 22 '20 at 03:09
  • 1
    and `raster::extract` – Michael Roswell Jun 15 '21 at 15:51

2 Answers2

3

SparkR also conflicts with select (object 'mpg' not found)

Instead of having to remember in which order to load the packages, you can also use

mtcars %>% dplyr::select(mpg)

when you run into these conflicts.

PKumar
  • 10,971
  • 6
  • 37
  • 52
Janna Maas
  • 1,124
  • 10
  • 15
0

I recently ran into a similar problem with an R script in Azure Databricks. To solve the issue I installed the R conflicted package, called the library and then ran my code. A message then appears in the console window which indicated that the dplyr mutate function was conflicting with the one from sparkR. To easily resolve this, you can tell R which package to default to with the conflict_prefer() function and passing it the arguments of the conflicting function name and the preferred/default package to use when a conflict arises. So for example:

Step 1. Install conflicted and attach it

install.packages("conflicted")
library(conflicted)

#Step 2. Run your code that might contain a conflict with packages you've already attached

(Note: I'm using R 4.1, which uses the new pipe operator |> instead of %>% in older versions:

cars |> mutate(log_dist=log(dist))

In my case, this produced the following output to my console/log:

Error:
! [conflicted] `mutate` found in 2 packages.
Either pick the one you want with `::` 
 * SparkR::mutate
 * dplyr::mutate
Or declare a preference with `conflict_prefer()`
 * conflict_prefer("mutate", "SparkR")
 * conflict_prefer("mutate", "dplyr")

#Step 3. Modify your code to tell R which package to use when function conflicts arise among packages

conflict_prefer("mutate", "dplyr")
cars |> mutate(log_dist=log(dist))

And the result is then as expected with no errors/warnigns:

   speed dist  log_dist
1      4    2 0.6931472
2      4   10 2.3025851
3      7    4 1.3862944
4      7   22 3.0910425
.
.
.

Of course, you can always explicitly tell R which library to use with:

cars |> dplyr::mutate(log_dist=log(dist))
StatsStudent
  • 1,384
  • 2
  • 10
  • 28