[R-sig-eco] help with assignment
Mike Dunbar
mdu at ceh.ac.uk
Fri Jul 4 10:30:43 CEST 2008
Exactly: STEER WELL CLEAR OF attach(). This is just the sort of problem you can get into.
Mike
>>> tyler <tyler.smith at mail.mcgill.ca> 04/07/2008 00:11 >>>
"Olivia LeDee" <lede0025 at umn.edu> writes:
> Would someone mind helping with the following code:
>
> 1) After attaching the primary dataset
>
> ambi1<-read.table("ambi.txt",header=TRUE,sep='\t')
> attach(ambi1)
> 2) I would like to change na's for variable (pwd) to 0
> pwd[is.na(pwd)]<-0
>
> 3) Then, I need the results of the following based on all observations.
> sqrt(area)
> m1<-lm(prs~area+sqrt(area))
> summary(m1)
> residuals(m1)
>
> 3) Next, I would like to omit na's from the rest of the dataset. I need this
> truncated dataset to include 0's for variable pwd.
>
> ambi2<-na.omit(ambi1)
>
> In this process, I am overwriting step 2. Can someone help (other than
> omitting na's for each column)?
>
Hi Olivia,
If I understand you correctly, the problem is that after this command:
> ambi2<-na.omit(ambi1)
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
--
This message (and any attachments) is for the recipient ...{{dropped:6}}
More information about the R-sig-ecology
mailing list