重现问题的代码:
创建一列“a”的数据框。
dataObj=data.frame(a=1)
> dataObj
a
1 1
附上dataObj
attach(dataObj)
> a
[1] 1
修改dataObj,但a的值仍然没有改变。
dataObj[1,"a"]=3
> a
[1] 1
答案 0 :(得分:2)
来自?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.
副本将添加到搜索路径中,而不是对象本身。修改原件时,副本不会更改。
建议(许多人)避免attach
。 with
是一个方便的替代品。