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 the Statistics Globe Newsletter

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


12 Comments. Leave new

  • Loading required package: zoo

    Attaching package: ‘zoo’

    The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

    Attaching package: ‘xts’

    The following objects are masked from ‘package:dplyr’:

    first, last
    this is appearing for all packages whatever package i load. can u tell me what should i do now?

    Reply
    • Hi Ayesha,

      This message tells you that a package contains a function with the same name as another package. For example, the zoo package contains a function called as.Date, and the basic installation of R contains this function as well.

      You can avoid problems in your code by explicitly specifying which package you want to use when calling a function. For example: zoo::as.Date

      Regards,
      Joachim

      Reply
  • I am new to R, how can I solve the below problem? Hope you can help me
    The following object is masked from package:base:

    T

    > base::T
    [1] TRUE
    > attach(refs)
    The following objects are masked from refs (pos = 3):

    B, T

    The following object is masked from package:base:

    T

    Reply
    • Hi Chimee,

      I apologize for the delayed reply. I was on a long vacation, so unfortunately I wasn’t able to get back to you earlier. Do you still need help with your syntax?

      Regards,
      Joachim

      Reply
  • Pouvez-vous m’aider à résoudre ce problème s’il vous plait ?

    Attaching package: ‘Rcmdr’

    The following object is masked from ‘package:base’:

    errorCondition

    Reply
  • 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
    how to fix this thing, THANKS IN ANTICIPATION

    Reply
  • Hi, Could you please help me?

    > library(WGCNA)
    Loading required package: fastcluster

    Attaching package: ‘fastcluster’

    The following object is masked from ‘package:stats’:

    hclust

    Attaching package: ‘WGCNA’

    The following object is masked from ‘package:stats’:

    cor

    Reply
    • Hello,

      I think you can handle the issue as suggested in this tutorial. If you want to use the cor function from the WGCNA package, you can simply call cor() after you’ve loaded the WGCNA package. If you want to use the original cor function from the stats package, you should call it using stats::cor() to ensure you’re accessing the correct version.

      Best,
      Cansu

      Reply

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.

Top