[R] Simple syntax question (I think)
Duncan Murdoch
murdoch.duncan at gmail.com
Wed Jan 20 20:51:59 CET 2016
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