From p.dalgaard@biostat.ku.dk  Mon Feb 24 01:43:03 2003
From: p.dalgaard@biostat.ku.dk (Peter Dalgaard BSA)
Date: Mon Feb 24 01:43:03 2003
Subject: [Rd] nested assignment (PR#2575)
In-Reply-To: <200302232254.h1NMs135014430@pubhealth.ku.dk>
References: <200302232254.h1NMs135014430@pubhealth.ku.dk>
Message-ID: <x2of52mrqs.fsf@biostat.ku.dk>

larry@cognition.wustl.edu writes:

> > A <- list(B=1, C=2, D=3)		# A list of 3 elements
> > attach(A)				# Attach it to the search list
> 
> > assign("D", 4, pos="A")		# Assign a new value to 'D', inside 'A'
> 
> 
> > ls()
>   D					# Looks good ...
> 
> > D
>   4					# Looks good ...
>   
> > A
>   $B
>   [1] 1
> 
>   $C
>   [1] 1
> 
>   $D
>   [1] 3					# Huh?
> 
> 
> *Both* values of 'D' somehow live in 'A':
> 
> > detach("A")
> > attach("A")
> > D
>   3					# Now the changed value is gone

The explanation is that attach() attaches a *copy* of the original
list (a virtual copy, physical copying only happens if you attempt to
change one version). The help page should probably have something on
this. 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907



[Rd] nested assignment (PR#2575)
Peter Dalgaard BSA