[Rd] nested assignment (PR#2575)

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
Mon Feb 24 01:43:03 2003


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