dplyr::select function clashes with MASS::select
As Pascal said, the following works
require(MASS)
require(dplyr)
mtcars %>%
dplyr::select(mpg)
`dplyr` not working with `MASS` package in `r`
Both packages have a select
function.
Use dplyr::select()
or MASS::select()
as needed to prevent errors.
Another popular conflict is the dplyr::filter
vs signal::filter
.
If you don't want to type the package name every time you can type once dselect <- dplyr::select
and then use dselect
all the time instead.
odd behavior with select in dplyr
Try ungroup()
summ%>%
ungroup() %>%
select(-cross_valid)
# park agent ha_dist
#1 slbe forest_pathogen 18.18
#2 slbe harvest_30_60 2.07
groups(summ)
#[[1]]
#park
#[[2]]
#cross_valid
dplyr mutate with within group select function
library(dplyr)
df %>%
group_by(city,year) %>%
mutate(group_affiliation = affiliation[victory==1])
What's the best way to work around function name conflicts in R?
Here are some things that can be done.
When issuing a
library
statement, R will list all conflicts. Pay attention to these! If you need to know the conflicts later enter:conflicts()
.Use the
exclude
argument onlibrary
(R 3.6 and later). e.g.library(MASS, exclude = "select")
. dplyr clobbers baselag
andfilter
so you might want to get into the habit of routinely excluding those:library(dplyr, exclude = c("filter", "lag"))
One can still access them usingdplyr::lag
, etc.Use
dplyr::select
notation if you find it has been masked.detach
any packages you are no longer using, e.g.detach("package:MASS")
.In some cases the masking is benign since the new version it is backwardly compatible and in those cases there is no need to avoid the conflict.
As noted by @LenGreski in the comments the package order will determine the resolution of any conflicts. Each package overrides all prior packages that were loaded before it in the case of conflicts.
There are also a number of packages that address the problem of conflicts including conflicted (mentioned by @MrFlick), modules, import and box which are all on CRAN.
Related Topics
Tools For Making Latex Tables in R
How to Number/Label Data-Table by Group-Number from Group_By
Splitting a Continuous Variable into Equal Sized Groups
Find How Many Times Duplicated Rows Repeat in R Data Frame
Plot Multiple Lines in One Graph
Format Numbers With Million (M) and Billion (B) Suffixes
Data.Table "Key Indices" or "Group Counter"
What Does the Dot Mean in R - Personal Preference, Naming Convention or More
Scatterplot With Marginal Histograms in Ggplot2
Multiple Plots in For Loop Ignoring Par
Basic Lag in R Vector/Dataframe
What Ways Are There to Edit a Function in R
How to Display Only Integer Values on an Axis Using Ggplot2
Remove Columns With Zero Values from a Dataframe