Dplyr::Select Function Clashes With Mass::Select

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.

  1. When issuing a library statement, R will list all conflicts. Pay attention to these! If you need to know the conflicts later enter: conflicts() .

  2. Use the exclude argument on library (R 3.6 and later). e.g. library(MASS, exclude = "select"). dplyr clobbers base lag and filter so you might want to get into the habit of routinely excluding those: library(dplyr, exclude = c("filter", "lag")) One can still access them using dplyr::lag, etc.

  3. Use dplyr::select notation if you find it has been masked.

  4. detach any packages you are no longer using, e.g. detach("package:MASS") .

  5. 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.

  6. 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



Leave a reply



Submit