[R] object(s) are masked from package - what does it mean?

Marc Schwartz MSchwartz at mn.rr.com
Sun Oct 22 03:15:11 CEST 2006


On Sat, 2006-10-21 at 18:43 -0500, tom soyer wrote:
> Hi,
> 
> Sometime when I attach a dataset, R gives me the following
> message/warning:"The following object(s) are masked from package:datasets:
> column_name". Does anyone know what it means? Since it seems that the
> dataset was attached and I could manipulate the data from the dataset
> without problems, I am wondering what was R trying to tell me.
> 
> Thanks,
> 
> Tom

This has to do with R's searchpath.

When you attach() a dataset, it loads the dataset into R's current
searchpath. The same occurs with packages when you use library(). This
can be seen with:

> search()
 [1] ".GlobalEnv"        "package:Design"    "package:survival"
 [4] "package:splines"   "package:Hmisc"     "package:chron"
 [7] "package:xtable"    "package:gplots"    "package:gtools"
[10] "package:gmodels"   "package:gdata"     "package:methods"
[13] "package:stats"     "package:graphics"  "package:grDevices"
[16] "package:utils"     "package:datasets"  "Autoloads"
[19] "package:base"

or with a bit more detail:

> searchpaths()
 [1] ".GlobalEnv"
 [2] "/usr/local/lib/R/library/Design"
 [3] "/usr/local/lib/R/library/survival"
 [4] "/usr/local/lib/R/library/splines"
 [5] "/usr/local/lib/R/library/Hmisc"
 [6] "/usr/local/lib/R/library/chron"
 [7] "/usr/local/lib/R/library/xtable"
 [8] "/usr/local/lib/R/library/gplots"
 [9] "/usr/local/lib/R/library/gtools"
[10] "/usr/local/lib/R/library/gmodels"
[11] "/usr/local/lib/R/library/gdata"
[12] "/usr/local/lib/R/library/methods"
[13] "/usr/local/lib/R/library/stats"
[14] "/usr/local/lib/R/library/graphics"
[15] "/usr/local/lib/R/library/grDevices"
[16] "/usr/local/lib/R/library/utils"
[17] "/usr/local/lib/R/library/datasets"
[18] "Autoloads"
[19] "/usr/local/lib/R/library/base"


You can see that the 'datasets' package is number 17 in the searchpath.

For example, if I now attach() the iris dataset (a widely used example
dataset in R):

> attach(iris)
> search()
 [1] ".GlobalEnv"        "iris"              "package:Design"
 [4] "package:survival"  "package:splines"   "package:Hmisc"
 [7] "package:chron"     "package:xtable"    "package:gplots"
[10] "package:gtools"    "package:gmodels"   "package:gdata"
[13] "package:methods"   "package:stats"     "package:graphics"
[16] "package:grDevices" "package:utils"     "package:datasets"
[19] "Autoloads"         "package:base"


You can see that 'iris' is now number 2 in the searchpath. This means
that I can now access columns in the iris dataset without needing to use
the typical DataframeName$ColumnName syntax:

> Species
  [1] setosa     setosa     setosa     setosa     setosa     setosa
  [7] setosa     setosa     setosa     setosa     setosa     setosa
  ...

However:

> detach(iris)
> Species
Error: object "Species" not found
> iris$Species
  [1] setosa     setosa     setosa     setosa     setosa     setosa
  [7] setosa     setosa     setosa     setosa     setosa     setosa
  ...


In your case, the warning is telling you that you have attached a data
frame that presumably contains a column, whose name is 'column_name'. If
you use:

  > column_name

in your R session, the object with that name in your data frame will be
seen before another object with the same name that is lower in the
searchpath, since your object is higher in the searchpath. Thus, your
object is 'masking' the other.

I did not see another object called column_name in my version of the
datasets package, so perhaps you have done some manipulation on those
objects during your R session.

As a general comment, as has been discussed previously in other threads,
this is a potentially significant danger in the habit of using attach()
and should generally be avoided.

HTH,

Marc Schwartz



More information about the R-help mailing list