[R] Question about <<- assignment
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Jul 2 12:54:19 CEST 2009
On 01/07/2009 8:20 PM, Hsiu-Khuern Tang wrote:
> Is this expected behavior?
>
>> z <- 1:5
>> z[1] <<- 0
> Error in z[1] <<- 0 : object "z" not found
>
> The documentation seems to suggest that z will be found in the global
> environment and modified accordingly.
z <<- 0 works as documented, it's the indexing that fails. (I tried
other replacement functions and they also failed.) I think this case is
also behaving as documented, but the docs are in the R Language
Definition manual. To evaluate
z[1] <<- 0
the following sequence is what is supposed to happen:
- Look up z in the parent environment of the evaluation frame, and
assign to a temporary variable. (This is what fails; z is in the
evaluation frame, not its parent.)
- Set the first element of the temporary.
- Use <<- to assign the temporary to z.
This might be surprising, but I think it makes sense. Using <<- when
evaluating in the global environment is almost certainly an error;
shouldn't R signal it as such?
Duncan Murdoch
More information about the R-help
mailing list