Warning in R: The following objects are masked from package (Example)

 

In this R programming post you’ll learn how to deal with the warning message “The following objects are masked from ‘package:X'”.

The article will consist of this information:

Let’s do this!

 

Example 1: Reproducing the Warning Message: The following objects are masked from ‘package:X’

In this example, I’ll illustrate how to replicate the message “The following objects are masked from ‘package:X’” in R.

For this example, we’ll the dplyr package. Let’s install and load the package:

install.packages("dplyr") # Install dplyr package
library("dplyr")          # Load dplyr package
# 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

As you can see, the RStudio console returned the warning message “The following objects are masked from ‘package:X’” twice. Once for the package stats and once for the package base.

The reason for this is that the dplyr package contains functions with the same name as the stats and base packages.

As you can see in the previous warning message, the filter and lag functions are contained in the stats and dplyr packages and the intersect, setdiff, setequal, and union functions are contained in the dplyr and base packages.

For that reason, R does not know which package to use when we apply one of these functions.

So how should we solve this problem? That’s what I’ll explain next!

 

Example 2: Handling the Warning Message: The following objects are masked from ‘package:X’

The R programming code below explains how to fix the issues with the warning message “The following objects are masked from ‘package:X'”.

Generally speaking: If functions with the same name are contained in different packages, you can specify which function of which package you want to use by writing the package name followed by :: in front of the function.

Let’s do an example!

For illustration, we’ll use the lag function that is contained in the dplyr and stats packages.

If we want to apply the lag function of the stats package, we can use the following R code…

stats::lag(1:5, 2)        # Applying lag function of stats package
# [1] 1 2 3 4 5
# attr(,"tsp")
# [1] -1  3  1

…and if we want to use the lag function of the dplyr package, we can use the following syntax:

dplyr::lag(1:5, 2)        # Applying lag function of dplyr package
# [1] NA NA  1  2  3

As you can see, the outputs of both functions are different. So it is very important to know which function of which package is used by the R programming language!

 

Video, Further Resources & Summary

Do you want to learn more about warnings in R? Then you may have a look at the following video that I have published on my YouTube channel. In the video, I’m explaining the content of this article:

 

The YouTube video will be added soon.

 

Besides the video, you might read some of the other articles on this website. A selection of tutorials is shown below.

 

To summarize: In this R tutorial you learned how to handle the message “The following objects are masked from ‘package:X'”. If you have further questions, let me know in the comments section.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top