0

Code to reproduce the problem:

Creates a data frame with one column "a".

dataObj=data.frame(a=1) 
> dataObj
  a
1 1


Attach dataObj

attach(dataObj) 
> a
[1] 1


Modify dataObj, but the value of a is still unchanged.

dataObj[1,"a"]=3
> a
[1] 1
ChaoYang
  • 837
  • 2
  • 8
  • 14

1 Answers1

2

From ?attach:

The database is not actually attached. Rather, a new environment is created on
the search path and the elements of a list (including columns of a data frame)
or objects in a save file or an environment are copied into the new environment.

A copy is added to the search path, not the object itself. When you modify the original, the copy is unchanged.

It is advised (by many) to avoid attach. with is a handy substitute.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • 1
    see also http://stackoverflow.com/questions/1310247/in-r-do-you-use-attach-or-call-variables-by-name-or-slicing – Ben Bolker Feb 02 '14 at 15:03