santoku
santoku

Reputation: 3427

what function/package conflicts with dplyr in R?

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?

Upvotes: 1

Views: 2560

Answers (2)

StatsStudent
StatsStudent

Reputation: 1594

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))

Upvotes: 0

Janna Maas
Janna Maas

Reputation: 1134

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.

Upvotes: 3

Related Questions