You may sometimes encounter the following message in R:
The following objects are masked from 'package:stats': filter, lag
This message appears when you load some package in R that contains functions that share names with functions that are already loaded from some other package in your current environment.
For example, suppose I load the dplyr package in R:
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
From the output we can observe:
1. The functions called filter and lag are masked from the R stats package.
- If we use filter() or lag() in our R code, the filter() and lag() functions from dplyr will be used since it was the package loaded most recently that contains these functions.
2. The functions called intersect, setdiff, setequal, and union are masked from the R base package.
- If we use intersect(), setdiff(), setequal(), or union() in our R code, these functions from dplyr will be used since it was the package loaded most recently that contains these functions.
How to Use Masked Functions
Suppose you would like to use the intersect() function from the base R package, but it is currently masked since there is an intersect() function that exists in the dplyr package we loaded more recently.
To explicitly use the intersect() function from base R, you can use the following syntax with double colons:
base::intersect(x, y)
In practice, you’ll most likely load several packages in your R environment at once.
To ensure that you’re using the function from some desired package, you can always type the package name with double colons in front of the function name.
Additional Resources
The following tutorials explain how to perform other common operations in R:
How to Interpret glm Output in R
How to Interpret ANOVA Results in R
How to Handle R Warning: glm.fit: algorithm did not converge