[R] "aliases" for R constructs?

Thomas Lumley tlumley at u.washington.edu
Tue Oct 21 00:02:44 CEST 2003


On Mon, 20 Oct 2003 Ted.Harding at nessie.mcc.ac.uk wrote:

> Hi Folks,
>
> My recent response to Laura Quinn's query about matrix subsetting
> reminded of a question.
>
> I wrote:
>
>   iDir <- ((Winds[,20]<45)|(Winds[,20]>315))&(!is.na(Winds[,20]))
>
> Now, I find "!is.na" a bit awkward to type, so I might prefer to
> type it as "nis.na".
>
> While it is possible to define
>
>   nis.na <- function(x){ !is.na(x) }
>
> this involves a slight bloat of overhead in execution since achieving
> !is.na involves an extra layer of function call. So is it possible
> to define an "alias" so that
>
>   nis.na(x)
>
> is _exactly_ equivalent to
>
>   !is.na(x)
>

No, R doesn't have macros.  As R is interpreted, a macro wouldn't
necessarily save you any execution time -- Lisp macros, IIRC, are only
expanded at compile-time.

You can get most of the programming advantages of macros with substitute
and eval, but this is presumably slower rather than faster.


	-thomas




More information about the R-help mailing list