[R] Question about <<- assignment

Duncan Murdoch murdoch at stats.uwo.ca
Thu Jul 2 15:10:52 CEST 2009


On 7/2/2009 6:44 AM, Kenn Konstabel wrote:
> On Thu, Jul 2, 2009 at 4:34 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote:
> 
>>
>> On 2/07/2009, at 12: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.
>>
>>
> Still, the error message sounds confusing because how comes that `<<-` can
> find z but `[<<-` (which doesn't even seem to exist -- but does seem to
> work) can't?

<<- doesn't need to find z.  It will replace it if found, or create a 
new one if not.  (Personally I would have limited that to the first 
case, i.e. it should fail if it doesn't find z.)

Duncan Murdoch


> 
> Now try:
> 
>> z <- 1:5
>> local(z[3] <<- 1000)
>> z
> [1]    1    2 1000    4    5
> 
> Kenn
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list