In most cases R doesn't care about the order in which packages are loaded. There are some exceptions however, where we've to be careful about how we load packages. If two packages use the same function name, then the package loaded last will hide the function from earlier packages. This is called masking. When masking occurs, you have two options:
Option 1: You can either load the packages in a specific order to ensure that the functions you're interested in are not masked. We often see this when loading dplyr
package as the function select()
is also defined by other packages. The easiest way to avoid this problem is to make sure that dplyr
is loaded last.
library(Zelig)
library(dplyr)
NOTE: If you've loaded packages in the wrong order, then the easiest way to fix it is by restarting R and making sure you load them in the correct order the next time.
Option 2: You can also explicitly tell R that you're interested in a function from a specific package.
For example, in the case of a clash between the select()
function from dplyr
and from other packages, you can tell R that you're interested in using the function from dplyr
using the namespace syntax of double-colons: ::
.
dataset <- dplyr::select(dataset, name, age, occupation)
The line above forces R to call the select()
function from the dplyr
package, regardless of the order in which the packages were loaded.