I suspect you've attach()'ed `DF' multiple times in your code (possibly inside a loop, or perhaps a function that was called several times). Note that if it were a `package', it would show up in search() as `package:DF' rather than just `DF'. Also, R Core folks took care to avoid attaching the same package multiple times:
library(MASS) search()
[1] ".GlobalEnv" "package:MASS" "package:methods" "package:stats" [5] "package:graphics" "package:grDevices" "package:utils" "package:datasets" [9] "Autoloads" "package:base"
library(MASS) search()
[1] ".GlobalEnv" "package:MASS" "package:methods" "package:stats" [5] "package:graphics" "package:grDevices" "package:utils" "package:datasets" [9] "Autoloads" "package:base" Notice how trying to load a package that's already on the search path has no effect. This is not true for R objects, though. When you attach a data frame, say, `DF', (or a list), it places a _copy_ on the search path, so you can access the variables in the data frame (or components of the list) directly. When you make modifications to the variables (such as x[i] <- something, rather than DF$x[i] <- something), the modifications are applied to the _copy_ on the search path, not the original. HTH, Andy
From: Fernando Saldanha I have noticed that after I ran a batch script multiple times I get multiple copies of a package's name when I call search(). Is this a problem?
search()
[1] ".GlobalEnv" "DF" "DF"
[4] "DF" "DF" "DF"
multiple copies here ...
[13] "DF" "DF" "DF"
other packages here ...
[28] "package:quadprog" "package:car" "package:methods"
[31] "package:stats" "package:graphics" "package:grDevices"
[34] "package:utils" "package:datasets" "Autoloads"
[37] "package:base"
The following strange (to me) behavior that may be related. Suppose I
have a variable x that is in the global environment, and also there is
an 'x' in a dataframe called DF. Then I remove the variable x from the
Global Environment, with
remove('x', pos = 1)
At this point if I call remove again in the same way I get an error:
the variable x does not exist anymore. However, at this point I also
can check that DF$x exists. So far so good.
Further down in my code I have an assignment of the type
x[i] <- something (*)
which works fine, except that then if I look at x[i] and DF$x[i] they
are different. So it looks like x was recreated in the Global
Environment, which I actually can check by typing
.GlobalEnv$x.
On the other hand, if I put in my code something like
newvar[i] <- something
where newvar was never defined, then I get an error. I was hoping that
the statement (*)
above would assign to the variable DF$x. But it looks like (although
that is probably not the correct explanation) that the interpreter
somehow "remembers" that once there was a variable x in the global
environment and accepts the assignment to x[i], recreating that
variable.
Any insights on this?
Many thanks,
Fernando
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html