Skip to content
Prev 254 / 7420 Next

help with assignment

Exactly: STEER WELL CLEAR OF attach(). This is just the sort of problem you can get into.
Mike
"Olivia LeDee" <lede0025 at umn.edu> writes:
Hi Olivia,

If I understand you correctly, the problem is that after this command:
you lose all the records that originally had NA values for the variable
pwd, despite having already converted the NAs to 0 in step 2. The reason
is that when you attach a dataset, you are actually making a copy. 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.  If you use
     '<<-' or 'assign' to assign to an attached database, you only
     alter the attached copy, not the original object. (Normal
     assignment will place a modified version in the user's workspace:
     see the examples.) For this reason 'attach' can lead to confusion.

This has indeed lead to your confusion. In step 2 you altered the
attached copy of your data.frame, not the original copy. Then in the
last step you refer to the original copy, not the altered copy. Then you
copied (and altered) the original copy when you created ambi2.

If you work through the examples in ?attach this should (maybe) become a
little clearer. The easiest solution may be to _not_ attach your primary
dataset, which requires more typing but less thought about search paths
and environments.

Cheers,

Tyler

-- 
Only YOU can stop forest fires.

_______________________________________________
R-sig-ecology mailing list
R-sig-ecology at r-project.org 
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology