[R] Simple syntax question (I think)

Bert Gunter bgunter.4567 at gmail.com
Wed Jan 20 21:40:16 CET 2016


Thanks to both Bill and Duncan for their help. As I said, my
mal-understanding of the syntax.

Best,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Jan 20, 2016 at 11:51 AM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> On 20/01/2016 2:21 PM, Bert Gunter wrote:
>>
>> Thanks Marc.
>>
>> Actually, I think the cognate construction for a vector (which is what
>> a list is also) is:
>>
>> > vector("numeric",2)[2] <-  3
>> Error in vector("numeric", 2)[2] <- 3 :
>>    target of assignment expands to non-language object
>>
>> but this works:
>>
>> > "[<-"(vector("numeric",2),2,3)
>> [1] 0 3
>>
>> I would have thought the 2 versions should be identical, but as you
>> allude, there are apparently subtleties in the parsing/evaluation that
>> I do not understand, so that the explicit functional form is parsed
>> and evaluated differently than the implicit one. The obvious message,
>> though, is: don't do this!
>>
>> I suspect there is a reference to this somewhere in the R Language
>> definition  or elsewhere, and if so, I would appreciate someone
>> referring me to it -- RTFM certainly applies!
>
>
> There's a detailed discussion in the Language Reference.  Search for
> "complex assignment", or look in section 3.4.4 Subset assignment. The big
> difference between the two expressions you give is in the final assignment
> of the result of the "[<-" function to the referenced variable.  In your
> first case there's no referenced variable, just an expression
> vector("numeric",2), so you get the error, just as you would with
>
> vector("numeric",2) <- c(0, 3)
>
> Duncan Murdoch



More information about the R-help mailing list