[R] Using '[' as a function

Johann Hibschman jhibschman at gmail.com
Tue Aug 3 19:59:18 CEST 2010


Duncan Murdoch <murdoch.duncan at gmail.com> writes:
> On 29/07/2010 6:18 PM, chipmaney wrote:
>>
>> -Why does R recognize '[' as a function?  
>
> Because it is a function.

More explicitly, '[' is a string.  sapply then calls match.fun to look
up that string to get the function named '['.

>> -Why does it need the quotes?
>
> Because sapply(example,[,1) would not be syntactically valid.

And, luckily enough, sapply uses the match.fun machinery to allow string
names for functions.  This is also useful to ensure late binding of
functions in other contexts.

Johann



More information about the R-help mailing list